Seek and destroy solution confusion

Seek and destroy solution confusion
0.0 0

#1

I got to the algorithmic thinking challenges and found some of them tricky, but with patience worked my way to a solution. This one though, I really struggled. I eventually stumbled to github, followed the hints, and was still lost. Eventually (don’t judge) I peeked at all ur solution. And all I can say is I’m baffled. I really don’t understand it. I read through the filter page and found the extra parameters really confusing. This is the solution I found.

function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);
args.splice(0, 1);
return arr.filter(function(element) {
return args.indexOf(element) === -1;
});
}

Can anyone help me understand how it is working? What does it mean when it says element and how does this term get understood by the computer?


#2

This is the same solution in ES6

const destroyer = (arr) => {
  let args = [...arguments].slice (1);
  
  return arr.filter (val => (args.indexOf (val) == -1));
};

Explain:

We have a function destroyer with the first argument arr where are all the elements we have to “purify”.
First we call the others arguments where are the elements to remove from arr, but in arguments there is also the first argument ( arr ) and we already have it, then we delete it with slice(1) ( remove first element from an array, Array.prototype.slice ).
The last action we have to do is filter the array arr, ie remove the elements. I think you was confuse by this function. ( To understand better: Array.prototype.filter )

You can see it in this way:

var start_array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];

function filter_callback (element) {
 if ( element % 2 == 0 ) return true;
 return false;
}

console.log (start_array.filter (filter_callback));

For each element in start_array filter call the filter_callback function and this filter_callback returns “true” if the element is pair. When filter returns false the element is removed from the main array.

In my, and in your solution, this callback is defined directly as argument and what do this callback? It checks if the current element in arr is somewhere in args through the indexOf function, this last function returns -1 if the element passed as argument doesn’t exist in the array on which is called.
Have done that, you have your solution.

Summary:

/* Build a function called "destroyer" */
function destroyer (arr) {
  /* Get the elements to remove in arr. 
  * Example: destroyer ([1, 2, 3], 2, 1);
  * var args = [ [1, 2, 3], 2, 1]
  */
  var args = Array.prototype.slice.call (arguments);
  /* Remove the first element from args because we already have it as "arr" */
  args.splice (0, 1);

  /* Return the filter result, every arr's element is passed as argument to
   * the callback function */
  return arr.filter (function (element) {

    /* For each element check if exist in args. If it doesn't exist ( === -1 )
     * return true, else false 
     */
    return args.indexOf (element) === -1;
  });
}

#3

This is still absolutely confusing.