Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
I am trying to decrease a number in a variable in javascript

I have been using the thing in HTML to wright in javascript and I am trying to do the math of a = a - 1 to try to decrease and print a number when you press a button.

this is my code

<body>
<button id="button" type="button">day passed</button>  
<p id="demo"></p>
<script>
    var a = 37;
    var submitButton = document.getElementById("button"); 
submitButton.addEventListener("click", function() {
    var a = a - 1;
    document.getElementById("demo").innerHTML = a;
     })</script>


</body>

how do I fix this or just do it in general

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

<body>
<button id="button" type="button">day passed</button>  
<p id="demo"></p>
  <script>
    let a = 37;
    let submitButton = document.getElementById("button"); 
    submitButton.addEventListener("click", function() {
      a--;
      document.getElementById("demo").innerHTML = a;
    })
  </script>


</body>

this works fine.

try to work with let or const instead of var.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Just change var a = a - 1 to a = a - 1 or a -= 1

var a = 37;
var submitButton = document.getElementById("button");
submitButton.addEventListener("click", function() {
  a -= 1;
  document.getElementById("demo").innerHTML = a;
})
<button id="button" type="button">day passed</button>  
<p id="demo"></p>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda