# Seek and Destroy - jumped a couple levels?

Seek and Destroy - jumped a couple levels?
0

#1

I was wondering if anyone else feels that Seek and Destroy is far more difficult than the previous problems. It may be that I never learned about the “.prototype” or the “.call”. So even looking at the answer does not help.

So, I am still struggling and trying to figure out what “.prototype” and “.call” is all about. Anyone have a brief explanation or a resource I should read?

I am quite frustrated, I have been moving through the “Basic Algorithm Scripting” challenges pretty well. I have had some road blocks that I was able to work through, learn and eventually understand it. Now that I hit “Seek and Destroy”, for the first time I feel like I don’t get it…

#2

Don’t worry about trying to use .prototype or .call. Write out your algorithm steps without any code first and then use the javascript you have already learned to solve it. I originally solved this problem 3 different ways and none involved using .prototype or .call

Sometimes the hints prevent you from using your “own solution” to solve the problem. I ignore the hints until after I have solved the problem. Then, I sometimes have gone back and tried to solve the problem using the hints.

After developing the algorithm and attempting to code it, paste your code into the forum here and someone will take a look to point you in the right direction.

#3

Can’t figure out why this doesn’t work.

``````function destroyer(arr) {
// Remove all the values
function remove(arr){
for (var i = 1; i < arguments.length; i++){
if (arr != arguments[i]){
return arr;
}
}
}
return arr.filter(remove);
}

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

If I change:

• arguments[ i ] to 1 - then it filters the 1’s,
• arguments[ i ] to arguments[ 1 ] - then it filters nothing out

I also set var seek = arguments[ 1 ] and then checked the value of seek and it was 2, but when I put it in the code (arr != seek), it filtered nothing out again…

#4

In the for loop, put the following right before your if statement, to see what you are comparing. I think you will be surprised about what is actually being compared.

Hopefully, this will give you some insight:

``````console.log(arr, arguments[i]);
``````

#5

Yes… I am surprised!!! LOL.

Well now I am truly confused… I need some time to mull this over.

Thank you!

#6

Each function has it’s own arguments. Test the code below to see the results:

``````function testArguments(main1,main2,main3) {
function innerFunction(inner1,inner2,inner3) {
for (var i = 0; i<arguments.length;i++) {
console.log(arguments[i]);
}
}
for (var i = 0; i<arguments.length;i++) {
console.log(arguments[i]);
}
innerFunction('i1','i2','i3','i4','i5');
}
testArguments('m1','m2','m3');

// yields the following:
m1
m2
m3
i1
i2
i3
i4
i5
``````

#7

rmdawson71 you are the man!!! Thank you!

I literally spent 3 1/2 days working on this problem. (not proud, but I was determined to not look at the answer) The last example you put up there helped clarify my problem. Also, I never used `console.log` like I do now, it is very helpful…

Now, I am not sure if this code looks clean or if there is a better way, but here goes.

``````  function destroyer(arr) {
// Remove all the values
var count = 1;
var destroyed = arr;

for (var i = 1; i < arguments.length; i++){
if (count <= arguments.length) {
var args = arguments[count];
count ++;
destroyed = destroyed.filter(remove);
}

}
function remove (arr) {
if (arr != args){
console.log(arr, args);
return arr;
}
}

return destroyed;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);``````

#8

Much better! I am glad I helped you see things in a different light and introduce you to the power of console.log. My only other advice is more of a personal preference, but I like to see all internal functions of another function at the very top of each function. See below where I used your exact code and moved the remove function to the top of the destroyer function. It just tends to read better this way because it is not stuck in the middle of the code (before the return in your function). Coding this way will you help you organize things better when you start putthing multiple internal functions in your solutions. You and anyone else will know to look at the top of each function for any other functions.

``````function destroyer(arr) {
// Remove all the values
function remove (arr) {
if (arr != args){
console.log(arr, args);
return arr;
}
}

var count = 1;
var destroyed = arr;
for (var i = 1; i < arguments.length; i++){
if (count <= arguments.length) {
var args = arguments[count];
count ++;
destroyed = destroyed.filter(remove);
}
}
return destroyed;
}
``````

#9

Understood!

It does look better and easier to read.