http://blog.haoji.me/big-int-add-and-minus.html
解决方法的思路是以字符串的形式按位来相加,即我们平时计算加法一样,个位与个位相加,超过十就进一位的思路
加法
function add(a, b)
{
a = a.split('');
b = b.split('');
var jinwei = 0, result = [];
// 这里一定不要忘了加上“ || jinwei”,否则最后面一个进位会被漏掉
while(a.length || b.length || jinwei)
{
var temp = parseInt(a.pop() || 0) + parseInt(b.pop() || 0) + jinwei;
result.push(temp % 10)
jinwei = Math.floor(temp / 10);
}
return result.reverse().join('');
}
let y=add('1234', '39'); // "1273"
console.log(y)
减法
function add(a, b)
{
a = a.split('');
b = b.split('');
var jinwei = 0, result = [];
// 这里一定不要忘了加上“ || jinwei”,否则最后面一个进位会被漏掉
while(a.length || b.length || jinwei)
{
let ai=a.pop(),bi=b.pop()
var temp = parseInt(ai || 0)-jinwei - parseInt(bi || 0) ;
if(temp<0){
temp = parseInt(ai || 0)-jinwei+parseInt(10) - parseInt(bi || 0) ;
jinwei = 1;
}else{
jinwei = 0;
}
result.push(temp)
}
return result.reverse().join('');
}
let y=add('1234', '39'); //
console.log(y)