Solving "Seek and Destroy" / freeCodeCamp Algorithm Challenges
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-07-06 18:54:46

---
title: Solving "Seek and Destroy" / freeCodeCamp Algorithm Challenges
published: true
description: My guide, notes, and solution to freeCodeCamp's basic algorithm challenge, "Seek and Destroy" 
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-019-seek-and-destroy
series: Solving freeCodeCamp's Algorithm Challenges
---

!['Seek and Destroy'](//images.ctfassets.net/yzwpq1epaq68/23S1dYp1DDeUHwCscTu1iO/c20dbdbbe9763dc811806425f7405cb1/ss-019.png)

Let's solve freeCodeCamp's intermediate algorithm scripting challenge, 'Seek and Destroy'.

### Starter Code

```javascript
function destroyer(arr) {
  return arr;
}

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

### Instructions

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.

### Tests

```
destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1].
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].
destroyer([3, 5, 1, 2, 2], 2, 3, 5) should return [1].
destroyer([2, 3, 2, 3], 2, 3) should return [].
destroyer(["tree", "hamburger", 53], "tree", 53) should return ["hamburger"].
destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan") should return [12,92,65].
```

---

# Our Approach

**Read** everything first. **Read** the instructions clearly, **read** the starter code we're given, and **read** the tests and understand what the challenge is asking of you.

- The function takes two or more arguments. `arr` is an array and it can be followed by one or more arguments. Looking at the tests, these additional arguments are numbers or strings.
- We need to evaluate `arr` items against the other arguments to see if there are any matching values.
- We have to use the `arguments` object.
- We need to return an array

Now that we understand what we are given and what we want to output, let's see how we can solve this.

We can start by understanding the `arguments` object.

#### [MDN: The arguments object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments "Arguments object on MDN")

**_"arguments is an Array-like object accessible inside functions that contains the values of the arguments passed to that function."_**

For example,

```javascript
function destroyer(arr) {
  console.log(arguments);
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
// [object Arguments] {
//  0: [1, 2, 3, 1, 2, 3],
//  1: 2,
//  2: 3
//}
```

So, we have our arguments in an 'array-like' object.

I'm thinking we can convert `arguments` into an actual `Array` so we will have more built-in methods to potentially work with.

From the above mentioned MDN documentation, we learn how to convert `arguments` into an `Array`.

```javascript
let args = Array.from(arguments);
// or
let args = [...arguments];
```

We now have an array of all our arguments. The first item in our new array is an array where we want to check if the other items exist. I would like to separate the `arr` from the other arguments, so we can call a frequently used method, `slice()`.

#### [MDN: Array.slice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice "MDN slice()")

We can call it like the below to result in having the other arguments in their own array.

`const argsArray = [...arguments].slice(1);`

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

This would give us `argsArray = [2, 3]`;

We can now prepare a for loop on the amount of `arr`'s length. We will have to check if each index in `arr` exists in our `argsArray`. If it is false, we can push it into a new empty array, and we would return the new array.

To check if the value exists in the array, we have one more `Array` method which we can use: `indexOf()`.

#### [MDN: Array.indexOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf "indexOf() MDN Docs")

When we compare, we will check the equality against -1 to see if the item is in the array or not (example below).

```
const nums = [1, 2, 3, 4];
console.log(nums.indexOf(20));
// -1
```

So, some pseudo-code:

```
function destroyer(arr) {
  create new array from arguments object but slice to remove arr
  create new empty array to house unique values
  for loop run arr.length of times
    if each index of arr is not in new args array
      add to unique array

  return unique array
}
```

---

# Our Solution

> :warning: 
> :warning: **Answer Below**
> :warning: 

  
```javascript
function destroyer(arr) {
  const argsArray = [...arguments].slice(1);
  const uniqueArray = [];
  for (let i = 0; i < arr.length; i++) {
    if (argsArray.indexOf(arr[i]) == -1) {
      uniqueArray.push(arr[i]);
    }
  }
  return uniqueArray;
}
```

---

### Links & Resources

['Seek and Destroy' Challenge on fCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy "Intermediate Algorithm Scripting: Seek and Destroy on fCC Website")

[freeCodeCamp](https://www.freecodecamp.org/, "freeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "fCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/019---seek-and-destroy.js "GitHub Solution")

Thank you for reading!