Here are the instructions:
You are given two arrays and an index. Copy each element of the first array into the second array, in order. Begin inserting elements at index n of the second array. Return the resulting array. The input arrays should remain the same after the function runs.
I tried the following and it did not work:
function frankenSplice(arr1, arr2, n) {
let newArr = arr2.slice(0);
return newArr.splice(n, 0, ...arr1);
}
My return
returned an empty array. But this change in the code does work:
newArr.splice(n, 0, ...arr1);
return newArr;
How come returning the result of the splice method does not work? Why do you have to first splice and then return the result on the net line of code? Is it because of the spread operator?