Intermediate Algorithm Scripting: Seek and Destroy - Why is it working?

Tell us what’s happening:

Hello everyone,

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.

Challenge: Seek and Destroy

Link to the challenge:

both should give you an error, this is not valid syntax

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.

1 Like

hi @luhah001,

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

1 Like

Thanks for your fast replies. But there’s still a question left.
If I consle.log both args.

var args = Array.from(arguments).slice(1); => [ 2, 3 ]

var args = Array.prototype.slice.call(arguments); =>
[ [ 1, 2, 3, 1, 2, 3 ], 2, 3 ]

But why is the filter method working on the second args? As far as I understand filter it shouldnt work for these cases or what am I missing?

@ieahleen changed the code, thanks for reporting

return !args.includes(item)

Here on this line, it’s checking if the element is present in the arguments object and returning true only if it’s not present

Had a thinking mistake here… Thanks for helping everybody

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.

Thank you