Destroyer + ...arguments + indexing problem

Hi there - After various attempts, I solved the exercise, but I have a question regarding arguments. I used splice to obtain all the arguments after the first one–the array.

let args = [...arguments];
let splicing = args.splice(1);// this works
let splicing = args.splice(0);// doesn't work. I expected to give me the first argument: arr. Instead it gives me ALL arguments.

Not sure why. Any suggestion, guidance is greatly appreaciated.
Any guidance is greatly appreciated!

function destroyer(arr) {
let args = [...arguments];
//let argsLength = args.length;
let splicing = args.splice(1);
return arr.filter(num => 

console.log(destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Seek and Destroy

Link to the challenge:

So arguments is “array-like”, and you’re turning it into a true array with [...arguments]. But that included all the arguments: arguments[0] is an array from which you’ll be removing stuff, so it’s a nested array, and arguments[1] to the end of the array (let’s call it arguments[N]) are the things we’ll be removing from that array.

Now, if you look at DevDocs, we see that splice is doing two things: it is deleting everything from the original array from the given index onward (so args is changing), and it returns everything it has deleted as a new array (which you’re assigning to splicing).

If we args.splice(0) we remove everything from that array (remember, we have that nested array at index 0), which isn’t what we want.

1 Like

Thank you so much as always.
Good point. I should’ve used slice instead. I certainly don’t want to modify the args.

Forgive me on this, but I’m not sure I totally understand why args.splice(0) doesn’t just gives me ‘arr,’ and instead seems to delete all arguments.
args[0] is the array
args[1] is everything after the array. Thus if I say:

let splicing = args.splice(0);

would’t splicing give me the array? Instead, it prints out all arguments again.

My apologies, there’s something i’m not seeing here. Just don’t what.

As always, thanks for your guidance!

1 Like

The argument you give splice is where to start from. So if you start at index 0, then naturally that’s the start of the array, so you get the whole thing. If you start at index 1 you get all the items from the second item onwards. Index 2 would be from the third item onwards and so on

And splice deletes, so with splice(0) you’re just saying “delete everything from index 0 until the end of the array and return what I’ve deleted as an array of values”

If you want a specific number of things to be deleted, then you need to provide the second argument – splice(0, 1) says “delete one item starting at index 0”, which is kinda what you want. Kinda, because you don’t want splice here, you may want slice as @snowmonkey says, which copies a “slice” of an array.

But you don’t even need that to get the first argument, it’s already defined as the parameter arr, so just use that: this particular challenge is trying to get you to extract the remaining arguments that aren’t defined as parameters (which you did by using splice(1), which will work fine in this case)


Thanks so much. I thought i had responded. Much appreciated!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.