0
    function frankenSplice(arr1, arr2, n) {
       let juice = arr2;
       for(var i = 0; i < arr1.length; i++){
         juice.splice(n + i, 0, arr1[i]);
       }
       console.log(arr2); // [ 4, 1, 2, 3, 5, 6 ] Unwanted changes
       console.log(juice); // [ 4, 1, 2, 3, 5, 6 ] Expected value
       return juice;
      }

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

How does the arr2 value inside the function changes even though i used it only to submit its value into a variable

1 Answers1

0

You've just discovered references. Assigning one array to another doesn't copy the elements inside. Use

let juice = arr2.slice();
Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25