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

103
Visualizações
Only show first digit

I am making an HTML based score board for a sports event. The number (0-9) will be showed on an iPad running this code:

<script type="text/javascript">
var clicks = 0;
function helloup() {
    clicks += 1;
    document.getElementById("clicks").innerHTML = clicks;
    };
function hellodown() {
    clicks -= 1;
    document.getElementById("clicks").innerHTML = clicks;
    };
</script>

<em onClick="hellodown()">Blue</em>
<strong id="clicks">0</strong>
<i onClick="helloup()">Score!</i>

This works great - expect I only want it always only to show 1 digit. Meaning when it reaches 10 it should so 0.

A friend of mine mentioned the use of modulus or remainder, but I can't figure out what is what and how it would go into the code.

Any help greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are multiple possible solutions:

1. Using the modulus operator

var clicks = 0;
function helloup() {
    clicks = (clicks + 1) % 10;
    document.getElementById("clicks").innerHTML = clicks;
};

function hellodown() {
    clicks = (clicks - 1) % 10;
    document.getElementById("clicks").innerHTML = clicks;
};

Note:
hellodown will still result in -1,...,-9.

2. Checking he value of clicks

var clicks = 0;
function helloup() {
    clicks += 1;
    if(clicks >= 10) {
        clicks = 1;
    }
    document.getElementById("clicks").innerHTML = clicks;
};

function hellodown() {
    clicks -= 1;
    if(clicks <= 0) {
        clicks = 9;
    }
    document.getElementById("clicks").innerHTML = clicks;
};
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