Hi, i would love to get some hint how to improve this code, it only work with a give number of arguments (in this case only 2 numbers to seek and destroy)
I need some hint on how to improve it to search and destroy as much arguments as given.
function destroyer(arr) {
var target = arguments[0];
var destroyer1 = arguments[1];
var destroyer2 = arguments[2];
return target.filter(function(val){
return val !== destroyer1 && val !== destroyer2;
});
destroyer([1, 2, 3, 1, 2, 3], 2, 3); // this works
destroyer([1, 2, 3, 1, 2, 3], 1,2, 3); // this wont work beacuse i have only defined 2 destroyers
First off, to post code in the forum, use triple backticks. See here for more details.
Secondly, if you are only looking for a hint, (and because I can’t remember the details of this challenge), you should place the rest of the arguments into an array and continue from there. To collect the rest of the arguments, excluding the first, you could simply use a for loop, starting at one. Example:
var args =[];
for(var i = 1; i < arguments.length; i++){
args.push(arguments[i]);
}
Then perform your checks on the contents of the args array. This is probably not the most efficient way, but it was the easiest off the top of my head.
Excellent post. I went back to my challenge to see how I completed it, but for some reason the code for all of my challenges has been reset to default. I doubt my solution was as eloquent as yours though!
@JavaTheNutt Ha, Thanks! You’re definitely the first to call my code eloquent… If you go to you’re profile page (where they show the calendar of activity, etc) is there a list of completed projects and algorithms? If you click ‘view solution’ you should be able to see what you wrote…
Yes, that worked! Some of the more complex challenges I saved on CodePen so that I can review them, but most were just saved on FCC. I was afraid that I wouldn’t be able to view them again. For some reason when I navigate using map, I can only see the default code.
And my solution was somewhat similar to yours, with a second loop instead of the filter function. Filter is a much more concise method though. I’m going back to refactor now!