Doubts on the banner

0

This Banner is working correctly, but only wanted to understand this account: (bannerAtual + 1) % 3 being q: 'BannerAtual = 0' So, (0 + 1) = 1% 3 = 0,3333 .... does anyone explain to me why to be trading for value = 1 that would be the next array?

var banners = ["imagem 1.jpg","imagem 2.jpg", "imagem 3.jpg"]
var bannerAtual = 0

function trocarBanner(){
	bannerAtual = (bannerAtual + 1) % 3
	document.querySelector('.destaque_img').src = banners[bannerAtual]
}

setInterval(trocarBanner, 2000)
<img class="destaque_img" />
    
asked by anonymous 05.09.2017 / 00:29

1 answer

1

The % operator is not division. It returns the "remainder" of the division of the first number by the second, that when the first is equal to or greater than the second. If the first one is smaller, it returns the first one:

1%3 = 1 // 1 (primeiro número, à esquerda de %) é menor que 3

3%3 = 0 // resto 0 (3 dividido por 3 = 1, resta 0)

5%2 = 1 // resto 1 (5 dividido por 2 = 2, resta 1)

References in this link.

    
05.09.2017 / 00:39