# Basic Algo Seek and Destroy

I can’t seem to figure out why my code is incorrect. i’ve read the suggested solution, but it seems like my code should run and do the same thing but I can’t figure out what the hangup is. Can anyone help me figure out what’s wrong. Here’s what I have so far:

function destroyer(arr) {
// Remove all the values from initial argument(array) that are present in element 2-n

var endArr = Array.prototype.slice.call(arguments);
endArr = endArr.splice(0,1); //endArr will be final array w/ removed elements
var target = Array.prototype.slice.call(arguments);
target.shift(); // target is the array with the elements to be deleted

//iterate through each element of endArr and if it’s in target, remove that element
for (i=0; i < endArr.length; i++)
if (target.includes(endArr[i])) {
endArr.splice(i,1);
}

return endArr;

}

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

Hi,

what’s happening is that this portion of your code:

``````var endArr = Array.prototype.slice.call(arguments);
endArr = endArr.splice(0,1); //endArr will be final array w/ removed elements
``````

results into endArr containing another array as its (only) element. If I use the example you gave, after those two lines of code, endArr looks like this:

``````endArr = [[1, 2, 3, 1, 2, 3]];
``````

Whereas your target array contains simple integer values.

``````target = [2, 3];
``````

Therefore, the following test condition will never evaluate to true.

``````if (target.includes(endArr[i])) {
// endArr[i] is an array!
// endArr[i] = [1, 2, 3, 1, 2, 3];
}
``````

And that’s why no element is removed from endArr.

I hope this helped! Happy learning!

P.S: beware of the operation you’re carrying out inside the loop! You’re mutating your array while iterating on it… This will definitely have unwanted side effects.
For instance, as soon as you remove an element, the size of your array changes. So what you’d expect to be element at position 2, is actually now element at position 1!

Thank you so much for the help. I wasn’t sure how else to take the first argument and make it [1,2,3] as opposed to [[1,2,3]]. So I just iterated through it and created a new array with the same values. kind of a redundant solution and i’m positive that it is not the most elegant or most efficient way of doing it but it does work and pass all tests.

I just wanted to share the final solution for anyone that came across this thread in the future.

final passing code was:

function destroyer(arr) {
// Remove all the values from initial argument(array) that are present in element 2-n

var endArr = Array.prototype.slice.call(arguments);
endArr = endArr.splice(0,1);

var endArr2 = [];//endArr2 is the first arg that we’re removing els from
for(i=0; i < endArr[0].length; i++) {
endArr2.push(endArr[0][i]);
}

var target = Array.prototype.slice.call(arguments);
target.shift(); // target is the array with the elements to be deleted

//filteredArr is endArr2 after we’ve removed the target elements AKA final return value
var filteredArr = endArr2.filter(function (el){
return target.includes(el) == false;
});

return filteredArr;

}

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

1 Like