-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathadding-big-numbers.js
57 lines (55 loc) · 998 Bytes
/
adding-big-numbers.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
function add(a, b) {
const length = Math.max(a.length, b.length);
a = a.padStart(length, '0');
b = b.padStart(length, '0');
const aDigits = [...a];
const bDigits = [...b];
const resultDigits = [];
let carry = 0;
for (let i = length - 1; i >= 0; i -= 1) {
const result = +aDigits[i] + +bDigits[i] + carry;
if (result < 10) {
resultDigits.unshift(result);
carry = 0;
} else {
resultDigits.unshift(result % 10);
carry = 1;
}
}
if (carry) {
resultDigits.unshift(carry);
}
return resultDigits.join('');
}
console.log(
add('1', '10'),
'11'
);
console.log(
add('1', '1'),
'2'
);
console.log(
add('123', '456'),
'579'
);
console.log(
add('888', '222'),
'1110'
);
console.log(
add('1372', '69'),
'1441'
);
console.log(
add('12', '456'),
'468'
);
console.log(
add('101', '100'),
'201'
);
console.log(
add('63829983432984289347293874', '90938498237058927340892374089'),
'91002328220491911630239667963'
);