Iterate Through All an Array's Items Using loops(for)

Tell us what’s happening:
Have look in the test cases , im not understanding how the comparision in the
if condition takes place because we are using 2-d array in test cases but using only 1 for loop how can when i=0 check all three values in the array if it is not found or not(-1) .

Your code so far


function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for(let 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));

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

Link to the challenge:

If you write i.e. arr[0] you will get first element of the array arr. In this example that’s another array - [3, 2, 3]. So in condition indexOf() method is of the array is used to check whether elem is in arr[i], where i is from 0 up to length of the arr.

i dint understand
coinsider an array
[1,2,3][4,5,6]
how would traverse all elements using one single for loop

You don’t traverse all elements one by one. You loop arrays inside of the arr and let indexOf() method to check elements in each array arr[i].

1 Like