Skip to content

Instantly share code, notes, and snippets.

@ANUPAMCHAUDHARY1117
Created September 28, 2019 06:04
Show Gist options
  • Save ANUPAMCHAUDHARY1117/8fdce7ee0c72c4d44402b12602a670de to your computer and use it in GitHub Desktop.
Save ANUPAMCHAUDHARY1117/8fdce7ee0c72c4d44402b12602a670de to your computer and use it in GitHub Desktop.
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