forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (51 loc) · 1.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function reverseNumber(num) {
if (typeof num !== 'number') {
throw new Error('Invalid Argument');
}
let absNum = Math.abs(num);
let reverse = 0;
while (absNum > 0) {
const rem = absNum % 10;
reverse = reverse * 10 + rem;
absNum = parseInt(absNum / 10, 10); // important to get whole number
}
return reverse * Math.sign(num);
}
/**
*
* Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 1534236469
Output: 0 // overflows
Note:
Assume we are dealing with an environment which could only
store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1].
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
*/
function reverse32BitInt(x) {
let isNegetive = 0;
if (x < 0) {
x *= -1;
isNegetive = 1;
}
let reverse = 0;
while (x >= 1) {
const r = Math.floor(x % 10);
reverse = reverse * 10 + r;
x = Math.floor(x / 10);
}
if (reverse > 0x7fffffff) {
return 0;
}
return isNegetive ? reverse * -1 : reverse;
}
module.exports = {
reverseNumber,
reverse32BitInt
};