Can someone explain why my ‘if statement’ didn’t work here? I got the solution, but I’m trying to figure out why my if statement didn’t work in the first place?
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for(let i = 0; i < arr.length; i++) {
if (arr[i] !== elem) {
newArr.push(arr[i]);
}
}
// Only change code above this line
return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
Your if statement does not work because the arr element is a 2 dimensional array. It just means it’s an array that contains other arrays in it. So when you said arr[i] you are accessing another array. And if we look at it, [3, 2, 3] is not the same as 3.
If you want to access individual items inside the array, you would create another for loop to loop through all the items or you can use the .forEach() method.