Skip to content

Instantly share code, notes, and snippets.

@dainiuxt
Created April 29, 2020 13:27
Show Gist options
  • Save dainiuxt/dc5aed83fb8df0f732395f6ddbfaa89e to your computer and use it in GitHub Desktop.
Save dainiuxt/dc5aed83fb8df0f732395f6ddbfaa89e to your computer and use it in GitHub Desktop.
/* Free c code camp task
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice
Are there any flaws in my solution (code passes test as OK)?
You are given two arrays and an index.
Use the array methods slice and splice to 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) {
var firstArr = arr2.slice(0, n);
var thirdArr = arr2.slice(n);
var inter = firstArr.concat(arr1);
var result = inter.concat(thirdArr);
return result;
}
frankenSplice([1, 2, 3], [4, 5], 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment