Doubt on the banner

Asked

Viewed 26 times

0

This Banner is working properly, but I just wanted to understand this account: (bannerAtual + 1) % 3 where q: 'Banneratual = 0' then, (0 + 1) = 1 % 3 = 0,3333.... someone explains to me why to change 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" />

  • 1

    What exactly is your question? It was not clear in your question

  • And like, how is it working? Why is it changing value from 'Banneratual'' ?

1 answer

1


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

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 at this link.

  • Entedi, That summed up everything "If the first is smaller, it returns the first", did not know

  • 1

    @This Sorezemartins This. When trying to divide a smaller number by a larger one, the result is zero and the rest is the divisor itself (whereas the result of the division is an integer).

  • I entedi, I reviewed here the subject of mathematics, thank you very much for the explanation

  • 1

    Just one correction: I said "divisor", but it’s actually "dividend".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.