Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完成作业 #249

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 17 additions & 1 deletion lib/add.js
Original file line number Diff line number Diff line change
@@ -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