JavaScript (Help) Slice and Splice

I don’t understand why

arr2

is inserted in reverse. Can anyone explain without giving away the answer? Thank you.

Hello~!

Currently, your function is putting arr1 into arr2 at index n. If I am understanding your question correctly, you want it to put arr2 into arr1 instead?

I want to put arr1 into arr2 at index n, but arr1 is in reverse. 3, 2, 1 not 1, 2, 3. I hope I’m making sense lol sorry I barely started learning js.
So I’m expecting [4, 1, 2, 3,5]
Not [4, 3, 2, 1, 5]

Ah, I see~!

It’s because you splice each element in at index n. So it splices in 1, then 2, then 3, but because n doesn’t change, the return result is 3, 2, 1.

If I break it down step by step:
arr2 is [4, 5]. n is 1.
The first iteration of your loop takes 1 from arr1 and splices it into arr2 at index 1. arr2 is now [4, 1, 5].
The second iteration of your loop takes 2 from arr1 and splices it into arr2 at index 1. arr2 is now [4, 2, 1, 5].

Does that help you see what is going on?

I see two different possible methods to fix this. If you want a hint, let me know. :slight_smile:

Thanks for explaining that it really helped. I was able to solve it by incrementing n
after each number was sliced into the second array.