Slice and Splice (my arr.splice became an empty arr? )

Tell us what’s happening:

Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let copy = arr1.slice(0); // copy the first arr

  let copy2 = arr2.slice(0); // copy the second arr

   return copy2.splice(n,0,copy); // insert the copy of first arr to the copy of arr2. 

  
}

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

Your browser information:

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

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

If console.log (copy2) is [ 4, 5, 6 ]
but console.log(copy2.splice(n,0,copy)) is [].
What did I do wrong?

becasue of following line

This will place the whole array into the position n, rather its element.

NOTE: splice doesn’t return anything, instead it affect current array instance. you should call splice over an array instance, and then return the instance. e.g.:

copy2.splice(n,0,copy);
return copy2

You need to use a for look(or similar like map, foreach, etc…) over all elements of arr1(copy), and insert each element at correct position.

TIP: in each iteration(loop), the position of insert should be added by 1.

Feel free if you need more help, keep goin on great work. happy programming.

Thank you !!

But I have other question now.

function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let copy = arr1.slice(0); // copy the first arr

  let copy2 = arr2.slice(0); // copy the second arr
    
    for (let i=0 ; i<copy.length; i++){
          copy2.splice(n,0, copy[i]);
          
    }
    // insert the copy of first arr to the copy of arr2. 
     return copy2;
  
}

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

The array insert properly now, but why the result is reversed???

This is my result [ 4, 3, 2, 1, 5, 6 ] instead of [4,1,2,3,5,6,]

I don’t understand this. My for loop started from i = 0, why it became [3,2,1] as the result ?

The problem is here:

I repeat my tip again

More tip: when you insert a value at 1, and next value at 1 again, so previous index 1 will be shifted to 2.

I believe you may fix this issue very easy now.

Keep goin on great work, happy programming

Thank you!! I got your hint now.
I solved the problem.
What a trick!

 copy2.splice(n++,0, copy1[i]); will give you [4,1,2,3,5,6,]