Re:Elevator Saga – An elevator programming game
by garaetjjte on Saturday June 12, 2021 @07:16PM
(#27488251)
I think this was discussed there: https://news.ycombinator.com/item?id=8933979
[ Parent
| on HN ]
Re:Elevator Saga – An elevator programming game
by mercora on Saturday June 12, 2021 @07:22PM
(#27488293)
this did indeed help understanding and fixing the issue.
Re:Elevator Saga – An elevator programming game
by davnicwil on Saturday June 12, 2021 @07:51PM
(#27488460)
Just in case this is interesting, a potentially more elegant way to solve this with modern JS is to use 'for(let i' instead of 'for(var i'.
The reason is that 'let' gives 'i' 'block scope', meaning that each iteration of the loop block gets its own scope so that 'i' isn't overwritten each time, and each function you declare captures the value of 'i' at the time it was declared.
In other words - the way you originally expected 'var' to behave is how 'let' behaves!