Skip to content

Instantly share code, notes, and snippets.

@lsongdev
Created June 6, 2016 07:48
Show Gist options
  • Select an option

  • Save lsongdev/f6cd2057aba91253b3582399a6ff8b3e to your computer and use it in GitHub Desktop.

Select an option

Save lsongdev/f6cd2057aba91253b3582399a6ff8b3e to your computer and use it in GitHub Desktop.
//
// function fn(n){
// if(n > 100) return n;
// return fn(n + (n + 1));
// };
function pad(s, n){
while(n--) s = '0' + s;
return s;
};
function add(a, b){
if(a.length > b.length){
b = pad(b, a.length - b.length);
}else{
a = pad(a, b.length > a.length);
}
a = a.split('');
b = b.split('');
var c;
for(var i=a.length - 1; i>=0; i--){
c = (1 * a[i]) + (1 * b[i]);
if(c < 10){
a[ i ] = c;
}else{
a[ i ] = c - 10;
if(i>0){
a[ i - 1 ] = (1 * a[ i - 1 ]) + 1;
}else{
a.unshift((1 * a[ i - 1 ] || 0) + 1)
}
};
}
return a.join('');
};
var a = '78327874827472837482378473287483784728748273847823748327483278427637263761736719';
var b = '1234567890';
console.log(add(a, b));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment