var myArr = [0,1,2,3,4,5,6];
Problem:
myArr.splice(2,1); \\ [0, 1, 3, 4, 5, 6];
now 3 movements in the second position, and the length decreases by 1, which creates a problem.
Solution: A simple solution will iterate backwards when spliced.
var i = myArr.length; while (i--) { console.log(myArr); myArr.splice(i,1); }
Conclusion:
[0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4] [0, 1, 2, 3] [0, 1, 2] [0, 1] [0]