diff --git a/question/FrontEnd/isAmount/answer.md b/question/FrontEnd/isAmount/answer.md new file mode 100644 index 0000000..6739fb3 --- /dev/null +++ b/question/FrontEnd/isAmount/answer.md @@ -0,0 +1,9 @@ +### 方法一 + +正则表达式 + +```js +export default function isAmount(amount) { + return /^(0|[1-9]\d*)(\.\d{1,2})?$/.test(amount); +} +``` diff --git a/question/FrontEnd/isAmount/answer.mjs b/question/FrontEnd/isAmount/answer.mjs new file mode 100644 index 0000000..32b998b --- /dev/null +++ b/question/FrontEnd/isAmount/answer.mjs @@ -0,0 +1,3 @@ +export default function isAmount(amount) { + return /^(0|[1-9]\d*)(\.\d{1,2})?$/.test(amount); +} diff --git a/question/FrontEnd/isAmount/index.md b/question/FrontEnd/isAmount/index.md new file mode 100644 index 0000000..5072579 --- /dev/null +++ b/question/FrontEnd/isAmount/index.md @@ -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 +``` diff --git a/question/FrontEnd/isAmount/test.mjs b/question/FrontEnd/isAmount/test.mjs new file mode 100644 index 0000000..7c72fc1 --- /dev/null +++ b/question/FrontEnd/isAmount/test.mjs @@ -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); +});