Seek and destroy challenge, help needed

Seek and destroy challenge, help needed
0

#1

Hi coders!

Really struggling with the “seek and destroy” challenge in Beginning Javascript. This is what the challenge is about:

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.

And this is my code so far…

function destroyer(arr) {

var args = arr.slice.call(arguments);

function remove() {
for (i = 1; i < args.length; i++) {
if (args[0].indexOf(args[i]) === -1) {
return false;
}

}
return true;
}

var filtered = args.filter(remove);
return filtered;

}

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

Could anyone point me in the right direction?

Thanks
Rossella


#2

You probably meant args[0].filter not args.filter. Also remove should take one argument, and you don’t need to check every element in the array with indexOf because filter will call remove once for each element. So instead of:

if (args[0].indexOf(args[i]) === -1) {

you just need:

if (thisElement === args[i]) {

where thisElement is the parameter to remove
Here is a link to the Array.filter docs on MDN


#3

Thanks a lot, @Motardo!

I have been stuck on this challenge for quite a while now and I can finally move on to complete the Basic algorithm challenges.

Rossella