Last active
February 4, 2018 17:06
-
-
Save dragonza/be098f19fa6d273d8672fac5d4488889 to your computer and use it in GitHub Desktop.
Reverse an integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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