Help with for loops in Arrays

Tell us what’s happening:
Hello everyone. in this challenge i need to create a for loop that runs through a nested array and removes the specific elem, now i sure my way is unnecessarily long, but i went over a few times and i cant seem to find whats wrong with it. i wanted it to cycle through the arr, the for loop with i as the variable, then if it comes across an array, i.e. nested in the initial one, then it would run the j variable, that why it could handle one dimensional arrays and nested ones.

I appreciate the help,

Your code so far

function filteredArray(arr, elem) {

 let newArr = [];
 // change code below this line
for (let i = 0; i < arr.length; i++){
   if (Array.isArray(arr[i])){
     for (let j = 0; j < arr[i].length; j++){
         if (arr[i[j]] != elem){
} else {
 if ( arr[i] != elem){
 // change code above this line
 return newArr;

// change code here to test different cases:
console.log(filteredArray([[3],3,3], 3));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) 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

Link to the challenge:

 if (arr[i[j]] != elem){

This is going to push the entire sub-array arr[i] to newArr for every element in arr[i] that does not match elem. I don’t think that’s what you you want to do.

1 Like

Okay so the lesson teach you about the for loop.
Which basicly is called that way because the for loop runs FOR a certain ammout of times. That ammout of times is determind by the conditions set in your code. The loop in this case is used to test if something is there.

This function iterates through and accesses each element of the array, and subjects it to a simple test that we have created. Basicly what there telling you is that they are using the code to look for data items that are greater then 10. See the example code in the lesosn how they did achieve this.

Now the 1st us mostly an introduction of the task you only need to know the next: Modify the function, using a for loop, to return a filtered version of the passed array such that any array nested within arr containing elem has been removed.

// change code below this line
for (let i = 0; i < arr.length; i++){
//this part seems to be alright but then…
why are u calling the console.log already? They already did it for you down bellow.
Same with like AL pointed out
if (arr[i[j]] != elem){

I stopped reading after that as it got very confusing and seemed full of uncearry elements. as a coder you alway’s should keep in mind to keep it simple. And clean as you most likely are going to work in a team.

What I really advice you to do is to look at the example they gave you. Annd think how it is setup.


hey there as far as the console.log i was just checking and then forgot to delete it.
i think i get what you and AL mean.
As far as the wordiness of the code, i’m positive that the way i did it was much more complicated then necessary, i just want to see if i could figure it out solo, which it turns out i couldn’t :stuck_out_tongue:

That’s fine sometimes you learn more from mistakes then you could have from solluting the whole problem on your own :slight_smile: