Splice loop behaviour

Dear FreeCodeCampers,

I started this excercise: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice/

but the splice logs not the same order as the first given array. I do not know how to recognize the reason. https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Obiekty/Array/splice
I did not find any helpful information here.

Thank you!

Hi @bartekbart,

It is happening because of this code: arr2.splice(n,0,[arr1[i]]);
Since n is constant throughout the loop (n=1 in this case), new element will be added to the arr2 at index=1 for each iteration

Yes! N referers to the same place every time.
Thank you!