Skip to content

Instantly share code, notes, and snippets.

@konstantinzolotarev
Created April 25, 2016 07:00
Show Gist options
  • Save konstantinzolotarev/3b56d20ad4c39f1bae191a54220f467f to your computer and use it in GitHub Desktop.
Save konstantinzolotarev/3b56d20ad4c39f1bae191a54220f467f to your computer and use it in GitHub Desktop.
Summ strings
function summ_char (str1, str2) {
return '' + (parseInt(str1) + parseInt(str2));
}
function summ (str1, str2) {
var num1 = str1.split('');
var num2 = str2.split('');
var tmp1, tmp2;
var result = '';
var mem = 0;
var nextStep = true;
while (nextStep) {
if (num1.length) {
tmp1 = num1.pop();
} else {
tmp1 = 0;
}
if (num2.length) {
tmp2 = num2.pop();
} else {
tmp2 = 0;
}
if (mem > 0) {
tmp1 = summ_char(mem, tmp1);
mem = 0;
}
var res = summ_char(tmp1, tmp2);
if (res.length > 1) {
mem = 1;
res = res.split('').pop();
}
if (num1.length <= 0 && num2.length <= 0) {
nextStep = false;
}
result = res + result;
}
return result;
}
console.log(summ('1112', '190'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment