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

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

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 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!