I not stuck here but, I came along the documentation of the arguments object and copied the lineArray.from(arguments).slice(1); both functions seem to work for all of the tests, but I barely understand why the second one is working. Can somebody help me?
Your code so far
function destroyer(arr) {
var args = Array.from(arguments).slice(1);
return arr.filter((item) =>{
return !args.includes(item)
});
}
function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);
return arr.filter((item) =>{
return !args.includes(item)
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36.
The arguments object (as the name indicates) is an object, but it has Array-like features. It has a .length property, and its items are indexed starting with 0, just like in a real Array, but it’s not a real Array, so the usual methods aren’t available for it (you can’t use myObject.slice(), because objects don’t have a .slice method).
So, in order to use a method like .slice on an Array-like object, you can either turn it into an array (like the first function does), or you can tell JavaScript that it should take the .slice method from Array.prototype and .call it on the Array-like arguments object.
First you have to understand the difference between arrays and array like object.
here in this statement:-
Array.from(arguments).slice(1)
A copy array is made with arguments as elements. Here in slice method “this” is set to the array created and slice work normally as it works with array
But here:-
Array.prototype.slice.call(arguments)
we are using the call method to manually set “this” in the slice method to the arguments object as if it was an array so that slice could perform normally thinking it is working with array
You have to understand the difference between arrays and arrays like objects to better understand this
I have edited your post.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.