From 42db20356a605fee9231d1a73460cc8f4cfb08f2 Mon Sep 17 00:00:00 2001 From: xjq Date: Sat, 3 Dec 2022 02:00:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20=E9=87=91=E9=A2=9D?= =?UTF-8?q?=E5=88=A4=E6=96=AD=20=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- question/FrontEnd/isAmount/answer.md | 9 +++++ question/FrontEnd/isAmount/answer.mjs | 3 ++ question/FrontEnd/isAmount/index.md | 45 +++++++++++++++++++++++++ question/FrontEnd/isAmount/test.mjs | 47 +++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 question/FrontEnd/isAmount/answer.md create mode 100644 question/FrontEnd/isAmount/answer.mjs create mode 100644 question/FrontEnd/isAmount/index.md create mode 100644 question/FrontEnd/isAmount/test.mjs 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); +});