Hi everyone,
I was hoping to get some assistance with this challenge.
You are given two arrays and an index.
Use the array methods slice and splice to 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.
My first solution was as follows:
function frankenSplice(arr1,arr2,n) {
arr2.splice(n, 0, arr1);
return arr2;
}
This gave me all the correct answers it seems but still failed the challenge? So I reread the challenge and saw that the alteration should not modify the original arrays. I figured maybe my splicing was editing the originals and tried a new solution by assigning each array argument as its own variable inside the function.
function frankenSplice(arr1, arr2, n) {
var arrchng1 = arr1;
var arrchng2 = arr2;
arrchng2.splice(n, 0, arrchng1);
return arrchng2;
}
This also did not work. When I console.log all of the tests I get the correct solution so I am confused as to why my code is failing. Could anybody point me in the right direction of what I am doing wrong?