# Basic Algorithm Scripting problem: Seek and Destroy, Get hint. Intermediate code solution. explanation?

**URL:** <https://forum.freecodecamp.org/t/basic-algorithm-scripting-problem-seek-and-destroy-get-hint-intermediate-code-solution-explanation/152524>\
**Category:** General\
**Created:** [October 11, 2017, 7:14am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-problem-seek-and-destroy-get-hint-intermediate-code-solution-explanation/152524 "2017-10-11T07:14:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kozmickai](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kozmickai/32/66519_2.png) [@kozmickai](https://forum.freecodecamp.org/u/kozmickai)\
**Post date:** [October 11, 2017, 7:14am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-problem-seek-and-destroy-get-hint-intermediate-code-solution-explanation/152524/1 "2017-10-11T07:14:52Z")

</div>

> [@freeCodeCamp Challenge Guide: Seek and Destroy](https://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-guide-seek-and-destroy/16046):
>
> [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.…

intermediate code solution:

```auto
function destroyer(arr) {
  var args = Array.from(arguments).slice(1);
  return arr.filter(function(val) {
    return !args.includes(val);
  });
}

```

Was wondering if someone could clarify the use of (val) in relation to .include and its use in both the function and the callback.  
I’m just confused as to where it came from?

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [October 11, 2017, 8:11am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-problem-seek-and-destroy-get-hint-intermediate-code-solution-explanation/152524/2 "2017-10-11T08:11:45Z")

</div>

The `.filter` function requires a callback function that has at least one parameter (`val`), which represents an element in the `arr` array.

So what the entire `arr.filter(..)` call does is it returns a new array whose elements are in the `arr` array but are not included in the `args` array.

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [October 11, 2017, 9:13am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-problem-seek-and-destroy-get-hint-intermediate-code-solution-explanation/152524/3 "2017-10-11T09:13:23Z")

</div>

- `arr` is the actual array given as the first argument.
- `args` is the rest of the arguments given to `destroyer` - so like `destroyer([1,2,3,1,2,3], 1, 2)`, `args` would be `[1,2]`.
- `filter` goes through each value in an array one by one and tests them each in turn. `val` in the callback is just a name for the current value being tested. It returns an array containing every value that tests as true.
- `includes` checks if the value given to it is in an array - `[1,2].includes(2)` would be true, `[1,2].includes(4)` would be false.
- the unary `!` negates. So `![1,2].includes(2)` would be false. You want the filter to return false if the target value is one of the values in `args` - this will drop it from the return value.

* * *

If I add console logs to the function, I can see what happens when the function funs, then each iteration of filter as it goes through the array:

> **Version of the solution with console logging added:**
>
> ```auto
> function destroyer(arr) {
> console.log(`The array to filter is [${arr.join(',')}]`);
> var args = Array.from(arguments).slice(1);
> console.log(`The values to destroy are [${args.join(',')}]`);
> return arr.filter(function(val) {
> const valIsATarget = (args.includes(val)) ? '*is*' : '*is not*';
> const whatToDo = (args.includes(val)) ? '*filter it out*' : '*keep it*';
> console.log(`
> Iterating through array, current value being checked is ${val}.
> The value ${valIsATarget} one of the values that should be destroyed.
> Return *${!args.includes(val)}* for it, which will *${whatToDo}*.
> `);
> return !args.includes(val);
> });
> }
> 
> ```

* * *

If I run the above in the console, I get:

```auto
> destroyer([1,2,3,1,2,3], 1, 2)
The array to filter is [1,2,3,1,2,3]
The values to destroy are [1,2]

     Iterating through array, current value being checked is 1.
     The value *is* one of the values that should be destroyed.
     Return *false* for it, which will **filter it out**.
    
     Iterating through array, current value being checked is 2.
     The value *is* one of the values that should be destroyed.
     Return *false* for it, which will **filter it out**.
    
     Iterating through array, current value being checked is 3.
     The value *is not* one of the values that should be destroyed.
     Return *true* for it, which will **keep it**.
    
     Iterating through array, current value being checked is 1.
     The value *is* one of the values that should be destroyed.
     Return *false* for it, which will **filter it out**.
    
     Iterating through array, current value being checked is 2.
     The value *is* one of the values that should be destroyed.
     Return *false* for it, which will **filter it out**.
    
     Iterating through array, current value being checked is 3.
     The value *is not* one of the values that should be destroyed.
     Return *true* for it, which will **keep it**.
  
Array [3, 3]

```

(first line is input into the console, the last line is the final return value)

---

<div class="post-metadata">

**Author:** ![kozmickai](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kozmickai/32/66519_2.png) [@kozmickai](https://forum.freecodecamp.org/u/kozmickai)\
**Post date:** [October 11, 2017, 11:22am UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-problem-seek-and-destroy-get-hint-intermediate-code-solution-explanation/152524/4 "2017-10-11T11:22:21Z")

</div>

Thanks for the indepth explanation I think I even learnt more than expected haha. but just to confirm:

As the arr array is being filtered and checked with the callback function each element in arr will be stored in val one at a time. Then it is compared to the args array using args.include(val) to check if the current arr element is included in the args array and if so it is removed from the arr array.

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [October 11, 2017, 1:53pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-problem-seek-and-destroy-get-hint-intermediate-code-solution-explanation/152524/5 "2017-10-11T13:53:09Z")

</div>

Yip, that’s about it. 🙂

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:37pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-scripting-problem-seek-and-destroy-get-hint-intermediate-code-solution-explanation/152524/6 "2021-01-17T20:37:49Z")

</div>


