Skip to content

Instantly share code, notes, and snippets.

@dragonza
Last active February 4, 2018 17:06
Show Gist options
  • Save dragonza/be098f19fa6d273d8672fac5d4488889 to your computer and use it in GitHub Desktop.
Save dragonza/be098f19fa6d273d8672fac5d4488889 to your computer and use it in GitHub Desktop.
Reverse an integer
function reverseInt(n) {
const reversed = n.toString().split('').reverse().join(''); // turn a number into a string, then turn it into an array to reverse.
return Math.sign(n) * parseInt(reversed); // Math.sign will return -1 as for negative number, 1 as for position number, 0 as for zero.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment