0
0
pkpavan koka
The code snippet will take a string x and convert it to a number. It will then look to see if x is less than zero and if it is, it will set isNegative to true and convert the number to a reverse string. The reverse string will then be divided by 2, multiplied by 31 and rounded down to return the original number.
var reverse = function(x) {
let number = x.toString()
let isNegative = false
if(x < 0) {
isNegative = true
number = number.substring(1)
}
const reverse = number.split('').reverse().join('');
if(reverse > (Math.pow(2,31) - 1)){
return 0;
} else if (isNegative && reverse > Math.pow(2,31)) {
return 0
}
return isNegative ? -1 * parseInt(reverse) : parseInt(reverse)
};