Help me understand Seek and Destroy

Tell us what’s happening:

the code works, but I cant wrap my head around what
“var args = Array.prototype.slice.call(arguments).slice(1)” is doing. can someone help explain this?

Your code so far

function destroyer(arr) {
  // Remove all the values
  var args = Array.prototype.slice.call(arguments).slice(1);
  for (i = 0; i < arr.length; i++){
    for(j = 0; j < args.length; j++){
      if(arr[i] === args[j]){
        delete arr[i];
      }
    }
  }
  
  return arr.filter(Boolean);
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; CrOS x86_64 10323.58.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.167 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/seek-and-destroy

1 Like

Looking at the solutions, the “.slice(1)” isnt needed. which is even more confusing.

what about slice.call?

You may find this SO question to be beneficial:

Copying from the second answer:

Let me try to simplify: arguments is not an array. Slice() is an array method. To use Slice() on the arguments object we must take the arguments and pass them into the slice array method using call.

Since the call to slice returns an array, there is no need to call slice a second time.

2 Likes