Need help Seek and Destroy

Tell us what’s happening:

Your code so far


function destroyer(arr) {
  let arrNew=[];

  // Remove all the values
  for(let i=0;i<arguments.length;i++){
    arrNew.push(arguments[i]);
  }
  return arrNew.slice(0,1).filter( function(i) { return !arrNew.slice(1).includes(i) } );
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Your browser information:

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

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

return arrNew.slice(0,1).filter( function(i) { return !arrNew.slice(1).includes(i) } );

Please describe, what you think this line does.

Hi,
You have the right idea but I think your code became confusing with all the indexing and calls to slice. I believe if you log to console arrNew.slice(0,1) you will see why it is not working.

SPOILER
arrNew.slice(0,1) // returns a nested array [ [ 1, 2, 3, 1, 2, 3 ] ]
  //with only one array element

so below is not filtering what you think

arrNew.slice(0,1).filter(...)

Why not just call it arr as passed in the function arg list? That change alone would pass your code.

  arr.filter(...)

I would suggest…

  1. Only a slight change to your code to isolate the additional arguments into their own named array (which I named others here)
  2. Refer to the passed array as arr as named in the arguments list of function instead of slicing it from the arguments
function destroyer(arr) {
  let others =[];

  // isolate additional arguments
  // note i initialized to 1 to skip arr
  for(let i=1;i<arguments.length;i++){
    others.push(arguments[i]);
  }
  // now others is [2,3]

  return arr.filter( function(i) { return !others.includes(i) });
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

There are other ways to capture the additional arguments in an array but this was most like your original code.

1 Like