Slice and Splice -cant understand why this doesnt work?

Tell us what’s happening:

Your code so far


function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
let copyarr1=arr1
let copyarr2=arr2
for(let i=0; i<arr1.length; i++){

  copyarr2=copyarr2.splice(n,0,copyarr1[i])
  n++

}
console.log(arr2)
return copyarr2;
}

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

Challenge: Slice and Splice

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice

why do i have to delete the " copyarr2= " in order for this to work?

why do i have to delete the " copyarr2= " in order for this to work?

The return value of splice() is not what you’re thinking it is.

From MDN Docs, the return value is:

An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

They give an example:

var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
var removed = myFish.splice(2, 0, 'drum');

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"] 
// removed is [], no elements removed
1 Like