Code Lab

Reverse a string or a number

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

Examples

reverseValue('hello')

Returns 'olleh'

reverseValue(1200)

Returns 21

reverseValue(-45)

Returns -54

Asked at Amazon · Source

Loading the editor...
  1. InputreverseValue('hello')
    Expected'olleh'
  2. InputreverseValue(12345)
    Expected54321
  3. InputreverseValue(-45)
    Expected-54
  4. InputreverseValue(1200)
    Expected21
  5. InputreverseValue('')
    Expected''

Hidden tests run on Submit.