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

93
Views
JS 4 digit array counter

I'm trying to make a 4 digit counter (e.g. 0000, 0001, ... 0152, 0153, ...) in JS and I have created the following logic, however, I have a bug when the numbers reach something like 0099. Is there a better way of doing this?

    c = [0,0,0,0]
    c = c.reverse();

    c[0]++;

    for (let i = 0; i < c.length-1; i++) {
        if (c[i] >= 9) {
            if(i > 0 && c[i-1] != 9){
            } else {
                c[i] = 0;
                if (c[i+1] == 9) {
                    c[i+1] = 0;
                    c[i+2]++;
                } else {
                    c[i+1]++;
                }
            }
        }
    }

    c = c.reverse().join("");
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Why not just count up an int and then convert it to 4-digits with

myInt.toLocaleString("en", {minimumIntegerDigits: 4, useGrouping: false})
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!