Basic Data Structures - Iterate Through All an Array's Items Using For Loops

Tell us what’s happening:
Hello everybody,
I don’t understand why it’s failing to return the right answer when I check my code with this example :
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26],[19, 3, 9]], 3))
it keeps returning : [ [ 1, 6, 3 ], [ 19, 3, 9 ] ]
I’ve checked with other arrays and they all returned the right answer except for this one. Can you please help me if my code is wrong somewhere?
Thank you so much.

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){
    arr.splice(i, 1);
  }
} newArr = arr;
  // Only change code above this line
  return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26],[19, 3, 9]], 1))



Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36

Challenge: Basic Data Structures - Iterate Through All an Array’s Items Using For Loops

Link to the challenge:

You want to return a new array, so you don’t want to make changes to the original arr passed into the function, you want to create a completely new array to return.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.