Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!