Here is the simple solution with JavaScript:
function add(a, b) { var ans = ""; if (a.length > b.length ) { var zeroLength = a.length - b.length; for (var index=0; index<zeroLength; index++) { b = "0" + b; } } if (a.length < b.length ) { var zeroLength = b.length - a.length; for (var index=0; index<zeroLength; index++) { a = "0" + a; } } var step = []; if (a.length === b.length) { for (var index=a.length-1; index>=0; index--) { var numberA = Number(a[index]); var numberB = Number(b[index]); var addResult = 0; if (step[index+1]) { addResult = step[index+1] + numberA + numberB; } else { addResult = numberA + numberB; } if (addResult >= 10 && index !== 0) { ans = String(addResult - 10) + ans; step[index] = 1; } else { ans = String(addResult) + ans; } } } return ans; }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here is the simple solution with JavaScript: