DEV Community

Cover image for Find number of digits in range of numbers
ncutixavier
ncutixavier

Posted on

Find number of digits in range of numbers

How many digits does it take to assign numbers to 283 employees using numbers from 1 through 283?

function findNumberOfDigits(n){
  let count = 0
  for(let i=1; i<=n; i++){
    // ((''+i)).length => count number of digits
    count += ((''+i)).length
  }
  return count
}
Enter fullscreen mode Exit fullscreen mode

This function returns the number of digits from 1 to n. n should be entered by the user.

For example: if n=12, it returns 15. From 1 to 12 (1,2,3,4,5,6,7,8,9,10,11,12), there is 15 digits.

@ncutixavier

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay