diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml new file mode 100644 index 0000000..02dfa92 --- /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 + # 运行测试 + - run: yarn test diff --git a/lib/add.js b/lib/add.js index 1714b95..544b271 100644 --- a/lib/add.js +++ b/lib/add.js @@ -1,5 +1,21 @@ -function add() { +function add(num1, num2) { // 实现该函数 + // 利用BigInt + // return String(BigInt(num1) + BigInt(num2)) + // 字符串截取进位 + const safeLength = 14; + let a = num1; + let b = num2; // 保留一份参数值 + let flag = 0, // 进位标识 + res = ''; // 结果 + while(a.length || b.length || flag) { + const andSum = String(parseInt(a.substr(-safeLength) || 0, 10) + parseInt(b.substr(-safeLength) || 0, 10)); + res = parseInt(andSum.substr(-safeLength) || 0, 10) + flag + res; + flag = andSum.length > safeLength; + a = a.substr(0, a.length - safeLength); + b = b.substr(0, b.length - safeLength); + } + return res } module.exports = add \ No newline at end of file