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

131
Visualizações
Decrement number by clicking button until reaching 0

I'm working on a project and am a bit stuck. I am sure I am missing something but have not what exactly I'm missing.

I extracted the general concept of what I'm doing and threw it onto a JS fiddle.

So basically, I am wanting to decrement the displayed number (200) by a randomly generated number between a specified number range every time the button is clicked (using vanilla JS). Got it to work a bit. When the button is clicked, it does decrement.

However I am wanting to continue to decrement it until the displayed number is 0. The issue I'm running into however is, every time the button is clicked the displayed number updates but still subtracting from 200, not from the updated value.

For example, I can click once and the number will jump from 200 down to 189. When I click again, the number will actually jump up to say 195, meaning it's subtracting from 200 rather than subtracting the new generated amount from 189.

Jsfiddle: https://jsfiddle.net/miguerurso/0wmtgd67/5/

html:

<head></head>
<body>
<div id="number" style="font-size: 2em;"></div>
<button>decrement</button>
</body>

JS:

const numberCont = document.getElementById('number');
const button = document.querySelector('button');

let x = 200;

numberCont.innerHTML = x;

button.addEventListener("click", function(){
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min)
      }
      const randomNumResult = randomNum(7, 12)

     let updatedX = x - randomNumResult;

      numberCont.innerHTML = updatedX;
})

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

0

You need assign the updateX To x as well. Currently you are only giving it to the element. Try this.

const numberCont = document.getElementById('number');
const button = document.querySelector('button');

let x = 200;

numberCont.innerHTML = x;

button.addEventListener("click", function(){
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min)
      }
      const randomNumResult = randomNum(7, 12)

      x = Math.floor(0, x - randomNumResult);

      numberCont.innerHTML = x;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You're not actually changing the value of x to the new subtracted value. You'll have to do this to keep subtracting from the variable. I rewrote your event listener to change the value of x without going below 0.

button.addEventListener("click", function(){
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min)
    }

    x = Math.max(x - randomNum(7, 12), 0);

    numberCont.innerHTML = x;
})
about 4 years ago · Juan Pablo Isaza Relatório

0

Just change one line of code:

let updatedX = x - randomNumResult;
numberCont.innerHTML = updatedX;

with:

x = x - randomNumResult;
numberCont.innerHTML = x;
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