Created
September 28, 2019 06:04
-
-
Save ANUPAMCHAUDHARY1117/8fdce7ee0c72c4d44402b12602a670de to your computer and use it in GitHub Desktop.
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 addZeroToString(str1, str2){ | |
while (str1.length > str2.length) { | |
str2 = "0" + str2; | |
} | |
return str2; | |
} | |
function addTwoBugNumbers(a, b) { | |
if (a.length > b.length) { | |
b = addZeroToString(a,b); | |
} else { | |
a = addZeroToString(b,a); | |
} | |
a1 = a.split(""); | |
b1 = b.split(""); | |
let sum = 0; | |
let carry = 0; | |
let array = []; | |
for (var i = a1.length-1; i >= 0; i--) { | |
sum = parseInt(a[i]) + parseInt(b[i]) + parseInt(carry); | |
if (sum >= 10) { | |
carry = 1; | |
sum = sum - 10; | |
} else { | |
carry = 0; | |
} | |
array.push(sum); | |
} | |
console.log(array.reverse().join("")); | |
return array.join(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment