Skip to content

Commit 89437cd

Browse files
author
Jaskaran Singh
authored
Create reverse_integer.js
1 parent 88e8b67 commit 89437cd

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

reverse_integer.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number} x
3+
* @return {number}
4+
*/
5+
var reverse = function(x) {
6+
let isNeg;
7+
let min = Math.pow(-2, 31);
8+
let max = Math.pow(2, 31) - 1;
9+
10+
if(x < 0)
11+
isNeg = 1;
12+
13+
x = x.toString();
14+
15+
for(let i = x.length-1; i >= 0; i--) {
16+
x = x.concat(x[i]);
17+
}
18+
19+
x = x.substring(x.length/2, x.length);
20+
21+
x = parseInt(x);
22+
23+
if(x < min || x > max)
24+
return 0;
25+
26+
if(isNeg)
27+
x = -x;
28+
29+
return x;
30+
};

0 commit comments

Comments
 (0)