JS如何实现两个超大数相加
function addBigNum(a, b){
let arrA = String(a).split('').reverse()
let arrB = String(b).split('').reverse()
let aLen = arrA.length
let bLen = arrB.length
let shortArr, shortLen, longArr, longLen
let result = []
if(aLen<bLen){
shortArr = arrA
shortLen = aLen
longArr = arrB
longLen = bLen
}else {
shortArr = arrB
shortLen = bLen
longArr = arrA
longLen = aLen
}
let add = 0
for(let i = 0; i < shortLen; i++){
let temp = (parseInt(arrA[i])+parseInt(arrB[i])+add)%10
result.push(temp)
add = Math.floor((parseInt(arrA[i])+parseInt(arrB[i])+add)/10)
}
if(shortLen===longLen){
result.push(add)
}else{
for(let j = shortLen; j < longLen; j++){
if(j===longLen-1){
result.push(parseInt(longArr[j])+add)
}else{
let temp = (parseInt(longArr[j])+add)%10
result.push(temp)
add = Math.floor((parseInt(longArr[j])+add)/10)
}
}
}
return parseInt(result.reverse().join(''))
}