0

I was able to complete the task using .filter() and a For Loop, but not sure why I cannot use the format in my second example. First example works fine.

function destroyer(arr) {

 for(i=1; i < arguments.length; i++){
   number = arguments[i];
   arr = arr.filter(function(num){
     return num !== number;
   });
  }
  return arr;
}

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

Tried to replace the return num !== number; with return num!==arguments[i] but the arguments[i] does not appear to return the numeric value.

2
  • The arguments object inside the filter callback is for that function, and will be different from the arguments object in the surrounding function. Commented Aug 10, 2017 at 20:47
  • Thanks for the feedback. Commented Aug 10, 2017 at 21:14

1 Answer 1

3

Because arguments refers to a different functions arguments at that point.

Every time you enter a function, arguments will refer to the currently executing functions arguments. It wouldn't make much sense for arguments to only refer to the top-level functions arguments.

Example:

function a() {
  console.log(arguments[0]);
  var b = function() {
    console.log(arguments[0]);
  };
  b('This is b');
}

a('This is a');

Sign up to request clarification or add additional context in comments.

2 Comments

I was not looking at it from that perspective, where the second function on the .Filter was involved. Makes sense now. Thanks.
@BradCunningham Sure thing! I remember having a similar issue with arguments at first haha.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.