While trying to solve a challenge that reads " Modify the function, using a for
loop, to return a filtered version of the passed array such that any array nested within arr
containing elem
has been removed."
I wrote following code–
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for (let x =0; x < arr.length; x++){
for (let y = 0; y < arr[x].length; y++){
if (arr[x][y]==elem){
arr.splice(x,1);
}
}
newArr.push(arr[x]);
}
// Only change code above this line
return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
It generates following error “TypeError: Cannot read property ‘length’ of undefined”
Can you please explain this to me? I am unable to find what’s wrong?