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.
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.
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.
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.
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.