Slice and splice spread operator or flat()

Tell us what’s happening:
Describe your issue in detail here.

Your code so far


function frankenSplice(arr1, arr2, n) {

let cpyArr2 = arr2.slice(0);

cpyArr2.splice(n,0,arr1);

return cpyArr2.flat();

}

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


Is there any reason to use spread operator over using  return cpyArr2.flat() to concatentate to one array?


**Your browser information:**

User Agent is: <code>Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36</code>

**Challenge:**  Slice and Splice

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

I added spoiler tags since your code works.

As to your question, generally you only want to use flat if you are flattening out your array. The spread operator is the typical way you execute a shallow copy of an array. It’s best to use built in functions as they are designed to help your code make sense to other developers.

1 Like

Many thanks JeremyLT :grinning:

1 Like

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