Iterate Through All an Array: all test passing except one

My code passes all the tests except this test.

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

my code returns [1,6,3] and [19,3,9] instead.
Can anyone help mme with whats happening?


function filteredArray(arr, elem) {
  let newArr = arr;
  // change code below this line
  for(let i=0; i< newArr.length; i++){
   newArr[i].indexOf(elem)>=0? newArr.splice(i,1) : newArr.splice(i,0);
  }
  // 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 (X11; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-all-an-arrays-items-using-for-loops

If you successfully remove an array, you need to check for the next array but your code doesn’t. It removes an array THEN it skips the next array then checks the array after the next array.

Hint: Think about where i stands after you call splice.

1 Like