Hi. I am doing doing the “Seek and Destroy” challenge and i solve it but i think is a much better way. What changes or ideas you have to improve it?
function destroyer(arr) {
-- Seek and Destroy--
for(var i = 1; i < arguments.length; ++i) {
while(arr.indexOf(arguments[i]) != -1) {
arr.splice(arr.indexOf(arguments[i]), 1);
}
}
console.log(arr);
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.
How about this?
function destroyer(arr) {
var args = [].slice.call(arguments, 1);
return arr.filter(x => !args.includes(x));
}
The first line returns an array of the contents of arguments
except the first. (We can’t use arguments.slice(1)
because arguments
isn’t exactly an array).
Then the second line returns an array of elements in arr
that are not in args
.
Thanks for the new solution and explaniation!