# Help with the Seek and Destroy algorithm

Help with the Seek and Destroy algorithm
0
#1

Hello,
I managed to write a working code for the Seek and Destroy algorithm, but as i saw other solutions my code is a bit messy and poorly written compared to them:

``````function destroyer(arr) {
var finalArr = [];
var argArr = [].slice.call(arguments);
var target = argArr.shift();
var test = argArr.slice(0);
finalArr = target.filter(function(item) {
if (!test.includes(item)) {
return target.slice(item);
} else {
finalArr.push(item);
}
});
return finalArr;
}

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

Could someone please help me to refactor this code, because even i donâ€™t really understand every piece of it, how it works and passed the test.
Thanks

0 Likes

#2

When posting working solutions, please blur them out using the `[spoiler]` â€¦ `[/spoiler]` tags.

Which pieces do you not understand?

Currently, your solution creates an empty array (finalArr). Then you create an array (argArr) of the function arguments, so using the example call to the function of destroyer([1,2,3,1,2,3],2,3); argArr has 3 elements with the first being an array and the other two elements representing the elements you want to remove from the first element. Next you assigned first element of the argArr array to target and assigned the remaining elements of argArr (the 2 and 3) to a variable called test (This line was not needed, because after the shift on the previous line, argArr only had the values 2 and 3 in it).

Next, you assign the result of the target.filter to finalArr. Your filter callback function is not using really taking advantage of the filter functionality. All you have to do is return true for the values you want to keep and assign to finalArr. You could have written your filter function code as:

``````  finalArr = target.filter(function(item) {
return !test.includes(item); // returns true if the item is not in the test arr
});
``````
0 Likes

#3

Oh sorry corrected the spoiler tag.

What i donâ€™t understand is the IF statement at the end.

My original code was: `if (test.includes(item)` so when the `item` is included in the test array, slice the item â€śoutâ€ť (donâ€™t put it to the `finalArr`) and when itâ€™s not included in the `test` than add it to the `finalArr`, and at the end give back the `finalArr`.

This code was wrong because it gave back the opposite what i wanted so i just added the `!` to the `if` statement. However i donâ€™t understand why the `!` was needed for the algorithm to pass.

Thank you for your help, and forgive me if im asking a banal question.

0 Likes

#4

Hereâ€™s my 2 cent breakdown.

So target held [1,2,3,1,2,3] after the shift. This is what we will be filtering. the test variable is actually unnecessary as it will hold [2,3], which is what argArr is now holding after you shifted the first index item out, but that is minor.
finalArr will hold the items filtered out based on the functionâ€™s boolean value being true or false. So, if test (which is [2,3]) includes item (each item in target ie [1,2,3,1,2,3] one at a time) we have a true result which pushes the item into the final array for us. Now you had to use ! to indicate that if test DID NOT include the item, it would push the item into the array. So when item is 1, test [2,3] does not include (or have present) a 1 inside it, so the 1 gets pushed to the finalArr.

Here is what I did:

``````function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);
var tempArr=args.shift();
var final=tempArr.filter(function(temp){
return !args.includes(temp);
});
return final;

}
``````

Matt

0 Likes

#5

Thanks very much Matt! I read your explanation and went through the JS basics again so itâ€™s all clear now.

1 Like