# Basic Algorithm Scripting - Slice and Splice

Tell us what’s happening:
So, my chief complaint is that I copy the original array, and return it to it’s original state, but it’s failing the test that the second array is not the same as it was before the function was called, which doesn’t seem entirely fair, since it contains the same elements after the function as it did before (which I confirm with my console.log()s). If you can’t alter the second array at all, that should be more explicitly spelled out, otherwise my code should be accepted as passing.

``````function frankenSplice(arr1, arr2, n) {
let newArr = [];
let arr2Cpy = arr2.map(a=>a);
arr2.splice(n,0,...arr1);
newArr = arr2;
arr2 = arr2Cpy;
console.log(arr2);
console.log(newArr);
return newArr;
}

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

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/110.0`

Challenge: Basic Algorithm Scripting - Slice and Splice

Fair or not, that’s the requirement for the function. This is the last line in the instructions:

“The input arrays should remain the same after the function runs.”

And this is actually usually a good thing and it is considered a best practice not to modify arrays/objects passed into a function since they are passed by reference and thus whatever changes you make to them in the function will also exist outside of the function.

So you’ll need to come up with a solution that doesn’t modify `arr2`.

1 Like

You don’t actually return the `arr2` to its original state though:

``````function frankenSplice(arr1, arr2, n) {
let newArr = [];
let arr2Cpy = arr2.map(a=>a);
arr2.splice(n,0,...arr1);
newArr = arr2;
arr2 = arr2Cpy;
console.log(arr2);
console.log(newArr);
return newArr;
}

const firstArray = [1, 2, 3];
const secondArray = [4, 5, 6];
frankenSplice(firstArray, secondArray, 1);
console.log(secondArray);
``````
1 Like

it’s not the exact same array as before, but it has the same contents as before.

A simple solution exists to use slice instead of splice that doesn’t alter any of the arrays, but I feel that the splice solution is more readable, and thus more maintainable in an actual production environment.

Did you run the code I gave you? You change the contents of `arr2` outside of your function call.

It is not more maintainable if your function has side effects.

You can still use `splice` without altering the input arrays. The solution I am staring at uses `splice`. Your solution is almost there. You just shouldn’t be doing `splice` on `arr2`.

I could literally just change the splice from the arr2 to my arr2Cpy and that would resolve my problem… It was staring me in the face.

the slice solution I used before that was the […arr2.slice(0,n), …arr1, …arr2.slice(n,arr2.length)] and that yielded the same results, but it feels less straightforward/obvious than the splice method for some reason.

true. I did not run the code before, but I did just now. I found a few different resolutions to the problem.

Out of curiosity, is there a way to alter the pointer for the array in javascript?

You don’t have the same tools in JavaScript that you do in C. Primitive values are always pass by value. Arrays and objects are pass by reference. You can’t pass pointers to pointers.

1 Like
``````  newArr = arr2;

When you set this equality, you are simply creating a reference to arr2, rather than creating a separate array that is a copy of arr2.

To do this, you can use the spread operator within a bracket as follows:

newArr = [...arr2];

This way, you can make changes to newArr without affecting the original arr2.``````

Perhaps the curriculum and challenge should be updated to include the Array.prototype.toSpliced() method.

it makes the challenge so simple it’s crazy.

My solution:

> return(arr2.toSpliced(n, 0, …arr1))

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.