Skip to content

Commit

Permalink
Solution for 7- Reverse Integer in TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinha1994 authored Jul 10, 2022
1 parent 5515379 commit b421501
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions typescript/7-Reverse-Integer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const reverse = (x: number): number => {
const max: number = 2 ** 31 - 1;
const min: number = -(2 ** 31);

let result: number = 0;
while (x !== 0) {
const digit = x % 10;
x = Math.trunc(x / 10);

if (result > max / 10 || (result === max / 10 && digit >= max % 10)) {
return 0;
} else if (
result < min / 10 ||
(result === max / 10 && digit <= min % 10)
) {
return 0;
} else {
result = result * 10 + digit;
}
}

return result;
};

0 comments on commit b421501

Please sign in to comment.