I understand that -1 is the response from a indexOf when elem does not exist, so we’re checking this to log the ones that don’t exist, but how is the result of he equation below the following:
[ [ 10, 8, 3 ], [ 14, 6, 23 ] ] — doesn’t [ [ 10, 8, 3 ] have index of 0 and [ 14, 6, 23 ] ] index of 1?
If it was filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)
should return [ ["flutes", 4] ]
I thought it was logging [ ["flutes", 4] ]
because it elem was 4 and not 2, but it appears not.
Your code so far
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for (let i = 0; i < arr.length; i += 1) {
if (arr[i].indexOf(elem) === -1) {
newArr.push(arr[i]);
}
}
// Only change code above this line
return newArr;
}
console.log(filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36
.
Challenge: Iterate Through All an Array’s Items Using For Loops
Link to the challenge: