/*
    [1]双指针从后向前
    [2]先将字符串上面的字符转化为数值
    [3]相加
    [4]取余取进位
    [5]中间存储的变量反转
    */

    1. function addStrings(num1, num2) {
    2. let i = num1.length - 1;
    3. let j = num2.length - 1;
    4. let add = 0;
    5. const ans = [];
    6. while (i >= 0 || j >= 0 || add != 0) {
    7. const x = i >= 0 ? +num1[i] : 0;
    8. const y = j >= 0 ? +num2[j] : 0;
    9. const result = x + y + add;
    10. ans.push(result % 10);
    11. add = Math.floor(result / 10);
    12. i--;
    13. j--;
    14. }
    15. return ans.reverse().join('');
    16. }
    17. console.log(addStrings('123','321'));