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..3998c08 100644 --- a/lib/add.js +++ b/lib/add.js @@ -1,5 +1,22 @@ -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