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

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

#1

Here is the 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/

Here is my current saved code that works in CodePen :

here is the code : ( in case you didnt want to click the link :slight_smile:

function filteredArray(arr, elem) {
  let newArr = [];
  // change code below this line
  
for (let i=0; i<arr.length;++i){
  let subArr=[];
  
   for(let j=0; j < arr[j].length; ++j)
  
     if(arr[i][j] != elem){
    
   
    subArr.push(arr[i][j]);

  }
     newArr.push(subArr);
}
  // 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]], 3));

it works there but copy and pasting it into the challenge and NOTHING passes ? Not even the " use a for loop test … So what gives ?

THank you in advance !


#2

Debugging-wise, you want to see if there are any errors in the console itself. If you’re in Chrome, open the developer console, run the tests, then see if there are any errors there, first. You may see something like the following:

transformers.js:85 TypeError: unknown: Cannot read property '0' of undefined
    at Function.e.get (babel.js:3632)
    at e.h [as unshiftContainer] (babel.js:42437)

You can actually copy/paste your function into the console, then copy/paste the console.log statement above to see what the output is. As it turns out, it works in the console, so the problem may be in how the FCC code is running the function.

As it turns out, the problem seems to be with

for(let j=0; j < arr[j].length; ++j)
     if(arr[i][j] != elem){
    subArr.push(arr[i][j]);
  }
     newArr.push(subArr);

Although this should work, apparently the FCC code doesn’t like that the for loop doesn’t surround the if block with brackets, i.e.,

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

Once you get past that, however, you’ll find that your solution doesn’t pass the tests. You want to remove arrays that contain the second argument, not remove the second argument from each array, e.g., one example from the tests is below:
‘’’
filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18) should return [ [10, 8, 3], [14, 6, 23] ]
‘’’

P.S. — The debugger statement in the function that I’ve commented out allows you to stop at that point in the function and use the Chrome developer tools to step thru the function to see how it behaves. There’s a lot that you can do in the developer tools, so it’s worthwhile to play with those features and see how things work, if you haven’t already.


#3

It made perfect sense as soon as I had enough caffine in the brain ROFL… As soon as you pointed out the fact that I read the problem incorrectly … let the headbanging begin !

Thank you for taking the time to respond… !


#4

One additional question… ( warning… this may also be pre-coffee brain) …

in the if statement where :
if( arr[i].indexOf(elem)<= -1 ){}

Why is the (<) Less than operator needed there if the .indexOf only returns the -1 if the item being searched for is not present ?

THanks again.