Iterate Through All an Array's Items Using For Loops 3

Tell us what’s happening:
Hello friends,
Can anyone explain to me why my code does not work?
I already checkout the FCC solution and i was able to understand their code. Still i’m not satisfied because i thought mine was supposed to do the job too. And it did for most argument.

Passed

filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18) should return [ [10, 8, 3], [14, 6, 23] ]

Passed

filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2) should return [ ["flutes", 4] ]

Passed

filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter") should return [ ["amy", "beth", "sam"] ]

but not this one ;(

Not Passed

filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3) should return [ ]

Please explain why this is happening because i could not ;(

The instruction was

" We have defined a function, filteredArray , which takes arr , a nested array, and elem as arguments, and returns a new array. elem represents an element that may or may not be present on one or more of the arrays nested within arr . Modify the function, using a for loop, to return a filtered version of the passed array such that any array nested within arr containing elem has been removed."

Your code so far


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

// change code here to test different cases:
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/71.0.3578.98 Safari/537.36.

You have a problem with the indexes of your array. When you modify the arr with splice, it re-indexes it. Try to debug and you will see exactly what the problem is.