[SOLVED] Slice and Splice Spread operator

EDIT apparently its because splice can’t be used to assign it modifies the string it is attached to. After changing it to remove the newArr3 it worked.

I can’t figure out how to delete a topic.


I’m working on the algorithm lesson “Slice and Splice” and couldn’t get my below code to work. Then I look at the solution and see I’m supposed to be looping through the items. I see how that works, but I don’t get why this code doesn’t work.

For all my research you “should” be able to insert an array into another array using the spread operator.

Your code so far


function frankenSplice(arr1, arr2, n) {
  let newArr1 = arr1.slice(0);
  let newArr2 = arr2.slice(0);
  let newArr3 = newArr2.splice(n, 0, ...newArr1);
  console.log(newArr3[0]);
  return newArr3;

}
frankenSplice([1, 2], ["a", "b"], 1)
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/69.0.3497.100 Safari/537.36.

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