Basic Algorithm Scripting: Slice and Splice

You are given two arrays and an index.

Copy each element of the first array into the second array, in order.

Begin inserting elements at index n of the second array.

Return the resulting array. The input arrays should remain the same after the function runs.

function frankenSplice(arr1, arr2, n) {
let arr = arr2.slice();
arr.splice(n,0,...arr1);
return arr;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);

Written by

Don’t follow me. I wrote junks here. Follow me on Twitter instead.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store