Recursion with slice and splice

Tell us what’s happening:

  **Your code so far**

function frankenSplice(arr1, arr2, n) {

let arr3=arr2.slice();
    return arr1;
    return arr3;

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

Anyone knows whats wrong with this recursion? It seems that it only adds the first to the second, but the second part of the second is cut off, like it was popped, without I even realizing how it came to be.

  **Your browser information:**

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

Challenge: Slice and Splice

Link to the challenge:

This isn’t recursive, that means that the function may [repeatedly] call itself.

The task is:

Copy each element of the first array into the second array, in order.

Begin inserting elements at index n of the second array.

So the first line is fine, you make a copy of the second array and that’s what you’ll be adding to.

Then you say if n is 0, you just return the first array for some reason, instead of copying the elements in first array into the copy of the second array.

Then the first two arguments to splice are fine, but then you’re picking a specific item from the first array to splice in, rather than adding all of the elements in the first array

1 Like

you may find the spread operator useful here. Splice takes multiple arguments, and you effectively turn an array into a list of arguments by doing ...[1,2,3] that will create 1,2,3 no array attached, but you can also spread elements into an array [...[1,2,3]] is the same as [1,2,3]

1 Like

I tested it and putting the spread operator in the right place will make your function pass

1 Like

It still won’t be recursive though.

1 Like