From 676c4b3d4f7ca9ae4d4b0ed53f53ef26e4336340 Mon Sep 17 00:00:00 2001 From: "kexin.xu" Date: Wed, 17 Aug 2022 19:28:33 +0800 Subject: [PATCH 1/2] exercise2 --- .github/workflows | 0 lib/add.js | 19 ++++++++++++++++--- test/test.spec.js | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 .github/workflows diff --git a/.github/workflows b/.github/workflows new file mode 100644 index 0000000..e69de29 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') }) From 4c672c36e4b71095389fc3b744ed5f9421932f0d Mon Sep 17 00:00:00 2001 From: "kexin.xu" Date: Wed, 17 Aug 2022 19:44:18 +0800 Subject: [PATCH 2/2] exercise --- .github/workflows | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/.github/workflows b/.github/workflows index e69de29..cacdd10 100644 --- a/.github/workflows +++ 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