Created
May 2, 2016 23:50
-
-
Save armandocanals/339e2ae831080fa9f825a71cf45f8ecb to your computer and use it in GitHub Desktop.
Sum Large Numbers in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add(num1, num2) { | |
num1 = num1.split(''); | |
num2 = num2.split(''); | |
num1 = num1.map(function (num) { | |
return parseInt(num, 10); | |
}); | |
num2 = num2.map(function (num) { | |
return parseInt(num, 10); | |
}); | |
if (num2.length > num1.length) { | |
return _add(num2, num1); | |
} else { | |
return _add(num1, num2) | |
} | |
} | |
function _add(num1, num2) { | |
var num1_idx = num1.length-1; | |
var num2_idx = num2.length-1; | |
var remainder = 0; | |
for (; num1_idx > -1; num1_idx--, num2_idx--) { | |
var sum = num1[num1_idx] + remainder; | |
if (num2_idx > -1) { | |
sum += num2[num2_idx]; | |
} | |
if (sum <= 9 || num1_idx === 0) { | |
remainder = 0; | |
num1[num1_idx] = sum; | |
} else if (sum >= 10) { | |
remainder = 1; | |
num1[num1_idx] = sum - 10; | |
} | |
console.log(remainder); | |
} | |
return num1.join(''); | |
} | |
document.querySelector('#sum').innerHTML = add('1923129312616892123789632134615237899999999999999999999999999999995', '19273612943202027304826153899999999999999999999999999999999999999999999999999999999'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your time and effort!