Hi everyone. this is my first post ever so forgive me if there was any mistakes.
I think I understand the solution to the problem posted. there is one thing I can’t seem to get my head around. I tried the for loop method and it works just fine except that it splices the values of arr1 in backwards order. if I tried this :
function frankenSplice(arr1, arr2, n) {
let localArray = arr2.slice();
for (let i = 0; i < arr1.length; i++) {
localArray.splice(n, 0, arr1[i]);
}
return localArray;
};
the Answer will be :
[4, 3, 2, 1, 5, 6]
but if I added n++ as in the answer provided :
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
let localArray = arr2.slice();
for (let i = 0; i < arr1.length; i++) {
localArray.splice(n, 0, arr1[i]);
n++;
}
return localArray;
};
the answer will be :
[4, 1, 2, 3, 5, 6];
Can someone please explain to me why this is happening ?
Thank you in advance!!!
Challenge: Slice and Splice
Link to the challenge: