Given a signed 32-bit integer x, return x with its digits reversed. If the reversed value falls outside the signed 32-bit range, return 0.
Pop the last digit with % 10 and push it onto the result with ×10 +. Before pushing, check that the result can grow by one more digit without leaving the 32-bit range.
1function reverse(x: number): number {2let r = 0;3while (x !== 0) {4const d = x % 10; x = Math.trunc(x / 10);5if (r > Math.trunc((2 ** 31 - 1) / 10) || r < Math.trunc(-(2 ** 31) / 10)) return 0;6r = r * 10 + d;7}8return r;9}
r = 0.
Space: play/pause · ←/→: step