Help me understand Seek and Destroy

Help me understand Seek and Destroy
0.0 0

#1

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:


#2

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


#3

var args = Array.prototype.slice.call(arguments);

The above line creates an array out of the arguments object which contains all the arguments passed to the function. The array reference is then assigned to the variable args.


#4

what about slice.call?


#5

You already know what slice does, but for call, take a look at the documentation.

.


#6

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.