const reverseNum = (x) => {
const MIN = -(2**31)
const MAX = (2**31) - 1
if (x < MIN || x > MAX) {
return 0
}
let num = Math.abs(x)
let revNum = 0
while (num !== 0 ) {
const pop = num % 10
num = Math.floor(x/10)
revNum = revNum * 10 + pop
}
console.log('x', x)
return revNum
}
console.log(reverseNum(123))
Click Run to execute JavaScript.
Code runs in an isolated environment.
Code runs in an isolated environment.