Seek and Destroy code doesn't work

Seek and Destroy code doesn't work
0.0 0

#1

I don’t understand why this doesn’t work


function destroyer(arr) {
  // Remove all the values
  function getItOut(value) {
    for (var i=0;i < arr[0].length;i++) {
value = (arr[0][i]==arguments[i+1]);
      return value;
     } 

  } return arr.filter(getItOut);
}

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

#2

What do you think arr[0][i] is supposed to be? You’re returning inside of your for loop. If arr[0][i] == arguments[i+1] represented anything, you’d be filtering so you only get matching numbers rather than getting rid of the matching numbers.

Look up how to use filter.


#3

Ok so value has to be false to get filtered right? So I changed it to !== but I still get the same result


#4

You didn’t read anything else I said.

arr[0][i] doesn’t exist. arr[0] is 1. You can’t index any further.

Then look at your for loop. i adds + 1 until arr[0].length (this means 1.length).
But even you wrote arr.length, that would mean i goes up to 5. Look at the rest of arguments. There is only 2 indexes. How would you compare arr[i] to arguments[i + 1] when they are different lengths?

Filter iterates over everything you’re calling it on. If you’re going to do a for loop in filter, you don’t need to do loop around the thing you are filtering. You don’t understand filter.

Change “value” to “number” to give yourself a better picture. Think of number representing arr[i] during a loop through arr. number is literally arr[i] in this case.

for (var i = 1; i < arguments.length; i++) {
   // start at i = 1 so we don't touch arr
  if (number === arguments[i]){ return false }
  // If there is a match, we break the loop and return false to the filter for that number
}

If we wrote something more like loop { variable = number !== arguents(i)} return variable then it wouldn’t matter if numbers in the middle tested false. All that would matter is the number at the end, because the variable is constantly changing and only the last iteration gets returned.


#5

Wouldn’t arr[0] be the array in the first argument?


#6

In the time it takes for you to ask that question and for me to type this you could’ve already known the answer to that question.

https://repl.it/languages/javascript


#7

Huh? Neither of those links lead to anything…


#8

arguments[0] is an array. arguments[1] & arguments[2] are not.

In your code, you’re treating all arguments as if they were in a single array. arguments[1][0] is undefined where arguments[1] == 2

arguments ==
[[1, 2, 3, 1, 2, 3], 2, 3]

You could change your for loop to i = 1 ( to bypass the test array ) & then adjust value to equal arguments[i]


#9

Oh right, that makes sense. Ok I changed line 6 to value = arguments[i]; but it’s still not working :frowning:


#10

You should work on your reading comprehension. I told you what to do the first time, then I gave you the answer. Then I gave you links to a places where you can easily test your code, and where you can practice doing algorithms.

How much hand holding do you need? Read my second response until you get the answer. arr and arguments are two different things.


#11

Why are you so rude, I wasn’t even talking to you