Code Lab

Count the digits

EasyBasics5 min

Write a function countDigits(n) that returns how many digits the integer n has.

  • n is an integer from -999999999999999 to 999999999999999, so it has at most 15 digits. It can be negative or zero.
  • The minus sign is not a digit: countDigits(-305) is 3.
  • 0 is written with one digit, so countDigits(0) is 1.
  • Return a number, for example 5, not the string '5'.

Examples

countDigits(12345)

Returns 5

countDigits(-305)

Returns 3

countDigits(0)

Returns 1

Source

Loading the editor...
  1. InputcountDigits(12345)
    Expected5
  2. InputcountDigits(-305)
    Expected3
  3. InputcountDigits(0)
    Expected1

Hidden tests run on Submit.