实现一个add方法完成两个大数相加
# 实现一个add方法完成两个大数相加
let a = "9007199254740991";
let b = "1234567899999999999";
function add(a ,b){
//...
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
function add(a, b){
const [i, j, curry, res] = [a.length - 1, b.length - 1, 0, ''];
while(i >=0 || j >=0 || curry){
const sum = (+a[i--] || 0) + (+b[j--] || 0 ) + curry;
curry = ~~(sum / 10);
res = (sum % 10) + res;
}
return res;
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9