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

201
Views
Number shows up as NaN instead of the number in my variable

I need it to show up as "0" before you click, and for some reason, it shows up as NaN, help!

<html>
<head>
  <script>

  var money = 0;
  var clickingPower = 10; 
    
    function moneyClick() {
    var clicks = clicks + 1;
    
        document.getElementById("money").innerHTML = money + clickingPower * clicks;
    }
    
  </script>
</head>

  <h1>You have <span id="money"></span> dollars</h1>
  
  <button onclick="moneyClick()">Click to make money</button>
  
</body>

</html>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It this line

   var clicks = clicks + 1

Your variable clicks does not exist yet, that is why you get NaN

about 4 years ago · Juan Pablo Isaza Report

0

You're using clicks before defining it.

you can try this:

<html>
    <head>
    <script>

        var money = 0;
        var clickingPower = 10; 

        function moneyClick() {
            var clicks = (clicks || 0) + 1;

            document.getElementById("money").innerHTML = money + clickingPower * clicks;
        }

    </script>
    </head>

    <h1>You have <span id="money"></span> dollars</h1>

    <button onclick="moneyClick()">Click to make money</button>

    </body>
</html>
about 4 years ago · Juan Pablo Isaza Report

0

You have to initialize the variable clicks:

var clicks = 0;
function moneyClick() {
clicks = clicks + 1;

    document.getElementById("money").innerHTML = money + clickingPower * clicks;
}
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!