Basic Data Structures: Iterate Through All an Array’s Items Using For LoopsPassed
So while working on this challenge I decided to come up with a different solution for it. But for some reason it does not work, I understand the solution that appears in the help section but I would like to know what part of my logic is wrong. I am filling the “newArr” with all the objects inside “arr” and then eliminating the ones that contain the “elem” as I iterate through it.
function filteredArray(arr, elem) {
let newArr = [...arr];
// Only change code below this line
for(let i = 0; i < newArr.length; i++){
let elemCheck = newArr[i].indexOf(elem);
if(elemCheck != -1){
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));
According to my logic console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)); should outcome “” but instead returns “[ [ 1, 6, 3 ], [ 19, 3, 9 ] ]”.