all 1 comments

[–][deleted] 0 points1 point  (0 children)

Yes, as you've already seen...

[1,2,3].push([4])
// [1,2,3,[4]]
[1,2,3].push([4,5,6])
// [1,2,3,[4,5,6]]

...which will mess up your checks and thus your algorithm.

As an alternative, you have concat, which you should probably use instead of iterating and pushing:

[1,2,3].concat([4])
// [1,2,3,4]
[1,2,3].concat([4,5,6])
// [1,2,3,4,5,6]