Seek and Destroy: arguments object with arrow function

if I understand the limitation "The arguments object is a local variable available within all non-arrow functions” correctly, the code

function destroyer(arr) {

       var args = arr.slice.call(arguments);

       return arr.filter(ar => !args.includes(ar));

}

should not work. But it works. Why?

My idea are: it works because here I don’t use built-in methods, which are unavailable in arguments, like forEach() or map(). Is my understanding correct?

Your browser information:

User Agent is: Chrome/79.0.3945.88.

Challenge: Seek and Destroy

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy

1 Like

You’re not trying to access arguments in an arrow function, that’s why you can access the arguments object.

You’ve accessed arguments at the top and converted it to an array, and saved that array as args.

3 Likes

ahha. Simpler than I suggested. Many thanks.

Note it just needs to be

var args = arr.slice.call(arguments);

That is a call to the slice function, you don’t need the additional .slice() at the end, that’s just calling the function again.

You can also use the clearer

var args = Array.from(arguments);
2 Likes

Yes, it was my technical mistake. I should be more careful, thank you.

My advice is once you learn about the arguments object from this lesson, forget it even exists, because it’s been obsolete since ES6. FCC really should remove it from the curriculum and just start with ES6.

2 Likes

Thank you) Anyway, I am happy that I can use it, too. Unexpected surprise. Here is my solution for ES6.

function destroyer(arr, ...manyMoreArgs) {
  return arr.filter(ar => !manyMoreArgs.includes(ar));
}

very simple.

2 Likes

I wouldn’t be happy for long using the arguments object. It’s really weird and broken in that it looks like an array but doesn’t have most of the methods of an array. Just use rest-arguments and life will be happier for you. I believe the next lesson (or at least one close after) teaches rest-arguments.

1 Like