Docs Logo

  • GitHub
  • Twitter

Reverse an Int

Instructions:

  • Given an integer, return an integer that is the reverse ordering of numbers.

Examples:

  • reverseInt(15) === 51

  • reverseInt(981) === 189

  • reverseInt(500) === 5

  • reverseInt(-15) === -51

  • reverseInt(-90) === -9

Repl.it:

  • reverse_an_int

Code:

function reverseInt(int) {
const reversed = int
.toString()
.split('')
.reverse()
.join('');
// Math.sing returns
// 1 if (int) is positive
// -1 if is negative
// 0 if is 0
return parseInt(reversed) * Math.sign(int);
}