Iterate Through All an Array's Items Using For Loops question

Iterate Through All an Array's Items Using For Loops question
0

#1

Tell us what’s happening:
I cannot seem to catch the mistake(s) I made. Maybe because the array gets modified and messes with the for loops?

Your code so far


function filteredArray(arr, elem) {
  let newArr = [];
  // change code below this line
newArr=JSON.parse(JSON.stringify(arr));
for (let i=0; i<newArr.length; i++) {
  for (let j=0; j<newArr[i].length; j++) {
    if (newArr[i][j] == elem) {
      newArr.splice(i;1);
    }
  }
}
  // 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));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 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

I am sure the semicolon newArr.splice(i;1); have to be comma newArr.splice(i,1);


#3

If you didn’t already, you can open Chrome’s developer console and see if there are any syntax errors in the console view. The newArr.splice(i;1); with the semi-colon instead of a comma as noted by another user would show up as a syntax error in the console.


#4

Yes, my bad. I replaced it “;” with a comma, but now I get this error:

Cannot read property ‘length’ of undefined


#5

I don’t think you need to use the JSON.parse or even JSON.stringify here. You are not dealing with any JSON data at this point. Remember that the first parameter is just a multidimensional array.
Just make use of a single for loop to access each of the sub arrays and then return only the sub arrays that do not contain the second parameter passed to your function.

**HINT: ** convert each subarray into a string and if it doesn’t contain the ‘elem’ paramter, then push this sub array into the newArr variable


#6

Hi
Yes, mutating your array while looping through it is causing you problems.

I believe that you could make that work if you captured newArr.length in a variable before your loop and work from the other (right) end of newArr using i-- counting down to 0
BUT
I would suggest that newArr start as empty array, loop over arr, and push filtered subarrays onto newArr. That avoids copying the entire array step and avoids the mutate while looping over an array thing.

Good luck


#7

Okay, so I tried a different approach, as suggested by allhazen1 and also using a count variable, to count how many times elem is found in the nested array, however, only one challenge is validated. My guess is I got the if statement in the wrong position but I am not sure.

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

// change code here to test different cases:
console.log(filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter");

#8

Hi,

I tested your code and it passed for me

You might need to refresh your browser or clear your cache.

Spoilers

Some spoilers here including your current solution (that should pass) and one much like your original code
https://repl.it/@alhazen1/Alt-Iterate-Through-All-an-Arrays-Items-Using-For-Loops