Skip to content
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
12 changes: 12 additions & 0 deletions .github/workflows/learning_github_actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: learning-github-actions
on: [push]
jobs:
run-test-case:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "16"
- run: yarn
- run: yarn test
31 changes: 31 additions & 0 deletions lib/add.js
Original file line number Diff line number Diff line change
@@ -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<len;j++){
if(temp[j] >= 10){
temp[j+1] += 1;
temp[j] -= 10;
}
}
return temp.reverse().join('');
}

module.exports = add
23 changes: 23 additions & 0 deletions test/test.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
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')
})

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');
})
})