Understanding the modulus operator
I have some code that loops through a collection of list elements and a
collection of colours. It makes sure each list element is designated to a
colour.
I understand everything about this apart from the modulus operator. I get
that it finds and uses the remaining number, but I cannot for the life of
me understand what it is doing here?
var li = document.getElementsByTagName('li');
var colors = ["salmon", "teal", "orange", "grey", "blue"];
var colorsCount = colors.length;
for ( var i = 0; i < li.length; i++ ) {
li[i].style.backgroundColor = colors[ i % colorsCount ]; // why does
this work?
}
No comments:
Post a Comment