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

155
Vistas
JavaScript removing the zeros from front of a number

I have been writing a program to count the number of zeros in a number.

I implemented the following:

let num = 00012340000, count = 0, digits;
digits = num.toString().split("");
console.log(digits)
for (let i = 0; i < digits.length; i++) {
  if (parseInt(digits[i]) == 0) {
    count++
  }
}
console.log(count)

The entire digit's value changes and the program automatically strip off the zeros from the front.

[
  '2', '7', '3',
  '6', '1', '2',
  '8'
]

Can anyone please suggest what is responsible for this behavior?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you want to count the leading zeros, you can use a while loop and check the character at the current position.

/**
 * Counts the number of leading zeros in a numeric string.
 * @param {string} num - number with zero-or-more leading zeros
 * @returns {number} the amount of leading zeros
 */
const countLeadingZeros = (num) => {
  let count = 0;
  while (count < num.length && num.charAt(count) === '0') { count++; }
  return count;
}

console.log(countLeadingZeros('00012340000')); // Pass-in a string value

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