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.
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.
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 ?