diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..0063a3d --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,12 @@ +name: github-actions +on: [push] +jobs: + check-bats-version: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: '14' + - run: npm install -g bats + - run: bats -v diff --git a/lib/add.js b/lib/add.js index 1714b95..83eff59 100644 --- a/lib/add.js +++ b/lib/add.js @@ -1,5 +1,41 @@ -function add() { +function add(a, b) { // 实现该函数 + // 非数字报错 + if(isNaN(a) || isNaN(b)) throw Error('需要传入数字或字符串型数字') + const maxLength = 16 + // 边界:16位以内可以正常相加 + if(String(a).length=0 || bLength>=0) { + const resultTemp = Number(aList[aLength] || 0) + Number(bList[bLength] || 0) + // 判断长度是否溢出 + if(resultTemp.length > Math.max(aList[aLength].length, bList[bLength].length)) { + addResultList[resultLength - 1] = 1 + resultTemp = resultTemp.slice(1) + } + addResultList[resultLength] = addResultList[resultLength] + resultTemp + + aLength-- + bLength-- + resultLength-- + } + + const result = addResultList.join('') + + return result } module.exports = add \ No newline at end of file diff --git a/test/test.spec.js b/test/test.spec.js index 935b70e..6ec6493 100644 --- a/test/test.spec.js +++ b/test/test.spec.js @@ -8,4 +8,6 @@ describe('大数相加add方法', function () { test('"843529812342341234"加上"236124361425345435"等于"1079654173767686669"', function () { expect(add('843529812342341234', '236124361425345435')).toBe('1079654173767686669') }) -}) \ No newline at end of file +}) + +// test: add git actions \ No newline at end of file