Basic Algorithm Scripting - Slice and Splice

Tell us what’s happening:
Hi guys! Why can’t we use the splice() method directly on arr2.slice() ? Does this has something to do with referencing?

Thanks!

Your code so far

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0

Challenge: Basic Algorithm Scripting - Slice and Splice

Link to the challenge:

Hi,

Let’s take a look into return value of splice:

Unlike slice, splice is a mutating method that makes changes to the original array (in your case, the new array that is returned from slice) and then if items are deleted, returns those deleted items. The array that is worked on (along with the changes made to it) is not stored to arr3. Is this your goal?

If you would like to return the modified array, consider using toSpliced() instead:

Hi, thanks for the hint. I have missed that splice returns the deleted items. Ill take a look into the toSpliced method.