Basic Algorithm Scripting - Slice and Splice

Tell us what’s happening:
Describe your issue in detail here.
Why Slice()??
I was able to solve this, but was trying to wrap my head around one detail.
The first step is copying the original array - this step requires to use slice() without any arguments. What is the purpose of using slice in this assignment? Why not use something else like in this article → How to clone an array in JavaScript.

for example - a

const formattedArray = […arr2];
const newArray = arr2.slice();

  **Your code so far**
function frankenSplice(arr1, arr2, n) {
const formattedArray = arr2.slice();
for(let i=0; i<arr1.length; i++){
return formattedArray;

console.log(frankenSplice([1, 2, 3], [4, 5, 6], 1));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic Algorithm Scripting - Slice and Splice

Link to the challenge:

First of all, whenever you are not sure about something, google it. MDN is an excellent resource for JS, so google “mdn array slice”. Seriously, do it now. Really, it is one of the most important skills. Do it now.

The method slice returns a new array. And you aren’t doing any slicing to the original, so it is just a shallow copy of the old array. Yeah, it is a little “hacky”, or at least semantically misleading, but it is a common way to make a shallow copy or an array. It is not the only way.


There were examples with spread operator copying in the curriculum, that’s for sure. I guess they are just showing different approaches.
Btw solution 3 for this step is so cool, and it uses both slice and spread operator in single line of code.

1 Like

you are right I should pushed beyond the mdn/fcc/wc3 docs. I googled “slice vs spread operator performance” got this. Apparently it is more performant, but as you said semantically it is misleading. Anyhow thanks for the help, much appreciated!

Don’t worry about that too much, especially in web dev. 99% of a web apps time is spent just waiting. And almost all micro optimizations like this are saving micro seconds overall, when humans don’t even notice anything less than 0.1 seconds. Don’t worry about micro optimization until you have to. Make smart choices in terms of algorithms, but don’t worry about the pennies until you have to.

Really, the point here wasn’t the difference between splice and slice since the latter was kind of being used in an offbrand way, to do a shallow copy. slice can do a lot more than that.

1 Like

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