Javascript "clockwork arithmetic" -


i'm trying remember called "clockwork arithmetic" in javascript i've read long time ago in tutorial slideshows/carousels. (term might wrong since can't find useful in google)

it shorthand of following code:

a = + 1; if (a > total) = 0; 

essentially incrementing until reached total, , when reached total reset 0. used create carousels scroll indefinitely scroll beginning (index 0).

does how write above 2 lines of code in 1 line using said clockwork arithmetic? think used "remainder" operator % don't remember else.

this called modular arithmetic, , used in clocks:

a familiar use of modular arithmetic in 12-hour clock, in day divided 2 12-hour periods. if time 7:00 now, 8 hours later 3:00. usual addition suggest later time should 7 + 8 = 15, not answer because clock time "wraps around" every 12 hours

in javascript, can modulo operations using % operator:

the % operator yields remainder of operands implied division; left operand dividend , right operand divisor.

some equivalent examples:

a = (a+1) % total; 
a = ++a % total; 
++a, %= total; 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -