hari1
1
Tell us what’s happening:
is my for logic is correct ? need review
Your code so far
function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for(var i=0;i<arr.length;i++){
for(var j=0;j<arr[i].length;j++){
if(arr[i][j].indexOf(elem)==-1){
return newArr.push(arr[i][j]);
}
}
}
// 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/72.0.3626.121 Safari/537.36.
Link to the challenge:
No need second for loop. Because indexOf() method will search whole arr[i].
And no need return inside the if statement, because here you push elements in the newArr.
hari1
3
[[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]] this one is nested array right ?
so in order check each array with value 3 it needs 2 for loop right ?
indexOf() method will check second loop for each index of first loop.
you can do it with 2 for loops. But here you want to use indexOf() method. So no need to 2nd for loop.
[3, 2, 3].indexOf(3)
Think about it…
This function should have only one return statement, at the end.
Do you need two loops here? Try this single loop to demonstrate what’s happening when you loop the passed in arr parameter:
for(var i = 0; i < arr.length; i++) {
console.log('Current iteration is ' + arr[i] + '\n');
}