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

156
Views
JavaScript eliminando los ceros del frente de un número

He estado escribiendo un programa para contar la cantidad de ceros en un número.

Implementé lo siguiente:

 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)

El valor de todo el dígito cambia y el programa elimina automáticamente los ceros del frente.

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

¿Alguien puede sugerir qué es responsable de este comportamiento?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Si desea contar los ceros iniciales, puede usar un ciclo while y verificar el carácter en la posición actual.

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