Tell us what’s happening:
My code returns the correct input for every test case (verified via console). I ensured that the 2 original input arrays remain unchanged. Yet it is still failing the test, specifically on all elements from the first array should be added to the second array in their original order - which it does btw. Any thoughts?
Your code so far
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
//copy arr2 to arr3 to leave arr2 in tact
var arr3 = arr2.slice();
// Take a copy of arr1 (with slice) and insert into arr3 (with splice) at the supplied index, n
arr3.splice(n,0,arr1.slice());
console.log(arr3);
//return result array, arr3
return arr3;
}
frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.
I’m not entirely sure why yours is failing the tests. I converted your splice into a loop, and it passes.
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
//copy arr2 to arr3 to leave arr2 in tact
var arr3 = arr2.slice();
// Take a copy of arr1 (with slice) and insert into arr3 (with splice) at the supplied index, n
for (let i = 0; i < arr1.length; i++) {
arr3.splice(n,0,arr1[i]);
n++;
}
console.log(arr3);
//return result array, arr3
return arr3;
}
frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2);
Here arr1.slice() is an array, it is not passing because you are returning an array with a subarray. Try checking arr3 with the browser console. Or using:
You’re correct. Since splice takes multiple items I didn’t realize passing them in as a sub-array would make a difference. And I didn’t notice console.log(arr3) “looks” like it produces the correct result. Thank You @leahleen