# Basic Data Structures Question 11/20 Iterate through all an Array's Items using a For Loop

Basic Data Structures Question 11/20 Iterate Through Elements on an Array Using a For Loop I was trying to solve this problem using two for loops instead of using indexOf method suggested in the solution. My idea was to create a variable in which i would store all elements of arr[i] item that are not equal to elem, then if the length of that variable was the same as the length of the arr[i] (which it should be if there are no items that equal elem) i would push it to newArr but for some reason the code isn’t working.

function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for(let i=0; i<arr.lenght; i++){
let placeholder = [];
for(let j=0; j<arr[i].length; j++){
if(arr[i][j]!==elem){
placeholder.push(arr[i][j]);
}
}
if(placeholder.length===arr[i].length){
newArr.push(placeholder);
}
}
// 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’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

The typo in length is going to cause some issues.

Correcting this typo did solve almost all of the problems only the second test did’t pass.

filteredArray([["trumpets", 2], ["flutes", 4], ["saxophones", 2]], 2)

Hmm, I used the original code you pasted above and fixed the length type and it passed for me. Can you paste your updated code in here?

I just reloaded the page and tried again it passed all the tests now, so all is good. Thanks for the help.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.