Basic Algorithm Scripting - Slice and Splice

I don’t understand why the first two sets of code did not work

  **Your code so far**
function frankenSplice(arr1, arr2, n) {
/*let newarr=arr2;
newarr.splice(n, 0, arr1);
return newarr;*/
/*let newArr = arr2.slice();
newArr.splice(n, 0, arr1);
return newArr;*/
let newArr = arr2.slice();
newArr.splice(n, 0, ...arr1);
return newArr;

}

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

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Chrome version:
Version 103.0.5060.134 (Official Build) (64-bit)

Device:
Device name Tonys2022DellPrecision7760
Processor 11th Gen Intel(R) Core™ i9-11950H @ 2.60GHz 2.61 GHz
Installed RAM 64.0 GB (63.2 GB usable)
Device ID E8F61146-8933-4AEB-82BE-EDB4116034FC
Product ID 00355-60711-00677-AAOEM
System type 64-bit operating system, x64-based processor
Pen and touch No pen or touch input is available for this display

OS:
Edition Windows 11 Pro
Version 21H2
Installed on ‎5/‎20/‎2022
OS build 22000.795
Experience Windows Feature Experience Pack 1000.22000.795.0

Challenge: Basic Algorithm Scripting - Slice and Splice

Link to the challenge:

For the top one, saying newarr = arr2 is not making a copy of arr2 in newarr. Any change to newarr will effect arr2, and vice versa.

Your second attempt has fixed that problem of effecting the input with slice, but you are still not using splice properly. You are telling it to add arr1, which is an array full of elements, not each of the elements by themselves from arr1. You fix that in the last one by spreading arr1 in splice.

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