Basic Algorithm Scripting: Slice

[].splice.apply(arr3, [n, 0].concat(arr1));

pls help me to understand above the line. why .splice.

Are you talking about this challenge?

yes. i am talking about that.

To answer your question about why to use splice is because of this line in the FCC instructions

" Begin inserting elements at index n of the second array."

MDN docs explanation of splice:
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements [in place]

In terms of the code you posted, I am not really sure where arr3 came from. In the original function, you have three arguments (arr1,arr2,n). So I would just stick with those arguments.

I think reading through the MDN docs you will see how easy it is to insert that first array in the second. It will help to make a copy of arr2 before you use the splice method.

thanks. arr3 is the copy of arr2.

1 Like