Skip to content

Instantly share code, notes, and snippets.

@auser
Created November 30, 2018 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auser/3a4371023bd8c75c61112acd2223480c to your computer and use it in GitHub Desktop.
Save auser/3a4371023bd8c75c61112acd2223480c to your computer and use it in GitHub Desktop.
/**
* @param {number} x
* @return {number}
*/
var reverse = function(x) {
let y = 0;
let num = Math.abs(x);
const intMax = Math.pow(2, 31) + 1;
while(num != 0) {
let temp = num % 10; // last digit
num = Math.floor(num/10); // update x (remove last digit)
y = (y * 10) + temp;
}
if (y > intMax) return 0;
return Math.sign(x) * y;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment