Last active
October 17, 2018 12:47
-
-
Save netdesignr/afa7c626bd5a1cf6e69f7d629296e937 to your computer and use it in GitHub Desktop.
Reverse Integer// source https://jsbin.com/kororer
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
/* | |
* Reverse Integer using Javascript ES6 | |
* Built in JS methods: toString(), split(), reduce(), Math.sign() | |
*/ | |
const reverseInt = (n, rev='', final) => { | |
final = n.toString().split('').reduce((acc, inv) => rev = inv + acc, ''); | |
return parseInt(final) * Math.sign(n); | |
} | |
console.log(reverseInt(15)); // 51 | |
console.log(reverseInt(981)); // 189 | |
console.log(reverseInt(500)); // 5 | |
console.log(reverseInt(-15)); // -51 | |
console.log(reverseInt(-90)); // -9 | |
console.log(reverseInt(0)); // 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment