Why does not my code work

Tell us what’s happening:
Why am I getting an error “The second array should remain the same after the function runs.” I assigned the value of the array “arr2” to a new array and spliced the new array. Why would the splice change the array “arr2”?

  **Your code so far**

function frankenSplice(arr1, arr2, n) {
let newArr = arr2;
newArr.splice(n,0, ...arr1)
return newArr;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36

Challenge: Slice and Splice

Link to the challenge:

Hi, just assigning an array to another variable does not make a copy of the array. you can use spread operator to do that as you did it in splice

1 Like

Thank you for your reply. I understand it now.

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