Error in Basic Algorithm Scripting: Slice and Splice

Tell us what’s happening:
This code return right result why freeCodeCamp don’t accept it?

Your code so far

function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  var newArr=arr2.slice();
  return newArr;

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

Your browser information:

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

Link to the challenge:

You’re splicing a copy of array1 into array2. As in you’re just putting the actual array into newArr, not the values in the array.

It’s going to come out like [4, [1, 2, 3], 5]

1 Like

Here are some pointers when I debugged your program:

  • At the statement var newArr = arr2.slice() you are making a new Array which contains the whole value of arr2.

  • Secondly, at the statement newArr.splice(n,0,arr1) you are just inserting the value of arr1 i.e. [1,2,3] to newArr i.e. [4,5] which makes the newArr equal to [4,1,2,3,5]

  • which means you are just copying the copy of one array into another(not the actual array) while the challenge states to copy the value of actual array not the copy of the array.

Hope it helps.