From 2e1dc6ba99315af21696b5c0e2e1c8cef2609a9f Mon Sep 17 00:00:00 2001 From: babebabe <775202038@qq.com> Date: Tue, 23 Aug 2022 04:03:15 -0700 Subject: [PATCH 1/5] big data summation --- lib/add.js | 31 +++++++++++++++++++++++++++++++ test/test.spec.js | 23 +++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/add.js b/lib/add.js index 1714b95..953f92d 100644 --- a/lib/add.js +++ b/lib/add.js @@ -1,5 +1,36 @@ function add() { // 实现该函数 + if(arguments.length !== 2){ + throw new Error('wrong argument'); + } + let a = '' + arguments[0]; + let b = '' + arguments[1]; + if(isNaN(parseInt(a)) || isNaN(parseInt(b))){ + throw new TypeError('wrong type'); + } + + a = a.split(''); + b = b.split(''); + let lenA = a.length; + let lenB = b.length; + let temp = []; + let len = lenA > lenB ? lenA : lenB; + + let tempA = 0; + let tempB = 0; + for(let i=1;i<=len;i++){ + tempA = a[lenA-i] || 0; + tempB = b[lenB-i] || 0; + temp.push(parseInt(tempA)+parseInt(tempB)); + } + + for(let j=0;j 10){ + temp[j+1] += 1; + temp[j] -= 10; + } + } + return temp.reverse().join(''); } module.exports = add \ No newline at end of file diff --git a/test/test.spec.js b/test/test.spec.js index 935b70e..901620a 100644 --- a/test/test.spec.js +++ b/test/test.spec.js @@ -1,6 +1,21 @@ var add = require('../lib/add') describe('大数相加add方法', function () { + + test('参数不等于两个时,提示参数错误',function(){ + expect( + ()=>{ + add('123') + } + ).toThrow(Error) + }) + + test('非数字类型或字符类型数字时报错',function(){ + expect(()=>{ + add('abc','123'); + }).toThrow(TypeError); + }) + test('字符串"42329"加上字符串"21532"等于"63861"', function () { expect(add('42329', '21532')).toBe('63861') }) @@ -8,4 +23,12 @@ describe('大数相加add方法', function () { test('"843529812342341234"加上"236124361425345435"等于"1079654173767686669"', function () { expect(add('843529812342341234', '236124361425345435')).toBe('1079654173767686669') }) + + test('"123456"加上"123"等于123579',function(){ + expect(add('123456',123)).toBe('123579'); + }) + + test('"123456"加上"999"等于12455',function(){ + expect(add('123456',999)).toBe('124455'); + }) }) \ No newline at end of file From 96ac378fa068de7e49ce68d5ee9a5f3ace286d6c Mon Sep 17 00:00:00 2001 From: babebabe <775202038@qq.com> Date: Tue, 23 Aug 2022 04:32:19 -0700 Subject: [PATCH 2/5] add github actions --- .github/workflows/learning_github_actions.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .github/workflows/learning_github_actions.yml diff --git a/.github/workflows/learning_github_actions.yml b/.github/workflows/learning_github_actions.yml new file mode 100644 index 0000000..6a72b97 --- /dev/null +++ b/.github/workflows/learning_github_actions.yml @@ -0,0 +1,12 @@ +name: learning-github-actions +on: [push] +jobs: + run-test-case: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3 + - uses: actions/setup-node@3 + with: + node-version: "16" + - run: yarn + - run: yarn test From 4efb35db2cc6e875c89ea62a7448b0c973faa1a1 Mon Sep 17 00:00:00 2001 From: babebabe <775202038@qq.com> Date: Tue, 23 Aug 2022 04:36:52 -0700 Subject: [PATCH 3/5] github actions file --- .github/workflows/learning_github_actions.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/learning_github_actions.yml b/.github/workflows/learning_github_actions.yml index 6a72b97..0a0b0a8 100644 --- a/.github/workflows/learning_github_actions.yml +++ b/.github/workflows/learning_github_actions.yml @@ -2,11 +2,11 @@ name: learning-github-actions on: [push] jobs: run-test-case: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@3 - - uses: actions/setup-node@3 - with: - node-version: "16" - - run: yarn - - run: yarn test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3 + - uses: actions/setup-node@3 + with: + node-version: "16" + - run: yarn + - run: yarn test From 5e0d8b393542a48c4b94a3bdf6edcd03dd33a946 Mon Sep 17 00:00:00 2001 From: babebabe <775202038@qq.com> Date: Tue, 23 Aug 2022 04:39:27 -0700 Subject: [PATCH 4/5] github actions files --- .github/workflows/learning_github_actions.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/learning_github_actions.yml b/.github/workflows/learning_github_actions.yml index 0a0b0a8..19a75da 100644 --- a/.github/workflows/learning_github_actions.yml +++ b/.github/workflows/learning_github_actions.yml @@ -4,8 +4,8 @@ jobs: run-test-case: runs-on: ubuntu-latest steps: - - uses: actions/checkout@3 - - uses: actions/setup-node@3 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 with: node-version: "16" - run: yarn From 04169ea19b65c3a747c150bb9db2582451dff9ee Mon Sep 17 00:00:00 2001 From: babebabe <775202038@qq.com> Date: Thu, 25 Aug 2022 02:29:18 -0700 Subject: [PATCH 5/5] =?UTF-8?q?10=E7=9A=84=E8=BE=B9=E7=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/add.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/add.js b/lib/add.js index 953f92d..2d90eca 100644 --- a/lib/add.js +++ b/lib/add.js @@ -25,7 +25,7 @@ function add() { } for(let j=0;j 10){ + if(temp[j] >= 10){ temp[j+1] += 1; temp[j] -= 10; }