Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

210
Visualizações
What does the %-sign in a function

I'm an absolute js novice and I'm self-tought which doesn't make it better. I have the following function and want to understand what exactly the '% 5' does in it.

    $(function() {
      $(".carad" + new Date().getTime() % 5).css("display", "block");
    });
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The % symbol means the "modulo" operator in JavaScript. This operator returns the remainder after division is applied. So here it would return the remainder of whatever Date().getTime() returned. For example, if the function returned a value of 23, the % would take the remainder of 23/5, which is 3.

about 4 years ago · Juan Pablo Isaza Relatório

0

% sign simply returns the remainder for example 11%5 will return 1 as the answer.Hope you got your answer

about 4 years ago · Juan Pablo Isaza Relatório

0

% in the programming world means mod which is the remainder of a division. for instance, 11 divided by 5 equals 2 and the remainder is 1. Here are some resources that teach mod in-depth:

MDN Docs

Geekbox

Better Explained

Stackoverflow

That is being said in the code you've provided you are creating a new date object and extracting the time out of it. So basically this code is trying to select a carad class with the remainder of the date. I have attached a code snippet that will help you understand the idea more.

$(function() {
   $(".carad" + new Date().getTime() % 5).css("color", "red");
   $(".display-mod").text("the remainder is " + new Date().getTime() % 5);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 class="carad0">Hello world 0</h1>
<h1 class="carad1">Hello world 1</h1>
<h1 class="carad2">Hello world 2</h1>
<h1 class="carad3">Hello world 3</h1>
<h1 class="carad4">Hello world 4</h1>
<h1 class="carad5">Hello world 5</h1>
<h1 class="carad6">Hello world 6</h1>

<p class="display-mod"></p>

In short, this code selects a class with a random number.

Note: I've changed the applied styles to emphasize the idea

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda