Seek and Destroy - jumped a couple levels?

I was wondering if anyone else feels that Seek and Destroy is far more difficult than the previous problems. It may be that I never learned about the “.prototype” or the “.call”. So even looking at the answer does not help.

So, I am still struggling and trying to figure out what “.prototype” and “.call” is all about. Anyone have a brief explanation or a resource I should read?

I am quite frustrated, I have been moving through the “Basic Algorithm Scripting” challenges pretty well. I have had some road blocks that I was able to work through, learn and eventually understand it. Now that I hit “Seek and Destroy”, for the first time I feel like I don’t get it…

Can’t figure out why this doesn’t work.

function destroyer(arr) {
  // Remove all the values
  function remove(arr){  
  for (var i = 1; i < arguments.length; i++){
      if (arr != arguments[i]){
       return arr;
      }  
    }
  }
  return arr.filter(remove);
}

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

If I change:

  • arguments[ i ] to 1 - then it filters the 1’s,
  • arguments[ i ] to arguments[ 1 ] - then it filters nothing out

I also set var seek = arguments[ 1 ] and then checked the value of seek and it was 2, but when I put it in the code (arr != seek), it filtered nothing out again…

Yes… I am surprised!!! LOL.

Well now I am truly confused… I need some time to mull this over.

Thank you!

rmdawson71 you are the man!!! Thank you!

I literally spent 3 1/2 days working on this problem. (not proud, but I was determined to not look at the answer) The last example you put up there helped clarify my problem. Also, I never used console.log like I do now, it is very helpful…

Now, I am not sure if this code looks clean or if there is a better way, but here goes.

  function destroyer(arr) {
      // Remove all the values
    var count = 1;
    var destroyed = arr;
      
    for (var i = 1; i < arguments.length; i++){
      if (count <= arguments.length) {
        var args = arguments[count];
        count ++;
        destroyed = destroyed.filter(remove);
        }
        
       }
      function remove (arr) {
        if (arr != args){
           console.log(arr, args);
           return arr;
        }
      }
        
     return destroyed; 
    }
    destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Understood!

It does look better and easier to read.