Skip to content
Advertisement

Use $I++ more than once in a loop

I need to use the value for $i++ in numerous places but if I do that then some fields end up skipping values and instead of being 1, 2, 3 etc. they are 1, 3, 5 and the other field has values of 2, 4, 6 etc.

JavaScript

It works if I create another variable but this feels like a hack.

JavaScript

Is there a better way?

Advertisement

Answer

You can do the increment as a discrete step at the end of the loop, this means you can use it in various places without wondering when you changed the value…

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement