From 3d149ec9611421c5c9bc9489dc67c7689d7bd7ac Mon Sep 17 00:00:00 2001 From: "blake.li" Date: Sun, 14 Aug 2022 17:03:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=A4=A7=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/node.js.yml | 30 ++++++++++++++++++++++++++++++ lib/add.js | 20 ++++++++++++++++++-- package.json | 2 +- 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/node.js.yml diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml new file mode 100644 index 0000000..54857db --- /dev/null +++ b/.github/workflows/node.js.yml @@ -0,0 +1,30 @@ +# 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 + # Just run tests once + - run: yarn test diff --git a/lib/add.js b/lib/add.js index 1714b95..850e705 100644 --- a/lib/add.js +++ b/lib/add.js @@ -1,5 +1,21 @@ -function add() { - // 实现该函数 +function add(num1, num2) { + // 强转字符串,对于输入参数值为Number类型的正整数也可以支持 + num1 = String(num1); + num2 = String(num2); + let lenMax = num1.length > num2.length ? num1.length : num2.length; + num1 = num1.padStart(lenMax, '0'); + num2 = num2.padStart(lenMax, '0'); + let carry = 0; + let result = ''; + for (let i = lenMax - 1; i >= 0; i--) { + let sum = parseInt(num1[i]) + parseInt(num2[i]) + carry; + result = sum % 10 + result; + carry = sum > 9 ? 1 : 0; + } + if (carry === 1) { + result = 1 + result; + } + return result; } module.exports = add \ No newline at end of file diff --git a/package.json b/package.json index 556b63b..7efc4d1 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "bugs": { "url": "https://github.com/FE-star/exercise2/issues" }, - "homepage": "https://github.com/FE-star/exercise2#readme", + "homepage": "https://github.com/HappyCoder01/exercise2", "devDependencies": { "jest": "^28.1.2" } From 9d4b657e8da046957c5e8642adce61900ceba952 Mon Sep 17 00:00:00 2001 From: "blake.li" Date: Sun, 14 Aug 2022 17:09:24 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B5=8B=E8=AF=95=20Github=20Actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/add.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/add.js b/lib/add.js index 850e705..3998c08 100644 --- a/lib/add.js +++ b/lib/add.js @@ -1,5 +1,6 @@ function add(num1, num2) { // 强转字符串,对于输入参数值为Number类型的正整数也可以支持 + num1 = String(num1); num2 = String(num2); let lenMax = num1.length > num2.length ? num1.length : num2.length; From 91917d618298ba8bfe51bcf1f3410ae1ae2c8798 Mon Sep 17 00:00:00 2001 From: "blake.li" Date: Sun, 14 Aug 2022 17:11:21 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B5=8B=E8=AF=95=20Github=20Actions=20?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E5=92=8Cpackage.json=20=E4=B8=ADhomepage?= =?UTF-8?q?=E9=A1=B9=E6=9C=89=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7efc4d1..556b63b 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "bugs": { "url": "https://github.com/FE-star/exercise2/issues" }, - "homepage": "https://github.com/HappyCoder01/exercise2", + "homepage": "https://github.com/FE-star/exercise2#readme", "devDependencies": { "jest": "^28.1.2" }