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

Tell us what’s happening:

I did a code to solve the challenge, but I`m getting “not pass” and I observed that the console are giving me the error:

// running tests

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

should return

[ [10, 8, 3], [14, 6, 23] ]

But, this is incorrect, because it should return:

"
[ [ 10, 8, 3 ], [ 14, 6, 23 ], [ 3, 6 ] ]
"

Witch is what my code are returning. Maybe I`m missing something.

Best regards,

Felipe.

Your code so far


function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for (let x = 0; x < arr.length; x++) {
  newArr.push([]);
  for (let y = 0; y < arr[x].length; y++) {
    if (arr[x][y] != elem) {
        newArr[x].push(arr[x][y]);
    }
  }
}

// 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/80.0.3987.122 Safari/537.36.

Challenge: Iterate Through All an Array’s Items Using For Loops

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

hey @manfrin92

I think what it means is if a nested array has the elem in it remove that whole nested array, you are only removing the number inside the array.

1 Like

nope, read again:

such that any array nested within arr containing elem has been removed.

1 Like

Thank you all for the very quick answers, thats correct, I didn`t reallize that.

1 Like