Created
June 6, 2021 07:30
-
-
Save samandar-boymurodov/bff99dda575314288b712183f3f66f26 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* @param {number} x | |
* @return {number} | |
*/ | |
var reverse = function(x) { | |
let result = 0 | |
let number = Math.abs(x) | |
for (let i of [...new Array(number.toString().length)].map((e, index) => Number("1" + "0".repeat(index))).reverse()) { | |
result += (number % 10) * i | |
number = Math.floor(number / 10) | |
} | |
result = x<0 ? -result : result | |
if (result <= 2**31 -1 && result>= Math.pow(-2, 31)) { | |
return result | |
} else { | |
return 0 | |
} | |
}; | |
Author
samandar-boymurodov
commented
Jun 6, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment