Plz explain me this line of code

Tell us what’s happening:
what does this:
if (arr[i].indexOf(elem) == -1)

code line mean
im so confused on this lienof code

Your code so far


function filteredArray(arr, elem) {
 let newArr = [];
 // Only change code below this line
for (var i=0; i < arr.length; i++) {
  if (arr[i].indexOf(elem) == -1) {
    newArr.push(arr[i]);
  }
}
 // Only change code above this line
 return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36.

Challenge: Iterate Through All an Array’s Items Using For Loops

Link to the challenge:

Hello. I’m not a professional but I will try to explain it a bit.
It says if the element is not present in the each array then push the array that doesn’t include the elem number. In your example you passed [[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]] as arr and 3 as elem. So it asks if 3 is not present in one of the individual array then push each array that doesn’t include 3 to newArr. You can try changing the 3 to 2 so it will output the arrays that doesn’t include 2, but in your case it will output arrays that don’t include 3 but all of the arrays include 3 so it will output empty array.
Basically -1 means that if element is not present.
If you want to test if the element is present in the array you can change == -1 to >= 0
Read about indexOf() in MDN especially this part: Finding if an element exists in the array or not and updating the array

But im confused in a way that indexOf() is used to give index of something so index of elem wouldnt be 0 in every case?

An array index can’t be negative ( less than 0). That’s is why doing >= 0 works.

array.indexOf(el) returns the element index if found; returns -1 otherwise.

Here is an alternative if you want to just know whether the array contains that value.

ok just tell me what will indexOf(elem) do for every cycle of loop

The arrays to check are arr = [[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]]
The element you’re checking the arrays for is elem = 3

First loop:

arr[i] = [3,2,3]
arr[i].indexOf(elem) returns 0, because it finds elem in the array at the index of 0

Second loop:

arr[i] = [1,6,3]
arr[i].indexOf(elem) returns 2, because it finds elem in the array at the index of 2

Third loop:

arr[i] = [3,13,26]
arr[i].indexOf(elem) returns 0, because it finds elem in the array at the index of 0

Fourth loop:

arr[i] = [19,3,9]
arr[i].indexOf(elem) returns 1, because it finds elem in the array at the index of 1

It finds the elem in all cases, therefore never returns -1, therefore never goes into the if-statement.

If you’d add a fifth loop that checks for an array [1,2,4]:
arr[i] = [1,2,4]
arr[i].indexOf(elem) returns -1, because it cannot find elem in the array, so instead of returning a legal array index (integers from 0 upwards), it returns a negative value which per specification is -1

1 Like

Hi,

It means that the element cannot be found in the array. In other words the array item is equal to ‘not found’.

From MDN Docs: " The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex . Returns -1 if the value is not found."

More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

thnaks this all my confusion was regarding what u have explained