Https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-all-an-arrays-items-using-for-loops

Why does the result repeat itself twice?
What is the reason why it does not work?

function filteredArray(arr, elem) {
   let newArr = [];
  // Only change code below this line
for(var i=0;i<arr.length;i++){
 
   for(var k=0;k<arr[i].length;k++){

        if(arr[i][k]!==elem){
                 
                 newArr.push(arr[i]);
               
        }
 
   }  

}


  // Only change code above this line
  return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]],3));

Hi,
the result isn’t repeated twice. You need to check if a given ‘sub’-array contains elem and only append those who don’t. Right now you append each ‘sub’-array for each element of it that is not elem.
Hope that makes sense.

1 Like

What may I do to get an empty array. I am investigating methods, but I have not found it, which take elements from an array or eliminate it if it does not match the conditions.

function filteredArray(arr, elem) {
   let newArr = [];
  // Only 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]!=elem){
                         
                        console.log(arr[i][j]);
                 }
        }
 }



  // Only change code above this line
  return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]],3));

For example you could check each sub-array if it contains the given elem by using indexOf() and only adding it, if it doesn’t.
But I guess there are alot of ways to solve this.

1 Like

What is the reason why this is not working it? I want the newArr = ;

function filteredArray(arr, elem) {
   let newArr = [];
  // Only 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){

                    console.log(arr[i]);     
                       
                 }
        }
 }



  // Only change code above this line
  return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]],3));

Right now you’re just console-logging every element that isn’t elem. What exactly is your question here?

I want to use filter in this case, but I do not know how.

function filteredArray(arr, elem) {
   let newArr = [];
   let realArr = newArr.filter(function(newArr));
  // Only 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]!=elem){
                         
                        newArr.push(arr[i]);
                
                 //}
        }
 }



  // Only change code above this line
  return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]],3));

your referring to this?
for loop challenge
the challenge doesnt ask you to use filter to pass you can use a standard for loop to loop through the arrays and for each sub array you can use indexOf. You only need to go one level deep if you use array.indexOf because indexOf will itself loop for you so that will be in place of using another inner loop.

if you want to use filter you can do the following in pseudo:

set some variable pointing to the arr arguement passed into your filteredArray function on this arr call the filter method (this will be your outer iteration of the loop meaning all the sub arrays and the filter will automatically do this), to this filter method pass it a callback using the same logic you would otherwise use… (hint: the indexOf method will take care of your the second loop you need, which would be each element inside each sub array)
outside of this filtering and sub-loop(meaning after you finish those operations) return your variable

these are just some ways you could solve it… im not sure if the challenge specifically demands that you use a for loop to solve it but you can try…
basically with the filter it will run the callback function once for each element, and constructs a new array of all the values for which the callback returns a value that is true

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

  //filter method:
  const filtered= arr.filter(function(x){
    return x.indexOf(elem)<0
  })
  return filtered;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 9]], 3));
1 Like

You do have to use a for loop to pass the challenge.

I think using includes is a bit cleaner than using indexOf. Again, this is not a solution to the challenge, but I will blur it to not spoil the OP.

function filteredArray(arr, elem) {
  return arr.filter(nestedArray => !nestedArray.includes(elem))
}
1 Like

Now I can obtain an accomplished challenge, but the rest of them I cannot get them :frowning:

function filteredArray(arr, elem) {
  let newArr = [];
  // Only change code below this line
     
     for(var i=0 ;i<arr.length;i++) {
           
      let a =arr.indexOf(elem)

                 if (a !=-1 ) {
                       
                       newArr.push(a);
                    

                   }
     }


  // Only change code above this line
  return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

look carefully, what do you need to return?
what are you pushing to newArr?
are those related in the right way?

No, I am not doing it so. What do you suggest?

No, I am not doing it so. What do you suggest?

do not push a
figure what you actually need to push

It is done it!. Thanks!

function filteredArray(arr, elem) {
  let newArr = [];
  // Only change code below this line
     
     for(var i=0 ;i<arr.length;i++) {

             let a = arr[i].indexOf(elem)
                
                if(a==-1){
                      
                      newArr.push(arr[i] )
                     
                }
     }


  // Only change code above this line
  return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
2 Likes