Seek and Destroy need hint

Seek and Destroy need hint
0.0 0

#1

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


#2

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.


#3

Thank you! i dindt know how to post code on forum sorry about that.

I´m trying with this

  var toArray = Array.prototype.slice.call(arguments);
  var destroyers = toArray.slice(1,toArray.length);

Now ill see how to properly use .filter() once i got the arguments in an array.


#4

Excellent, far more concise than my poorly-crafted construct!


#5

I wrote a blog post on seek and destroy a while back. Check it out if you still need hints (it doesn’t show the answer until the end).


#6

Nice thanks a lot !! :wink:


#7

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!


#8

@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…


#9

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!


#10

Cool! Glad to help…


#12

Thanks to your post I was able to pass the challenge, and I think I understood most of it, too. Cheers!


#13

Great to hear! It’s encouraging to know that people actually read it!