```
function reverse(n) {
const MAX = 2147483647;
const sign = n < 0 ? -1 : 1;
let result = 0;
for (let i = Math.abs(n); i >= 1; i = Math.floor(i / 10)) {
result = (result * 10) + (i % 10);
}
return result < MAX ? (result * sign) : 0;
}
```