Tell us what’s happening:
Your code so far
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for (let i = 0; i < arr.length; i++) {
//console.log(arr[i]);
// console.log(arr[i].indexOf(elem));
for (let j = 0; j < arr[i]; j++) {
// console.log(arr[i].indexOf(elem));
//console.log(arr[j]);
if(arr[i].indexOf(elem) === -1){
return newArr.push(arr[i]);
}else{
return newArr;
}
}
}
// Only change code above this line
return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
I think you should change
for (let j = 0; j < arr[i]; j++) {
this line to this
for (let j = 0; j < arr[i].length; j++) {
i didn’t understand , why i should get length about index arr[i]?
in first loop you used arr.length so in second loop arr[i] lets assume i = 0 arr[i] becomes
[3, 2, 3]
so you’re comparing j with a list in the second loop instead of that you change arr[i] to arr[i].length will be equal to 3 so second loop iterate 3 times and in the second loop you should change arr[i] to arr[i][j]
arr[i] is array so yoru code will be
for (let j = 0; j < arr[i].length; j++) {
// console.log(arr[i][j].indexOf(elem));
//console.log(arr[i][j]);
if(arr[i][j].indexOf(elem) === -1){
return newArr.push(arr[i][j]);
}else{
return newArr;
}
}
This will not work: j < arr[i]
, j
is a number, arr[i]
an array, the comparison doesn’t do what you want. You will need to use j < arr[i].length
(in the same way that in the outer array you do i < arr.length
)
Also,
remember that a return statement will stop the function and return something.
Remember also that push()
returns the new length of the array
I suggest you look at your code with this tool: Online JavaScript Compiler, Visual Debugger, and AI Tutor - Learn JavaScript programming by visualizing code
i catch the answer. i don’t need the second loop because i don’t need index array, i’m just want to check if number is not in array push array, without return , because I don’t need return anything to console, also your website is gooooooood