3
\$\begingroup\$

The task is taken from leetcode

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

Note:

  • The length of both num1 and num2 is < 5100.

  • Both num1 and num2 contains only digits 0-9.

  • Both num1 and num2 does not contain any leading zero.

  • You must not use any built-in BigInteger library or convert the inputs to integer directly.

My solution 1

/** * @param {string} num1 * @param {string} num2 * @return {string} */ var addStrings = function(num1, num2) { let result = ''; let carry = 0; const LEN1 = num1.length - 1; const LEN2 = num2.length - 1; for (let i = 0, L = Math.max(LEN1, LEN2); i <= L; i++ ) { const numb1 = num1[LEN1 - i] ? num1[LEN1 - i] | 0 : 0; const numb2 = num2[LEN2 - i] ? num2[LEN2 - i] | 0 : 0; const tmp = numb1 + numb2 + carry; carry = tmp > 9 ? 1 : 0; result = `${tmp % 10}${result}`; } return carry > 0 ? 1 + result : result; }; 

My solution 2

/** * @param {string} num1 * @param {string} num2 * @return {string} */ var addStrings = function(num1, num2) { let result = ''; let carry = i = 0; const LEN1 = num1.length - 1; const LEN2 = num2.length - 1; while(num1[LEN1 - i] || num2[LEN2 - i]) { const numb1 = num1[LEN1 - i] ? num1[LEN1 - i] | 0 : 0; const numb2 = num2[LEN2 - i] ? num2[LEN2 - i] | 0 : 0; const tmp = numb1 + numb2 + carry; carry = tmp > 9; result = tmp % 10 + result; i++; } return carry > 0 ? 1 + result : result; }; 
\$\endgroup\$

    1 Answer 1

    2
    \$\begingroup\$

    WARNING

    Never do a double assignment in a variable declaration.

    "use strict"; let carry = i = 0; // throws i is not defined 

    Without strict mode there is no error and i is then declared in the global scope.

    Should be either

    let carry, i; carry = i = 0; 

    or

    let carry = 0, i = 0; 

    or

    let i = 0, carry = i; 

    General

    You can simplify some of the code.

    • Rather than test for a character test the position num1[LEN1 - i] can be (LEN1 - i) >= 0

    • Rather than have LEN1 and LEN2 as constants use them as indexes saving the need to subtract i each time

    • Put the test for the last carry in the while loop to save the need to do the final test on return.

    • Shortening the variable name length lets you put the addition in one line. Generally I use a, b or A ,B for unspecific maths operations.

    Rewrite

    I keep carry as a Number, JS optimizers don't like variables changing type. The benefit is tiny but on mass worth keeping in mind.

    Moved tmp to function scope to keep the inner loop free of tokens (noise)

    function addStrings(a, b) { var la = a.length, lb = b.length, res = "", carry = 0, tmp; while (la > 0 || lb > 0 || carry) { tmp = (la-- > 0 ? +a[la] : 0) + (lb-- > 0 ? +b[lb] : 0) + carry; carry = tmp / 10 | 0; res = tmp % 10 + res; } return res; } 
    \$\endgroup\$
    3
    • \$\begingroup\$Wouldn’t it be more safe to have tmp as a constant the loop?\$\endgroup\$CommentedMay 22, 2019 at 6:51
    • \$\begingroup\$@thadeuszlay Safe? There are 7 lines of code!!\$\endgroup\$CommentedMay 22, 2019 at 11:13
    • \$\begingroup\$Consistency of style? I’d personally always try to use const instead of car/let. Bứt that’s just my opinion\$\endgroup\$CommentedMay 22, 2019 at 11:16

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.