Basic Algorithm : Slice and Splice

Tell us what’s happening:
What is causing the different output for both ? I think, both should produce the same output.

Your code so far


**1**

function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let arrCopy = arr2.slice();
  arrCopy.splice(n, 0, ...arr1);
  return arrCopy;
}

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


**2**

function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let arrCopy = arr2.slice();
  let resultArr = arrCopy.splice(n, 0, ...arr1);
  return resultArr;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

splice returns the items which have been deleted from the array. So, in the second example, you’re returning the result of splicing at n. Check the documentation..

Example

frankenSplice2([1, 2, 3, 4], ["a", "b", "c", "d"], 1); // [1, 2]

Oh, Thanks for the point out. It should be :

return arrCopy;

in second example.

Here is my code:

function frankenSplice(arr1, arr2, n) {

let x = arr2.slice();
x.splice(n, 0, …arr1);
return x;

}