Basic Data Structures: Iterate Through All an Array's Items Using For Loops help me!

my code for some reasons isn’t working . can someone help please ??

function filteredArray(arr, elem) {

  let newArr = [];

  // Only change code below this line

  newArr = [...arr];

   for(let i = 0; i < arr.length ; i++){

     for(let j = 0; j < arr[i].length; j++){

       if(arr[i][j] === elem){

         newArr.splice(i, 1);





  // Only change code above this line

  return newArr;


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

mutating the array in which you are iterating is a bad idea, and your code never check all the elements as the array gets shorter

or in this case you are iterating on an array, and changing the other… newArr get out of sync with arr and the code is not doing what you think

still a bad idea.
do not mutate anything on which you use the iterating variable.

1 Like

What’s the code supposed to do?