diff --git a/.github/workflows b/.github/workflows new file mode 100644 index 0000000..cacdd10 --- /dev/null +++ b/.github/workflows @@ -0,0 +1,29 @@ +# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions + +name: Node.js CI + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [16.x] + # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ + + steps: + - uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + - run: yarn + - run: yarn test \ No newline at end of file diff --git a/lib/add.js b/lib/add.js index 1714b95..2b4b82d 100644 --- a/lib/add.js +++ b/lib/add.js @@ -1,5 +1,18 @@ -function add() { - // 实现该函数 +function add(str1, str2) { + let arr1 = str1.split("").map((s) => Number(s)), + arr2 = str2.split("").map((s) => Number(s)); + let temp, + result = "", + sign = 0; + + while (arr1.length || arr2.length || sign) { + temp = (arr1.pop() || 0) + (arr2.pop() || 0) + sign; + + sign = Math.floor(temp / 10); + result = (temp % 10) + "" + result; + } + + return result; } -module.exports = add \ No newline at end of file +module.exports = add; \ No newline at end of file diff --git a/test/test.spec.js b/test/test.spec.js index 935b70e..06b0335 100644 --- a/test/test.spec.js +++ b/test/test.spec.js @@ -4,7 +4,7 @@ describe('大数相加add方法', function () { test('字符串"42329"加上字符串"21532"等于"63861"', function () { expect(add('42329', '21532')).toBe('63861') }) - + test('"843529812342341234"加上"236124361425345435"等于"1079654173767686669"', function () { expect(add('843529812342341234', '236124361425345435')).toBe('1079654173767686669') })