Succesfully iterated through an array but can't pass the test

Succesfully iterated through an array but can't pass the test
0

#1

Hello, I’ve managed to complete the challenge in a sense that console logs the correct answer but I can’t get the test to pass. Maybe I’m doing something wrong here?


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

// change code here to test different cases:
console.log(filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18));

Thank you for your help :slight_smile:


#2

I suspect that you are not supposed to end up with a nested array? (This isn’t something you can see with console.log() because when JS converts arrays to strings, you can’t see their structure.


#3

Why does it nest the array 2 times though?


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

This line tells me that i is an array, and arr is a multi-dimensional array.
Then you do this:

newArr.push(arr);

Which pushes a multi-dimensional array into another array, making it a 3D array.