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

94
Vistas
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 Respuestas
Responde la pregunta

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