-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.js
60 lines (58 loc) · 1.76 KB
/
add.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
function addmINT(strng1, strng2) {
var longestString;
var shortestString;
if (strng1.length > strng2.length) {
longestString = strng1;
shortestString = strng2;
} else {
longestString = strng2;
shortestString = strng1;
}
while (shortestString.length < 4096) {
shortestString = "0" + shortestString;
}
while (longestString.length < 4096) {
longestString = "0" + longestString;
}
if (strng1.length > strng2.length) {
strng1 = longestString;
strng2 = shortestString;
} else {
strng2 = longestString;
strng1 = shortestString;
}
var carry = 0;
var a;
var b;
var c;
var d;
var e;
var strng3 = '----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'.split("");
for (var i = longestString.length - 1; i >= 0; i --) {
if (carry) {
carry = 0;
a = strng1[i];
b = strng2[i];
c = parseInt(a) + parseInt(b) + 1;
if (c >= 10) {
carry = 1;
strng3[i] = c.toString()[1];
} else {
strng3[i] = c.toString()[0];
}
} else {
a = strng1[i];
b = strng2[i];
c = parseInt(a) + parseInt(b);
if (c >= 10) {
carry = 1;
strng3[i] = c.toString()[1];
} else {
strng3[i] = c.toString()[0];
}
}
}
d = strng3.join("").split("-")[0];
return d.replace(/^0+/g, '');
}
module.exports = addmINT;