算法:JavaScript处理极限数字相加求和的问题

问题:JavaScript的数字超过2的52次方的极限值就会相加就会出问题;如何解决?
例如:999999999999999999999+88888888888888888888888888


```javascript

```javascript
function add(str1, str2) {
    const arr1 = str1.split('')
    const arr2 = str2.split('')
    const res = []
    let more = 0
    while (arr1.length || arr2.length) {
        let num1 = arr1.pop()-0;
        let num2 = arr2.pop()-0;
        let adds = null;
        if((num1||num1===0)&&(num2||num2===0)){
            adds=num1+num2
        }else if(num1||num1===0){
            adds=num1
        }else if(num2||num2===0){
            adds=num2
        }
        if(!adds&&adds!==0)return
        if (adds >= 10) {
            more = 1
            res.unshift(adds - 10)
        } else {
            if (more) {
                res.unshift(adds - 0 + more)
                more = 0
            } else {
                res.unshift(adds - 0 + more)
            }
        }
    }
    return res.join('')
}
console.log(add('999999999999999999999','88888888888888888888888888'))
     999999999999999999999
88888888888888888888888888
88889777777777777777777777

这样就解决了大数相加出错的问题了

上一篇:Leetcode - 43. 字符串相乘


下一篇:049.函数-函数的定义