Code Lab

Mode entretien. Minuteur de 5 min, sans indices ni autocomplétion

Count the digits

FacileBasics5 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'.

Exemples

countDigits(12345)

Renvoie 5

countDigits(-305)

Renvoie 3

countDigits(0)

Renvoie 1

Source

Chargement de l'éditeur...
  1. EntréecountDigits(12345)
    Attendu5
  2. EntréecountDigits(-305)
    Attendu3
  3. EntréecountDigits(0)
    Attendu1

Les tests cachés se lancent à la soumission