Slice and Splice - Spread Operator?

Tell us what’s happening:
Hi guys! So as you can see, I thought that perhaps I could use the spread operator as the last argument in splice, but it doesn’t appear to have worked. I think the slice stage seems to be working from a console.log I did but I can’t seem to figure out why splice isn’t working. Console.logging the splice instead of returning it just shows that I’ve created an empty array :confused:

Any pointers would be much appreciated!

Your code so far


function frankenSplice(arr1, arr2, n) {
var why = arr2.slice();
return why.splice(n, 0, ...arr1);

}

frankenSplice([1, 2, 3], [4, 5], 1);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice/

Return value

An array containing the deleted elements

from MDN - Splice

It’s working, you just have to return the array after the splice, not return the value returned by splice itself :slight_smile:

1 Like

Ahhh. Thank you for that. I’ve been trying to figure this out for over an hour :sweat_smile:

1 Like