Skip to content Skip to sidebar Skip to footer

Having Confusion With Modulo Operator

I was trying to create a rainbow background using Javascript, and I did find some code that works. The code is: (function() { // Wrap in a function to not pollute the global scope

Solution 1:

Its essentially preventing colorIndex to exceed the colors's array length by setting it back to 0 and again continuing the same. So no matter how big the colorIndex's value get, it will always be less than the color's array length.

And about the syntax, it is just a shortform of

colorIndex = colorIndex % colors.length;

Post a Comment for "Having Confusion With Modulo Operator"