Intermediate Algorithm: Seek and Destroy

Hey guys!

Working on Seek and Destroy… Thought I was going down the right track… but having issues. Where am I going wrong here…

  function destroyer(arr) {
    // Gives me the first argument
    let array1 = arguments[0];
    let newArr = [];
    for (var i = 0; i < arguments.length; i++) {
      if (array1.includes(arguments[i])) {
        newArr = array1.filter(x => x !== arguments[i]);

  destroyer(["tree", "hamburger", 53], "tree", 53);

This returns “tree”, “hamburger”… am I not looping correctly…

You’re making things complicated here.

First, understand the challenge/problem correctly and think about how you’d approach this? Write pseudocode.

Let’s understand the problem first:

destroyer(["tree", "hamburger", 53], "tree", 53);
// Should return => ["hamburger"]

The very first argument is the array we will be filtering/deleting items from, and the items we need to delete can vary from arguments[1] to arguments[n], there’s no limit.

So let’s start with what we understand,

// We can use ES6 features to take advantage here.
// (See below for ES5 version)

function destroyer(arr, ...itemsToDelete) {
    // `arr` argument will be: ["tree", "hamburger", 53]
    // `itemsToDelete` will be: ["tree", 53]   <-- Notice that we have them in array

    // Now we need to filter the `arr` and check if the values exists in `itemsToDelete`?
    // If it exists, then we DON'T need it, means we need to return false there.
    //  try to implement this from here. It's just a one-liner 
     return arr.filter..... // complete this.

ES5 version

function destroyer(arr) {
   // We have the first argument already named `arr`, so nothing to do here.
   // To gather the rest of the arguments we need to copy the whole `arguments` list
   // But WITHOUT the first element. Why? because the first element is our `arr`
   //  and we already have it. We just need to gather the rest of the arguments
   // into an array so we can work easily.

   var itemsToDelete = arguments.slice(1);  // ["tree", 53]

   // `slice` returns a new array starting from the index you provide.
   // we provide 1 here because 0 index is our `arr` argument and we don't want that. 

  // The rest is the same:
   return arr.filter.... // complete it

Read more about the slice here at MDN: