Basic Algorithm Scripting - Slice and Splice

Hello all,

I am working through the Splice and Slice problem and I wonder if someone could explain this. It feels like a quirk of some kind. In particular, I refer to the line: arr3.splice(n, 0, …arr1);

Why does the contents of arr2 change after this line?

Code so far:

function frankenSplice(arr1, arr2, n) {
  console.log("arr1 = " + arr1);
  console.log("arr2 = " + arr2);
  let arr3 = arr2;

/*splice arr1 into arr3 */
  console.log("arr2 before = " + arr2);
  arr3.splice(n, 0, ...arr1); /*<------------???*/
  console.log("arr2 after = " + arr2);

  console.log("-->" + arr3);
  return arr3;

The console outputs:
arr2 before = 4,5
arr2 after = 4,1,2,3

Challenge Information:

Basic Algorithm Scripting - Slice and Splice

let arr3 = arr2;

This line doesn’t create new array, but just creates another name (arr3), that’s pointing to the same array as arr2.

It might appear that changing one changes also the another, however that’s the single array, with two variables pointing to it.

Thank you very much for the reply! I’ll look into this more and keep it in mind in future.

Welcome to the community and thank you for posting both the code and the problem.

Good that you figured it out on our own.

Could you please remove the completed answer?

The forum is attempting to reduce the number of spoilers in order to help people learn.

Keep up your good progress!

I understand - solution removed. Thank you.

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