DEV Community

loading...
Cover image for Find number of digits in range of numbers

Find number of digits in range of numbers

ncutixavier profile image ncutixavier ・1 min read

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

Discussion (0)

pic
Editor guide