Help with Seek and Destroy please

Help with Seek and Destroy please
0.0 0

#1

Hi folks,

So I am at Seek and Destroy and this is my code but I cannot get it to work, in theory( in my head ) it works.:

What am I doing wrong here?

var results = arr.concat();
for (var i = 0; i < results.length; ++i) {
for (var j = i + 1; j < results.length; ++j) {
if (results[i] === results[j]) results.splice(j–, 1);
}
}
return results;

What do you think? Should I scrap this and go back to the drawing board? Cheers!!


#2

Rather then having a single small error your code seems a little off the right track. Without any comments or pseudo code it’s hard to say if it’s an error in your logic or an problem with your implementation.

Part of the issue is that currently you are only looking at the first argument (the initial array) and not using the additional arguments (the items to be removed from the array). Essentially your code is currently removing duplicate vales from the array, rather than removing the specified values.


#3

Cheers for this - my pseudo code is on paper but it goes like this

1, array contcat into 1 shallow array
2, check to see for dups
3, if…

Ah, I think I see where I am going wrong now.

Cheers for your help - back to the whiteboard!!


#4

You don’t want to add the numbers to the array, like you’re trying to do with concat(). In fact, I’m pretty sure arr.concat() isn’t working how you think it is because the function is only pulling the array with the arr value. I.e. the function may only care about the array (arr), but you need the other side of the argument to get the correct outcome too.

The key is in the “Arguments object” link.


#5

Excellent - I will re-read this again and see what happens.

Cheers again and will let you know when I have passed it!!


#6

Oh! I see what you’re trying to do. Again though, arr.concat() isn’t adding the rest of the argument like you think it is. If you console.log(arr.concat()); you can see the results in Chrome under Console when you use the Dev tools.

Keep in mind I’m a newbie on par with you, just a slightly further ahead, so take my advice with a bit of a grain of salt. But that’s what looks to be the main problem.