Iterate Through The Items Of An Array Using For Loops

Hi everyone,

I have no idea why my code isn’t working. I’m at my wit’s end! I wrote a for loop. I thought I was right to use the splice( ) method to remove all instances of elem. There’s something I’m missing, but I don’t know what it is. 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 (arr[i].indexOf(elem) == -1) {
    return arr.splice(elem[i]);
    newArr.push(arr[i]);

  // change code above this line
  return newArr;
}

// change code here to test different cases:
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], );

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 11895.118.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.159 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

Remember that when a return statement is executed, the function exits and does finish anything after it and that includes completing a for loop.

Thanks, Randell. You were a big help!