Skip to content

Commit

Permalink
feat: 新增 金额判断 题目
Browse files Browse the repository at this point in the history
  • Loading branch information
xjq7 committed Dec 2, 2022
1 parent 9aae34b commit 42db203
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
9 changes: 9 additions & 0 deletions question/FrontEnd/isAmount/answer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### 方法一

正则表达式

```js
export default function isAmount(amount) {
return /^(0|[1-9]\d*)(\.\d{1,2})?$/.test(amount);
}
```
3 changes: 3 additions & 0 deletions question/FrontEnd/isAmount/answer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function isAmount(amount) {
return /^(0|[1-9]\d*)(\.\d{1,2})?$/.test(amount);
}
45 changes: 45 additions & 0 deletions question/FrontEnd/isAmount/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
金额合法性判断

合法的金额包含整数以及小数点后不超过两位的小数

用例 1:

```js
const amount = '20';
isAmount(amount); // true
```

用例 2:

```js
const amount = '1.23';
isAmount(amount); // true
```

用例 3:

```js
const amount = '1.';
isAmount(amount); // false
```

用例 4:

```js
const amount = '0.2';
isAmount(amount); // true
```

用例 5:

```js
const amount = '.1';
isAmount(amount); // false
```

用例 6:

```js
const amount = '02';
isAmount(amount); // false
```
47 changes: 47 additions & 0 deletions question/FrontEnd/isAmount/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import isAmount from './answer.mjs';
import { it } from 'mocha';
import { assert } from 'chai';

it('用例 1: 输入 1.23', () => {
assert.equal(isAmount('1.23'), true);
});

it('用例 2: 输入 256.23', () => {
assert.equal(isAmount('256.23'), true);
});

it('用例 3: 输入 0.23', () => {
assert.equal(isAmount('0.23'), true);
});

it('用例 4: 输入 0.2', () => {
assert.equal(isAmount('0.2'), true);
});

it('用例 5: 输入 0.234', () => {
assert.equal(isAmount('0.234'), false);
});

it('用例 6: 输入 0.', () => {
assert.equal(isAmount('0.'), false);
});

it('用例 7: 输入 .2', () => {
assert.equal(isAmount('.2'), false);
});

it('用例 8: 输入 20', () => {
assert.equal(isAmount('20'), true);
});

it('用例 9: 输入 02', () => {
assert.equal(isAmount('02'), false);
});

it('用例 10: 输入 102', () => {
assert.equal(isAmount('102'), true);
});

it('用例 11: 输入 102.2', () => {
assert.equal(isAmount('102.2'), true);
});

0 comments on commit 42db203

Please sign in to comment.