I wrote the following code:
function frankenSplice(arr1, arr2, n) {
let arr = arr2.slice();
arr.splice(0, 0, arr.splice(0, n), arr1);
return arr;
}
When I do the tests individually, I get the right results. Here they are:
console.log(frankenSplice([1, 2], [3, 4, 5, 6], 2)); //3,4,1,2,5,6
console.log(frankenSplice([1, 2, 3], [4, 5], 1)); //4,1,2,3,5
console.log(frankenSplice([1, 2], ["a", "b"], 1)); //a,1,2,b
console.log(frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2)); //head,shoulders,claw,tentacle,knees,toes
But when I press the “Run the Tests” button the result is wrong. The tests 1, 2, 3 and 4 are incorrect. Tests 5 and 6 are correct.
What’s wrong with my code. Please help!
Here’s another version of the code that works the same way as the first one. And no errors in the final test gives the same.
function frankenSplice(arr1, arr2, n) {
let arr = arr2.slice();
let arr2Del = arr.splice(0, n);
arr2Del.splice(n, 0, arr1, arr);
return arr2Del;
}