Sunny-117/js-challenges

字符串相加

lzxjack opened this issue · 1 comments

字符串相加
var addStrings = function(num1, num2) {
    let p1 = num1.length - 1
    let p2 = num2.length - 1
    let temp = 0
    let res = []

    while(p1 >= 0 || p2 >= 0){
        let n1 = Number(num1[p1] || 0) 
        let n2 = Number(num2[p2] || 0) 
        let sum = n1 + n2 + temp
        res.unshift(sum % 10)
        temp = (sum / 10) | 0
        p1--
        p2--
    }
    if(temp !== 0){
        res.unshift(temp)
    }

    return res.join('')
};