đźš€ Check out my \$1,000,000 journey!

# JavaScript Coding Challenge: "Seek and Destroy"

Today weâ€™re going to solve another Coding Challenge. This one is categorized as: â€śintermediateâ€ť Coding Challenge. Uuuuuh :stuck_out_tongue:

Personally I donâ€™t think that this is that much of an â€śintermediateâ€ť challenge, butâ€¦ doesnâ€™t really matter, the important thing is that we have to solve this too, right? :smiley:

This is also coming from FreeCodeCamp and it says:

You will be provided with an initial array (the `first` argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

Note: You have to use the `arguments` object.

So basically weâ€™ll have to create a function called `destroyer`. When it will be called theyâ€™ll provide an `array` or `numbers` as the first argument and then one or more arguments as `numbers`. We need to remove all the occurrences of these numbers from the `array`.

### Example

In order for us to visualize it better letâ€™s see an example of what the function must do:

`destroyer([1, 2, 3, 2, 3, 1], 2, 3)` - Should return `[1, 1]` as we remove all the `2`â€™s and `3`â€™s from the provided `array`.

Easy peasy lemon squeezy. Right?

Unlike this little cute kitty which doesnâ€™t know how to tackle the situation, we know, right? xD Or if we donâ€™t know, weâ€™ll learnâ€¦ together! (Iâ€™m talking about solving the coding challenge, not about how we can squeeze a lemonâ€¦ for that weâ€™ll have to wait for the kitty to do it first and then teach us).

## The Solution

First thing first, letâ€™s write the function:

``````function destroyer(arr) {}
``````

Now, letâ€™s make a plan. As I told you in the previous post, itâ€™s importat to break down the steps before writing any code. This will simplify the process.

1. Get all the `values` from the `arguments` object using Object.values
2. Remove the first item as it is the provided `array`
3. Save all the remaining numbers into a separate array, letâ€™s call it `destroyerArr`
4. Filter out all the `numbers` that are into the `arr` by comparing them to the numbers from the `destoryerArr`.

It might sound a little confusing with all these arrays, but letâ€™s continue and once youâ€™ll see the code, it will be much simpler:

``````function destroyer(arr) {
// Step 3.        // Step 1.              // Step 2.
const destroyerArr = Object.values(arguments).slice(1);

// Step 4.
return arr.filter(x => !destroyerArr.find(y => y === x));
}
``````

As you can see in Step 4 we used the `find` method to check if the current item from the `arr` (the `x`) is in the `destroyerArr`. If it is, the `find` function will return it and by adding `!` in front of it, weâ€™ll convert it to a boolean and negate it.

Might be confusing, but letâ€™s see how thatâ€™ll work:

• when the `find` function will return a number, like: `2` for example, by adding `!` in front it will be converted to `false` -> JavaScript â€śmagicâ€ť. xD
• Otherwise if the `find` function will return `undefined`, because it couldnâ€™t find the number in the `destroyerArr`, the `!` operation will convert `undefined` to `true` -> JavaScript â€śmagicâ€ť reloaded!

We could also combine all the steps in a nice one-liner. Why doing that? Well.. just because itâ€™s fun and I like doing it! xD

``````function destroyer(arr) {
return arr.filter(
x =>
!Object.values(arguments)
.slice(1)
.find(y => y === x)
);
}
``````

Done! :smiley:

## Conclusion

Todayâ€™s challenge wasnâ€™t that hard, but I still enjoyed finding a solution to it.

Can you think of another way to solve it? Maybe a better approach? Letâ€™s see what you can find! :smiley: