# freeCodeCamp Challenge Guide: Seek and Destroy

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [June 30, 2016, 9:19pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046 "2016-06-30T21:19:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [June 30, 2016, 9:19pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/1 "2016-06-30T21:19:53Z")

</div>

# [Seek and Destroy](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy)

* * *

## Problem Explanation

This problem is a bit tricky because you have to familiarize yourself with Arguments, as you will have to work with two **or more** but on the script you only see two. You will remove any number from the first argument that is the same as any of the other arguments.

#### Relevant Links

- [Rest parameter syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters)
- [Arguments object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments)
- [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

* * *

## Hints

### Hint 1

The **rest parameter** syntax allows the function to accept an indefinite number of arguments, placed into an array. If you use the `arguments` object convert it into a regular array.

### Hint 2

You may want to use various methods like: `indexOf()`, `includes()`, or `filter()`. When in doubt about any function, check the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)!

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> function destroyer(arr) {
> const valsToRemove = Object.values(arguments).slice(1);
> const filteredArray = [];
> 
> for (let i = 0; i < arr.length; i++) {
> let removeElement = false;
> for (let j = 0; j < valsToRemove.length; j++) {
> if (arr[i] === valsToRemove[j]) {
> removeElement = true;
> }
> }
> if (!removeElement) {
> filteredArray.push(arr[i]);
> }
> }
> return filteredArray;
> }
> 
> ```
> 
> #### Code Explanation
> 
> 1. Create an array of `valsToRemove` using `Object.values(arguments).slice(1)` and store it in the variable `valsToRemove`. We’ll use this to check against `arr`.
> 
> 2. Start a basic `for` loop to iterate through `arr`. Nest another `for` loop inside the first, changing the integer variable `j` and `arr` to `valsToRemove`. This second loop will iterate through `valsToRemove` .
> 
> 3. Outside of the nested loops, return the `filteredArray`.
> 
> #### Relevant Links
> 
> - [Arguments Object](http://forum.freecodecamp.org/t/how-arguments-work-in-javascript-javascript-arguments-guide/14283)

> **Solution 2 (Click to Show/Hide)**
>
> ```javascript
> function destroyer(arr) {
> const valsToRemove = Array.from(arguments).slice(1);
> return arr.filter(function(val) {
> return !valsToRemove.includes(val);
> });
> }
> 
> ```
> 
> #### Code Explanation
> 
> 1. Declare a variable named `valsToRemove` and set it equal to a new `Array` object `from()` the `arguments` passed into the function. Use the `slice()` method on the array of arguments, starting from the second index, 1.
> 
> 2. Return the filtered array, using `includes()` in the callback function to check if `val` is _not_ in `valsToRemove`; returning `true` to keep the value in the original array or `false` to remove it.
> 
> #### Relevant Links
> 
> - [arguments](http://forum.freecodecamp.com/t/javascript-arguments/14283)
> - [Array.slice()](http://forum.freecodecamp.com/t/javascript-array-prototype-slice/14302)
> - [Array.includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)

> **Solution 3 (Click to Show/Hide)**
>
> ```javascript
> function destroyer(arr, ...valsToRemove) {
> return arr.filter(elem => !valsToRemove.includes(elem));
> }
> 
> ```
> 
> #### Code Explanation
> 
> - Using spread operator to retrieve the arguments.
> - Return the filtered array, using `includes()`.
> 
> #### Relevant Links
> 
> - [Rest parameter syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters)

---

<div class="post-metadata">

**Author:** ![samuelpath](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/samuelpath/32/162094_2.png) [@samuelpath](https://forum.freecodecamp.org/u/samuelpath)\
**Post date:** [July 17, 2016, 1:23pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/2 "2016-07-17T13:23:53Z")

</div>

> [@Rafase282](#):
>
> var args = Array.prototype.slice.call(arguments);

Why use `var args = Array.prototype.slice.call(arguments)` when `var args = Array.from(arguments)` does the same job in a seemingly more elegant and concise way?

---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [July 17, 2016, 1:41pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/3 "2016-07-17T13:41:08Z")

</div>

They are two valid ways, i just listed one. You are free to use either one.

---

<div class="post-metadata">

**Author:** ![Cairos](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cairos/32/160860_2.png) [@Cairos](https://forum.freecodecamp.org/u/Cairos)\
**Post date:** [August 6, 2016, 11:26pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/4 "2016-08-06T23:26:22Z")

</div>

Made an edit using both

---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 7, 2016, 1:06am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/5 "2016-08-07T01:06:52Z")

</div>

Please update the sample code, they all have the same old link. @Cairos

---

<div class="post-metadata">

**Author:** ![Cairos](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cairos/32/160860_2.png) [@Cairos](https://forum.freecodecamp.org/u/Cairos)\
**Post date:** [August 7, 2016, 12:52pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/6 "2016-08-07T12:52:11Z")

</div>

Will do, sorry about that

---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 10, 2016, 6:18pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/7 "2016-08-10T18:18:12Z")

</div>



---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 20, 2016, 3:40am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/8 "2016-08-20T03:40:15Z")

</div>



---

<div class="post-metadata">

**Author:** ![gsuxlzt](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gsuxlzt/32/17613_2.png) [@gsuxlzt](https://forum.freecodecamp.org/u/gsuxlzt)\
**Post date:** [September 24, 2016, 5:38am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/9 "2016-09-24T05:38:22Z")

</div>

Hi. I just finished this exercise and found out that my code is nowhere near the answer. However, it works. Can someone check if I did it right, and what points did I miss if ever? Here’s my code:

```
function destroyer(arr) {
arr = arguments[0];
var res= [];
for (var i = 1; i < arguments.length; i++) {
console.log(arguments[i]);
for (var j = 0; j < arr.length; j++) {
  if (arr[j] === arguments[i]) {
    delete arr[j];
    console.log(arr);
  }
}
}
  for (var i = 0; i < arr.length; i++) {
    console.log(arr[i]);
    if (arr[i]) {
      res.push(arr[i]);
    }
  }
return res;
}

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

```

As you can see I didn’t use the filter method. However, I found out that I can actually do `arr.filter(Boolean);` to remove the null values inside the array. Can someone enlighten me about the `Array.prototype.slice.call(arguments)` and why I need to use is instead of setting my array to arguments[0]? Thank you.

---

<div class="post-metadata">

**Author:** ![faustodc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/faustodc/32/21716_2.png) [@faustodc](https://forum.freecodecamp.org/u/faustodc)\
**Post date:** [October 20, 2016, 7:38am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/10 "2016-10-20T07:38:43Z")

</div>

I wanted to build upon my previous mutation solution, because this algorithm is about keeping the mutations, so I used my previous algorithm to find them, and used `filter()` to keep them:

> function destroyer(arr) {  
> var targets = Array.prototype.slice.call(arguments, 1);
> 
> return arr.filter(function(val){  
> for(var i = 0; i \< targets.length; i++){  
> if(targets.indexOf(val) \< 0)  
> return true;  
> }  
> return false;  
> });  
> }

@gsuxlzt The spread operator which you are using is another way to achieve the similar result ([Spread Operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator)), I instead use it to get an array with the targets to keep to compare against the main array.

---

<div class="post-metadata">

**Author:** ![Omegga](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/omegga/32/97940_2.png) [@Omegga](https://forum.freecodecamp.org/u/Omegga)\
**Post date:** [October 20, 2016, 8:28am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/11 "2016-10-20T08:28:32Z")

</div>

You don’t need to assign arguments[0] to arr, arr is already arguments[0].Since there is only one named parameter in the function, it only references the first element of arguments.

The other arguments are put an array by using Array.prototype.slice.call(arguments).

the slice() method is used by an array to copy an array, but because arguments is not really an array(it’s an array-like object), you can’t do arguments.slice(1).So we use Array.prototype.slice.call(arguments, 1) to force arguments to use the slice method, as if it is an array.

---

<div class="post-metadata">

**Author:** ![correx98](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/correx98/32/17776_2.png) [@correx98](https://forum.freecodecamp.org/u/correx98)\
**Post date:** [October 28, 2016, 3:09pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/12 "2016-10-28T15:09:05Z")

</div>

I’ve just passed this task and found out that my solution is much simpler but it also works and I hope it could be useful for the understanding of basic algorithm of the method filter.

function destroyer(arr, val1, val2, val3) {

function filterForFalsy (arr) {  
if (arr !== val1 && arr !== val2 && arr !=val3) {

```
  return arr;   
}  

```

}

return arr.filter(filterForFalsy);  
}

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

---

<div class="post-metadata">

**Author:** ![mamorvan](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@mamorvan](https://forum.freecodecamp.org/u/mamorvan)\
**Post date:** [November 7, 2016, 7:51pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/13 "2016-11-07T19:51:23Z")

</div>

I used a similar solution but realized that it will only work for up to 3 arguments. It passes all the tests on the exercise but the directions say 1 or more arguments so it should work for any number of arguments.

---

<div class="post-metadata">

**Author:** ![skratchbreaker](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/skratchbreaker/32/23914_2.png) [@skratchbreaker](https://forum.freecodecamp.org/u/skratchbreaker)\
**Post date:** [November 17, 2016, 10:53pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/14 "2016-11-17T22:53:59Z")

</div>

I am really confused and hopefully someone will be able to help me. Why does:

return arr; // only returns the first argument?

Is arr an object or an array?

---

<div class="post-metadata">

**Author:** ![ddvs2012](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ddvs2012/32/24433_2.png) [@ddvs2012](https://forum.freecodecamp.org/u/ddvs2012)\
**Post date:** [November 28, 2016, 3:11am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/15 "2016-11-28T03:11:18Z")

</div>

I did the same thing, couldn’t figure out how to create a callback function that would work with filter, still don’t really understand how the filter works.

```javascript
function destroyer(arr) {

  var args = arr.slice.call(arguments); 
  
 
    for (j=0; j<arr.length; j++){
      for (i=1; i<arguments.length; i++){
        if (arguments[i] === arr[j]){
          delete arr[j];
          
        }
      }
    }
  
  return arr.filter(Boolean);
  
}

```

---

<div class="post-metadata">

**Author:** ![ddvs2012](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ddvs2012/32/24433_2.png) [@ddvs2012](https://forum.freecodecamp.org/u/ddvs2012)\
**Post date:** [November 28, 2016, 3:22am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/16 "2016-11-28T03:22:34Z")

</div>

arr is a normal array

arr only returns the first argument because arr is the first argument. The other arguments 2, and 3, are not arr, they don’t have a parameter name because it was never set. Instead to access them you have to call them by arguments[1] and arguments[2].

arr can also be called arguments[0].

Hope that helps 🙂

---

<div class="post-metadata">

**Author:** ![roon83](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/roon83/32/95386_2.png) [@roon83](https://forum.freecodecamp.org/u/roon83)\
**Post date:** [February 7, 2017, 7:27pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/17 "2017-02-07T19:27:47Z")

</div>

Here’s my solution:

```
function destroyer(arr) {
  // Remove all the values
  var arrOfArgs = [];
  for (var i = 1; i < arguments.length; i++) {
    arrOfArgs.push(arguments[i]);
  }
  
  function filterArgs(value) {
    return !arrOfArgs.includes(value);
  }
  return arr.filter(filterArgs);
}

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

---

<div class="post-metadata">

**Author:** ![razieltov](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@razieltov](https://forum.freecodecamp.org/u/razieltov)\
**Post date:** [February 9, 2017, 9:07pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/18 "2017-02-09T21:07:52Z")

</div>

I think my solution is simple and works well.

function isValid(value) {  
if(this == value) {  
return false;  
} else {  
return true;  
}  
}  
function destroyer(arr) {  
// Remove all the values  
for(var i = 0; i \< arguments.length; i++) {  
arr = arr.filter(isValid,arguments[i]);  
}  
return arr;  
}

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

---

<div class="post-metadata">

**Author:** ![kizeeto](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kizeeto/32/163203_2.png) [@kizeeto](https://forum.freecodecamp.org/u/kizeeto)\
**Post date:** [March 12, 2017, 8:15pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/19 "2017-03-12T20:15:13Z")

</div>

Here’s my overly simplistic solution 😤

```
function destroyer(arr) {
  // Create two new arrays. One for the main array and another for unwanted chars.
  var unwanted = [];
  var givenArr = arguments[0];
  
  // Push all other args to the unwanted array
  for (var i = 1; i < arguments.length; i++) {
    unwanted.push(arguments[i]);
  }
  
  // A function for testing if a member of the unwanted array matches a char of main array.
  function tester (value) {
    if (unwanted.includes(value)) {
      return false;
    } else {
      return true;
    }
  }
  
  // filter out chars which donot meet the above criteria
  var filtered = givenArr.filter(tester);
  
  // return the filtered array to the console.
  return filtered;
}

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

---

<div class="post-metadata">

**Author:** ![lieberscott](https://avatars.discourse-cdn.com/v4/letter/l/bc79bd/32.png) [@lieberscott](https://forum.freecodecamp.org/u/lieberscott)\
**Post date:** [April 1, 2017, 8:22pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046/20 "2017-04-01T20:22:21Z")

</div>

Another solution!

```
function destroyer(arr) {      
  // newArr comprises an array of all arguments passed in,
  // slicing off the first
  var newArr = Array.from(arguments).slice(1);
  
  // fllter arr values against newArr values in line below
  arr = arr.filter(function(val) {
  
      // use ternary to return false if newArr val is in arr, else true
      return (newArr.includes(val)) ? false : true;
  });
  
// return updated arr
  return arr;

}

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

[Next page](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-seek-and-destroy/16046.md?page=2)
