I have a problem.
I need a Javascript function which increase (increment) variable value by 4, and when the variable value is 20, then set value of variable to 0 and again increment it by 4 and so on…
I think that I need for loop and if condition, but I don’t know how to implement this…
Example
the result must be:
x = 0; then x = 4, x = 8, x = 12, x = 16, x = 20, x = 0, x= 4 ….
Thank you
You can do this with a nested pair of loops:
This will be more efficient than using the mod (
%) operator.EDIT
From your comments, it sounds like you want to generate the sequence incrementally, rather than in a loop. Here’s a function that will return a function that will generate the next element of your sequence each time you call it:
You could then use it like this (among many ways):