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
// Colors. These are the color names you can use in CSS. You could use color codes
// like #rrggbb as well.
var colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
var colorIndex = 0;
setInterval(function(){
// Set the color and increment the index.
document.body.style.backgroundColor = colors[colorIndex++];
// Wrap the index if it goes past the length of the array. % is modulo.
colorIndex %= colors.length;
}, 1000);
})();
What does the line colorIndex %= colors.length;
do and how does it work?