Seek and Destroy - parsing to a "realArray"

Seek and Destroy - parsing to a "realArray"
0

#1

Tell us what’s happening:

I tried my code line by line in the Chrome console and it works fine. I believe my issue is I am failing to pass the initial argument “arr” into a actual array (realArr) so that I can apply the filter method. Any tips? Thank you!

Your code so far


function destroyer(arr) {
  // Remove all the values

let realArr = [arr];
console.log(realArr);
let remains = realArr[0];
for (let i = 1; i < realArr.length; i++){
      remains = remains.filter((x) => x != realArr[i]);
      console.log(remains);
    }
  return remains;
  }
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy/


#2

With the way your function is set up, arr is an array. In the test case you show above, arr is:

[1, 2, 3, 1, 2, 3]

You then created another array and added arr to it, so realArr is:

[ [1, 2, 3, 1, 2, 3] ]

The following line assigns remains another reference to arr:

let remains = realArr[0]; // remains is now [1, 2, 3, 1, 2, 3]

Your for loop does not execute any of it’s code block, because i starts as 1 and realArr.length is also 1 (it only has one element which is arr). Your for loop condition checks if 1 < 1, so it’s code block does not execute.

Finally, your return statement of return remains, just returns [1, 2, 3, 1, 2, 3], because you never reassigned remains to anything else.