Slice and Spice without Slice and Spice!

I think I was enjoying my Dub Reggae music too much when I answered this one :wink: It works anyway.

I thought Iā€™d post it out of interest and as a first post.


function frankenSplice(arr1, arr2, n) {
let newArr = [];

for(let i = 0; i < n; i++) {
  newArr.push(arr2[i]);
}

for(let i = 0; i < arr1.length; i++) {
  newArr.push(arr1[i]);
}

for(let i = n; i < arr2.length; i++) {
  newArr.push(arr2[i]);
}

return newArr;
}

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

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0

Challenge: Slice and Splice

Link to the challenge:

1 Like

Nice one!

I would reduce the whole second loop to a concat, though?

function frankenSplice(arr1, arr2, n) {
let newArr = [];

for(let i = 0; i < n; i++) {
  newArr.push(arr2[i]);
}

newArr = newArr.concat(arr1)

for(let i = n; i < arr2.length; i++) {
  newArr.push(arr2[i]);
}

return newArr;
}

frankenSplice([2, 3, 4], [1, 5, 6], 1);
//[1,2,3,4,5,6]
function frankenSplice(arr1, arr2, n) {

  return [...arr2.slice(0,n) ,...arr1,...arr2.slice(n)];

}

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