Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/add.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
function add() {
// 实现该函数
// 843529812342341234
// 236124361425345435
function add(a, b) {
const maxLength = Math.max(a.length, b.length)

a = a.padStart(maxLength , 0);
b = b.padStart(maxLength, 0);
let sum = 0;
let c = 0;
let ret = "";
for(let i=maxLength-1 ; i>=0 ; i--){
sum = parseInt(a[i]) + parseInt(b[i]) + c;
c = Math.floor(sum/10);
ret = sum%10 + ret;
}
if(c == 1){
ret = "1" + ret;
}
return ret;
}

module.exports = add
Loading