# Basic Algorithm Scripting "Slice and Splice" error?

In this exercise, my code passes all the prompts except the final one, “The second array should remain the same after the function runs.”

This doesn’t make sense to me because I’ve ensured that by the end of the function the second array is in fact in the same state as it was originally. What am I missing here?

``````function frankenSplice(arr1, arr2, n) {
let arrStandIn = [];
let arr2Copy = arr2;
for (let x = 0; x < arr2.length; x++) {
arrStandIn.push(arr2[x]);
}
let bank = arr2Copy.slice(n);
for (let i = 0; i < arr1.length; i++) {
arr2Copy.splice(n+i);
arr2Copy.push(arr1[i]);
}
for (let j = 0; j < bank.length; j++){
arr2Copy.push(bank[j])
}
arr2 = arrStandIn;
return arr2Copy;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);
``````

All help is greatly appreciated, thank you

This is not a copy

Here you’ve changed arr2

I was under the impression that arr2 should be the same at the end of the function; is the criteria actually that it not be altered in any way, at any point?

Edit:

So, after reading you comment, I changed the code to this:

``````function frankenSplice(arr1, arr2, n) {
let arrStandIn = [];
let arr2Copy = [];
for (let x = 0; x < arr2.length; x++) {
arrStandIn.push(arr2[x]);
arr2Copy.push(arr2[x]);
}
let bank = arr2Copy.slice(n);
for (let i = 0; i < arr1.length; i++) {
arr2Copy.splice(n+i);
arr2Copy.push(arr1[i]);
}
for (let j = 0; j < bank.length; j++){
arr2Copy.push(bank[j])
}
arr2 = arrStandIn;
console.log(arr2);
return arr2Copy;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);
``````

And now it works! That said, while I now understand that

``````let arr2Copy = arr2;
``````

is not a copy, I don’t understand why it isn’t. Could you please explain?

Arrays are references to where the data is stored, so when you modify the array inside of the function, you actually change its contents outside of the function too. In this case, you just change the variable you are referencing but its the same array

I would avoid this trying to make a arr2copy alltogether.