Slice and Splice what is wrong with my solution?

Tell us what’s happening:
Hello, I’m trying to solve this Algorithm
As I understate the main problem is with this point

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

But then I did not understand in what kind of order my solution added first arr1 to the secondary arr2?

Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  

  let newArr = arr2.map(val=> val)
  newArr.splice(n, 0, arr1)

  console.log(newArr)
  return newArr
}

frankenSplice([1, 2, 3], [4, 5 ], 1);
frankenSplice([1, 2], ["a", "b"], 1)
frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2)

Your browser information:

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

Link to the challenge:

This bugged me too. I think I solved it something like you at first. IIRC, what you get is really
["claw", "tentacle", ["head", "shoulders", "knees", "toes"]].

You have to slice the second array into the new array.

1 Like

I suggest using the following to see what you are getting

console.log(JSON.stringify(...))
1 Like