javascript add big numbers

Solutions on MaxInterview for javascript add big numbers by the best coders in the world

showing results for - "javascript add big numbers"
Ian
28 Aug 2016
1function add(str1, str2) {
2
3    let sum = "";  // our result will be stored in a string.
4
5    // we'll need these in the program many times.
6    let str1Length = str1.length;
7    let str2Length = str2.length;
8
9    // if s2 is longer than s1, swap them.
10    if(str2Length > str1Length ){
11        let temp = str2;
12        str2 = str1;
13        str1 = temp;
14    }
15
16    let carry = 0;  // number that is carried to next decimal place, initially zero.
17    let a;
18    let b;
19    let temp;
20    let digitSum;
21    for (let i = 0; i < str1.length; i++) {
22        a = parseInt(str1.charAt(str1.length - 1 - i));      // get ith digit of str1 from right, we store it in a
23        b = parseInt(str2.charAt(str2.length - 1 - i));      // get ith digit of str2 from right, we store it in b
24        b = (b) ? b : 0;                                    // make sure b is a number, (this is useful in case, str2 is shorter than str1
25        temp = (carry + a + b).toString();                  // add a and b along with carry, store it in a temp string.
26        digitSum = temp.charAt(temp.length - 1);            //
27        carry = parseInt(temp.substr(0, temp.length - 1));  // split the string into carry and digitSum ( least significant digit of abSum.
28        carry = (carry) ? carry : 0;                        // if carry is not number, make it zero.
29
30        sum = (i === str1.length - 1) ? temp + sum : digitSum + sum;  // append digitSum to 'sum'. If we reach leftmost digit, append abSum which includes carry too.
31
32    }
33
34    return sum;     // return sum
35
36}
queries leading to this page
what happens when you add numbers too large in javascriptadd big numbers javascriptadd ing large numbers in javascripthow to add large int in jsnodejs add big numbershow to sum two big numbers in jshow to add large numbers in javascriptbig int addition in jshow to deal with a large number in javascripthow to add big numbers in javascripthow to handle big number in javascriptadding big number in javascripthow to handle big numbers in jsadd big numbers in javascriptsum large numbers javascriptadd extremelly large numbers in javascriptadd large numbers in javascriptjavascript create new big numberfactorials of a big number in javascriptadd really huge numbers jsjavascript handle big numbersjavascript adding large numberswhy big numbers not adding correctly in javascriptadding big numbers javascriptjs add to big intjs adding really big numbersbiginteger addition javascriptsum of 2 large integer in javascripthow to solve additong of big numbers in javascriptadding big numbers jshow to add 2 big numbers javascripthow to mix big numbers in javascriptadd big integers javascriptadd big numbers javascript in stringhow to add big numbers jsjavascript sum of 2 large numbersadd big number in javascriptjavascript print big numbersbig number add javascripthow ti add big numbers in jsjavascript add to big intbig numbers in javascriptloop digit in a large number javascriptadding big numbers in javascriptjavascript add big numbersjavascript sum big numbersadding large numbers in javascriptstoring massive numbers in javascripthow to add number to a big number in javascripthow to add big int in jsjavascript big integers addaddition big int with int jshwo to add large number javascriptalgorithm with big number javascripthow to add really big numbers in javascriptjavascript add big numbers