# Help: "Iterate Through All an Array's Items Using For Loops"

Tell us what’s happening:
Hi,

I’m getting stuck on this challenge and am just looking for some hints/feedback. To explain what I have so far…

1. I have a nested For loop (“lion” & “tiger”) to loop through the content of the arrays.

2. I then have an If statement that says if the elem DOES NOT exist in the array, push the array (using spread operator) to “newArr”

However it’s not working. I would really appreciate a hint to get me on the right track.

Thanks!

``````
function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
let lion = arr.length;
for (let i=0; i<lion; i++) {
let tiger = arr[i].length;
for (let n=0; n<tiger; n++){
if (arr[i][n].indexOf(elem) === -1){
newArr.push(...arr[i]);
}
}
}
// change code above this line
return newArr;
}

// change code here to test different cases:
console.log(filteredArray([10, 8, 3], [14, 6, 23], [3, 18, 6], 3));

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36`.

Challenge: Iterate Through All an Array’s Items Using For Loops

Second loop is not needed. For example:
`arr[0] = [10, 8, 3]`
When you add second loop, you are looping through single numbers, for which `.indexOf(elem)` will not give you result that you expect.

1 Like

Thanks for this, I made an update by removing the second loop, but still doesn’t seem to be working correctly. Another hint would be appreciated.

``````function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for (let i=0; i < arr[i].length; i++) {
if (arr[i].indexOf(elem) === -1){
newArr.push(...arr[i]);
}
}
// change code above this line
return newArr;
}

// change code here to test different cases:
console.log(filteredArray([10, 8, 3], [14, 6, 23], [3, 18, 6], 3));
``````

Are you sure about using arr[i].length here?

What do you think this line is doing?

1 Like

Thank you, I updated the code to the below. One issue I was having is that I sometimes use codepen.io to demo the challenges.

With the below code I kept getting an “Uncaught TypeError: arr[i].indexOf is not a function at pen.js:-1” error message. But when I tested it on FCC, it passed. Do you know why there is a disconnect with codepen.io? This was causing me confusion.

``````function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for (let i=0; i < arr.length; i++) {
if (arr[i].indexOf(elem) === -1){
newArr.push(arr[i]);
}
}
// change code above this line
return newArr;
}
``````

The reason you are getting that error, is because you are not correctly calling the function. The test cases have a two-dimensional array as the first argument. Your code above has 4 arguments (the first 3 are one-dimensional arrays).

Upon reviewing it, I know why, I had changed the array at the very bottom to test different iterations, there is a syntax error which is causing it to not work, my mistake!