Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev|execise2作业 #263

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 32 additions & 2 deletions lib/add.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
function add() {
// 实现该函数
function add(strOne, strTwo) {
// 比较两个数长度 短的一方前面补0
if (strOne.length > strTwo.length) {
let arr = Array(strOne.length - strTwo.length).fill(0);
strTwo = arr.join('') + strTwo
} else if (strOne.length < strTwo.length) {
let arr = Array(strTwo.length - strOne.length).fill(0);
strOne = arr.join('') + strOne
}
// 字符串转为数组后反转,方便对数组加法运算
var strOneArr = strOne.split('').reverse()
var strTwoArr = strTwo.split('').reverse()
var sign = 0 // 进位标识
var resultArr = []

for (let i = 0; i < strOneArr.length; i++) {
var addResult = Number(strOneArr[i]) + Number(strTwoArr[i]) + sign
if (addResult >= 10) {
sign = 1
resultArr.push(addResult % 10)
continue
} else {
sign = 0
resultArr.push(addResult)
continue
}
}
// 最后一位计算完后,有进位,计入进位
if (sign) {
resultArr.push(sign)
}
return resultArr.reverse().join('')
}

module.exports = add