Seek and Destroy still having trouble

Can’t figure out why this won’t work?


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

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

getItOut has its own arguments object so you’re targeting it instead of destroyer's arguments.

That’s just one of the reasons why this code doesn’t work.

can you explain what the difference is between the arguments?

Each function has access to it’s own arguments object (an array-like structure of any argument passed to it) inside it’s own scope, you can’t access a functions upper scope arguments object like you’re trying to do above.

Have a look at the below repl to get a better idea of what I mean. In this case, getItOut is logging each value in the array along with the other arguments filter provides: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter#Parameters

arguments are values passed to a function.

In your example destroyer is given following arguments:
[1, 2, 3, 1, 2, 3], 2, 3

getItOut arguments are those passed by filter and correspond to callback (predicate) parameters:

element The current element being processed in the array.
index The index of the current element being processed in the array.
array The array filter was called upon.

https://jsfiddle.net/549h2xof/

ok so can i ask where does getItOut get its arguments from? is it the same array but in a different order somehow?

filter calls getItOut internally. Once for each element of arr.

yeah, it seems like it works backwards from the end of the array though?

It loops through from the beginning of the array; if you look at the repl, you can see it printing out the values (the first item in the arguments object) from left to right: 1, 2, 3, 1, 2, 3

What makes you think it starts from the end?

because in that thing you posted it lists the first argument at the end under 2

“getItOut: { ‘0’: 1, ‘1’: 0, ‘2’: [ 1, 2, 3, 1, 2, 3 ] }
getItOut: { ‘0’: 2, ‘1’: 1, ‘2’: [ 1, 2, 3, 1, 2, 3 ] }
getItOut: { ‘0’: 3, ‘1’: 2, ‘2’: [ 1, 2, 3, 1, 2, 3 ] }
getItOut: { ‘0’: 1, ‘1’: 3, ‘2’: [ 1, 2, 3, 1, 2, 3 ] }
getItOut: { ‘0’: 2, ‘1’: 4, ‘2’: [ 1, 2, 3, 1, 2, 3 ] }
getItOut: { ‘0’: 3, ‘1’: 5, ‘2’: [ 1, 2, 3, 1, 2, 3 ] }”

Okay, so those log statements are logging the arguments object of getItOut for each loop of the array. Those arguments are passed in by filter on each loop, they are the following:

So if we take the first log

getItOut: { '0': 1, '1': 0, '2': [ 1, 2, 3, 1, 2, 3 ] }

‘0’ = element, 1 in this case
’1’ = index , 2 in this case
’2’ = array, this will just print the arry this was called on everytime.

Does that make sense?

oh so it only processes the elements in the array and doesn’t look at the other two arguments?

That’s correct, the other arguments are there, but in your code, your just looking at the first element, the value

oh ok so I just gotta figure out a way for the filter to read the other arguments and compare haha