Why assigning Splice to a variable doesn’t work?
See commented line at bottom of code:
return arr2Mod
passes all tests, but return newArray
doesn’t. Why can’t I assign Splice
to a variable?
My code:
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
let arr1Mod = [...arr1];
let arr2Mod = [...arr2];
let newArray = [];
for (let i = 0; i < arr1.length; i++) {
let newArray = arr2Mod.splice(n, 0, arr1Mod[i]);
++n;
}
return newArray;
// return arr2Mod;
}
I’m not sure what the intended result of frankenSplice
is (it’s been a long time since I’ve done that challenge) but one problem I see immediately is that you’re reassigning newArray for every iteration through the loop. Perhaps you intended to .push
to newArray instead?
Thanks a lot for reverting @chuckadams
Humm, unsure if that is what is making it not work. Let me provide another example:
The below works:
// var fruits = ["Banana", "Orange", "Apple", "Mango"];
// console.log(fruits);
// function myFunction() {
// fruits.splice(1, 0, "Lemon", "Kiwi");
// return fruits;
// }
// myFunction();
However, when assigning Splice
to a variable, it doesn’t work. I’m trying to understand why here. 
// var fruits = ["Banana", "Orange", "Apple", "Mango"];
// console.log(fruits);
// function myFunction() {
// let fruta = fruits.splice(1, 0, "Lemon", "Kiwi");
// return fruta;
// }
// myFunction();
Because you’re returning two different things. In the first, you’re returning the array that had a chunk spliced out of it (fruits
), and in the second you’re returning the chunk that was spliced out of the array (fruta
).
1 Like
Oh, I should have read MDN’s examples before asking. 
Thanks a ton for the clarification @chuckadams