Slice and Splice vs. Spread Operator

So, I was making this challenge and it just do not accept my solution as valid, because I am using the spread operator to solve that. I tested in the console with all cases that are in the challenge and it work just fine.

Probably, I will remake it because the idea of the challenge is use slice() and splice() methods. But, I think that the spread operator is a valid option too.


function frankenSplice(arr1, arr2, n) {
let newArray = [];
for (let i = 0; i < arr2.length; i++) {
  if (i === n) {
    newArray.push(...arr1)
  }
  newArray.push(arr2[i])
}
return newArray;
}

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

Challenge: Slice and Splice

Link to the challenge:

Your code has a problem with this test case:

console.log(frankenSplice([1, 2], [], 0));

which is part of the requirement

All elements from the first array should be added to the second array in their original order.

It is a tricky little case.

Edit: Typo Fix

2 Likes

Uhm… I got it, I got it.

But, that would be considered a valid entry to the function? Because the second array do not even have a position with index 1 to insert new info into that positon.

Ah, yeah, that 1 should be a 0.

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