Code Lab

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

Reverse a string or a number

MédiaStrings10 min

Write a function reverseValue(value) that reverses a string or a whole number.

  • value is a string or an integer from -1000000000 to 1000000000.
  • For a string, return the string with its characters in reverse order: 'hello' gives 'olleh'.
  • For a number, return a number with its digits in reverse order. The minus sign stays in front: -45 gives -54. Zeros at the start of the result drop: 1200 gives 21, and 0 gives 0.
  • The result has the same type as value: a number gives a number, not a string. A string of digits is still a string: '120' gives '021'.
  • The empty string gives ''.

Exemplos

reverseValue('hello')

Retorna 'olleh'

reverseValue(1200)

Retorna 21

reverseValue(-45)

Retorna -54

Cobrada em Amazon · Fonte

Carregando o editor...
  1. EntradareverseValue('hello')
    Esperado'olleh'
  2. EntradareverseValue(12345)
    Esperado54321
  3. EntradareverseValue(-45)
    Esperado-54
  4. EntradareverseValue(1200)
    Esperado21
  5. EntradareverseValue('')
    Esperado''

Os testes ocultos rodam no envio