Basic Algorithm Scripting - Slice and Splice

Tell us what’s happening:
I don’t really have a problem on this topic but I just want to ask if this code is fine? It works but I’m not satisfied, I don’t know because the solution is different from mine haha lol.

Your code so far

function frankenSplice(arr1, arr2, n) {
  let newArr = [...arr2]
  newArr.splice(n,0,...[...arr1]);
  return newArr;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 OPR/91.0.4516.95

Challenge: Basic Algorithm Scripting - Slice and Splice

Link to the challenge:

...[...arr1]

What do you think this is doing?

You realize that arr1 is already an array?

Hello, sir, thank you for replying. On that part, without the first spread operator, it would be a nested array like this [ 4, [1,2,3], 5, 6 ]. So I think I should really put that operator. But then I think it was not a best practice or maybe it’s acceptable.

Without those spread operator it would look like this [ 4, [[1,2,3]], 5, 6 ]

...arr1 spreads the array.

1 Like

Ohhh, that’s why. It works and look fine with ...arr1 only. I got the wrong idea of spreading the value. Thank you!

Your code worked, but it was unnecessarily creating another array from arr1 and then spreading it.

Yeah, I was just confused that I also put a bracket on an array. But to think that my code works without loops works fine. Thank you!