# Basic Algorithm Scripting: Slice and Splice - broken checker

this passes:

``````function frankenSplice(arr1, arr2, n) {
var arr1Copy = arr1.slice();
var arr2Copy = arr2.slice();

for (var i = 0; i < arr1.length; i++) {
arr2Copy.splice(n+i, 0, arr1Copy[i]);
}
return arr2Copy;
}
``````

this doesn’t pass:

``````function frankenSplice(arr1, arr2, n) {
let ret = arr2.slice(0,n);
for( let i in arr1)
ret.push(arr1[i]);
ret.push(arr2.slice(n,arr2.length));
return ret;
}
``````

the first is harder to red and also makes a copy on top of the already existing copy passed as an argument.
That’s fairly counter intuitive.
I feel the second solution should pass as well.

I’m testing with frankenSplice([1, 2, 3], [4, 5, 6], 1);

In the first example, look at the arr2Copy.length and notice it’s 6. Then check out the ret.length in the second example and notice that it’s 5.

Also if you look at the last record in arr2Copy[5] is 6. Then you look at the last record in ret[4] and it’s 5,6.

In the second example, I think for some reason it’s adding 2 numbers to the last record? That’s why it appears to be working if you console.log the whole array (4,1,2,3,5,6), but it’s actually saving it as 4, then 1, then 2, then 3, then 5,6.

EDIT: You’re right, it doesn’t really need to copy the first array. You can cut that line out & it still passes the tests:

``````function frankenSplice(arr1, arr2, n) {
var arr2Copy = arr2.slice();

for (var i = 0; i < arr1.length; i++) {
arr2Copy.splice(n+i, 0, arr1[i]);
}

return arr2Copy;
}
``````

my code has a bug.

All you have to do is use the spread operator ( …)

it basically destructures an array.

that should fix it. If that still doesn’t work, then the checker is broken