Code Lab

Modo entrevista. Cronômetro de 5 min, sem dicas e sem autocompletar

Count the digits

FácilBasics5 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'.

Exemplos

countDigits(12345)

Retorna 5

countDigits(-305)

Retorna 3

countDigits(0)

Retorna 1

Fonte

Carregando o editor...
  1. EntradacountDigits(12345)
    Esperado5
  2. EntradacountDigits(-305)
    Esperado3
  3. EntradacountDigits(0)
    Esperado1

Os testes ocultos rodam no envio