Seek and destroy (intermediate algo scripting)

Tell us what’s happening:

I was able to eliminate the first instance of the arguments, however i can’t eliminate multiple instances. I think I might have to count the number of instances each arg shows up in the array but I’m not sure how to do this optimally.

Your code so far

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

for(let i = 1; i < arguments.length;i++){  
      const index = arr.indexOf(arguments[i]);
      if (index > -1) {
        arr.splice(index, 1);

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36.

Challenge: Seek and Destroy

Link to the challenge:

The problem with your code is that splice mutates (changes) the arr and now i points to the wrong position.
One fix would be to add i-- after the line where you do splice.

Also I’m not sure why there is a note that you have to use arguments. Because one could use rest operator (...) which, imho, is much readable.

function destroyer(arr, {
  return arr.filter(n => !rest.includes(n))
1 Like

Thank you! After looking at the documentation for the rest operator I don’t know why the note said to use arguments either. Your solution is very readable and optimal.

Edit: In the challenge it does not allow to add an additional parameter to the destroyer function, which is why I’m assuming they advised using arguments.

Adding rest operator to function parameters is equivalent to using arguments.
And my solution passes the tests and is accepted even without using arguments explicitly ¯\_(ツ)_/¯