Seek and Destroy Algorithm on FreeCodeCamp

Tim Parsons
Sep 6, 2018 · 2 min read

Day 16/100 of my #100DaysOfCode

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
argumentsobject.

This exercise was a tricky one for me. For some reason, I couldn’t wrap my head around it and figure out how I could use the arguments object to get it to work.

I created a new variable
let newArr = Array.prototype.slice.call(arguments); but misread it in the console and didn’t see it as a whole array.

I think due to the fact I misread it, it didn’t click for me on what to do next.

The 15-minute challenge was up, so I took a look at the answer. As always, very disappointed with myself and feel like I should have got it.

function destroyer(arr) {let newArr = Array.prototype.slice.call(arguments)
for(var i = 0; i < arr.length; i++) {
for(var j = 0; j < newArr.length; j++) {
if(arr[i] === newArr[j]) {
delete arr[i];
}
}
}
return arr.filter(Boolean);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

I was on the right track with the newArr variable, but, that doesn’t get me anywhere in coding interviews. From there, we need to loop through the arrargument to check all of the items. Nested within that loop, we need to loop through the new array that was created in the variable newArr to see if there are any items that are the same.

If any of the items match, delete that item from the original arrargument. Originally, I thought that by returning arr would return the correct number so thought I’d give it a try. Instead, the console gave me this:

console.log(arr)

The numbers were correct but the ‘empty x 2’ in the array isn’t helping me. This wouldn’t help me pass. So after researching the answer at the top, I started to understand that by filtering for a Boolean, it would only return the numbers that were truthy i.e. the result we needed.

Very disappointed with myself on this one. I need to start getting a better grasp on them. Bring on the next one.