[Help]Iterate Through All an Arrays Items Using For Loops

[Help]Iterate Through All an Arrays Items Using For Loops
0

#1

Tell us what’s happening:
What’s wrong with this code? All conditions pass the test except one.

Your code so far


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

// change code here to test different cases:
console.log(filteredArray[ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-all-an-arrays-items-using-for-loops


#2

temp is never reset. I hope this hint might help you :slight_smile:


#3

Now I realized my silly mistake, Anyways thanks for your reply😊