fix(i18n): update Chinese translation of JavaScript Algorithms and Da… (#38494)

Co-authored-by: Zhicheng Chen <chenzhicheng@dayuwuxian.com>
Co-authored-by: S1ngS1ng <liuxing0514@gmail.com>
This commit is contained in:
ZhichengChen
2020-09-07 16:17:22 +08:00
committed by GitHub
parent b378f110ca
commit 193d13e7ba
5 changed files with 237 additions and 107 deletions

View File

@ -3,15 +3,21 @@ id: 56533eb9ac21ba0edf2244e2
title: Caesars Cipher title: Caesars Cipher
challengeType: 5 challengeType: 5
isRequired: true isRequired: true
videoUrl: '' forumTopicId: 16003
localeTitle: 凯撒密码 localeTitle: 凯撒密码
--- ---
## Description ## Description
<section id="description">最简单和最广为人知的<dfn>密码之一</dfn><code>Caesar cipher</code> ,也称为<code>shift cipher</code> 。在<code>shift cipher</code>中,字母的含义被移动一些设定量。一种常见的现代用途是<a href="https://en.wikipedia.org/wiki/ROT13" target="_blank">ROT13</a>密码其中字母的值移动了13个位置。因此&#39;A&#39;&#39;&#39;N&#39;&#39;B&#39;&#39;&#39;O&#39;等等。编写一个函数,它将<a href="https://en.wikipedia.org/wiki/ROT13" target="_blank">ROT13</a>编码的字符串作为输入并返回一个已解码的字符串。所有字母都是大写的。不要转换任何非字母字符(即空格,标点符号),但要传递它们。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
<code>凯撒密码</code>是最简单和最广为人知的<dfn>密码之一</dfn>,也被称为<code>移位密码</code>。在<code>移位密码</code>中,明文中的字母通过按照一个固定数目进行偏移后被替换成新的字母。
<a href="https://en.wikipedia.org/wiki/ROT13" target='_blank'>ROT13</a> 是一个被广泛使用的编码技术,明文中的所有字母都被移动 13 位。因此,'A' &harr; 'N', 'B' &harr; 'O' 等等。
请编写一个函数,用于解码一个被 <a href="https://en.wikipedia.org/wiki/ROT13" target='_blank'>ROT13</a> 编码的字符串,然后返回解码后的结果。
所有解码后的字母都必须为字母大写。请不要解码非字母的字符(例如,空格、标点符号),但你需要在结果中保留它们。
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,13 +25,13 @@ localeTitle: 凯撒密码
```yml ```yml
tests: tests:
- text: <code>rot13(&quot;SERR PBQR PNZC&quot;)</code>应解码为<code>FREE CODE CAMP</code> - text: "<code>rot13('SERR PBQR PNZC')</code>应解码为<code>FREE CODE CAMP</code>。"
testString: assert(rot13("SERR PBQR PNZC") === "FREE CODE CAMP"); testString: assert(rot13("SERR PBQR PNZC") === "FREE CODE CAMP");
- text: <code>rot13(&quot;SERR CVMMN!&quot;)</code>应解码为<code>FREE PIZZA!</code> <code>rot13(&quot;SERR CVMMN!&quot;)</code> <code>FREE PIZZA!</code> - text: "<code>rot13('SERR CVMMN!')</code>应解码为<code>FREE PIZZA!</code>。"
testString: assert(rot13("SERR CVMMN!") === "FREE PIZZA!"); testString: assert(rot13("SERR CVMMN!") === "FREE PIZZA!");
- text: <code>rot13(&quot;SERR YBIR?&quot;)</code>应解码为<code>FREE LOVE?</code> - text: "<code>rot13('SERR YBIR?')</code>应解码为<code>FREE LOVE?</code>。"
testString: assert(rot13("SERR YBIR?") === "FREE LOVE?"); testString: assert(rot13("SERR YBIR?") === "FREE LOVE?");
- text: <code>rot13(&quot;GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.&quot;)</code>应该在<code>rot13(&quot;GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.&quot;)</code>解码<code>THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.</code> - text: "<code>rot13('GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.')</code>应解码<code>THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.</code>。"
testString: assert(rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.") === "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."); testString: assert(rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.") === "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.");
``` ```
@ -45,7 +51,6 @@ function rot13(str) { // LBH QVQ VG!
// Change the inputs below to test // Change the inputs below to test
rot13("SERR PBQR PNZC"); rot13("SERR PBQR PNZC");
``` ```
</div> </div>
@ -57,8 +62,33 @@ rot13("SERR PBQR PNZC");
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```js
// solution required var lookup = {
'A': 'N','B': 'O','C': 'P','D': 'Q',
'E': 'R','F': 'S','G': 'T','H': 'U',
'I': 'V','J': 'W','K': 'X','L': 'Y',
'M': 'Z','N': 'A','O': 'B','P': 'C',
'Q': 'D','R': 'E','S': 'F','T': 'G',
'U': 'H','V': 'I','W': 'J','X': 'K',
'Y': 'L','Z': 'M'
};
function rot13(encodedStr) {
var codeArr = encodedStr.split(""); // String to Array
var decodedArr = []; // Your Result goes here
// Only change code below this line
decodedArr = codeArr.map(function(letter) {
if(lookup.hasOwnProperty(letter)) {
letter = lookup[letter];
}
return letter;
});
// Only change code above this line
return decodedArr.join(""); // Array to String
}
``` ```
/section> </section>

View File

@ -3,15 +3,24 @@ id: aa2e6f85cab2ab736c9a9b24
title: Cash Register title: Cash Register
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16012
localeTitle: 收银机 localeTitle: 收银机
--- ---
## Description ## Description
<section id="description">设计一个收银抽屉功能<code>checkCashRegister()</code> ,它接受购买价格作为第一个参数( <code>price</code> ),支付作为第二个参数( <code>cash</code> ),以及现金抽屉( <code>cid</code> )作为第三个参数。 <code>cid</code>是列出可用货币的2D数组。 <code>checkCashRegister()</code>函数应始终返回带有<code>status</code>键和<code>change</code>键的对象。返回<code>{status: &quot;INSUFFICIENT_FUNDS&quot;, change: []}</code>如果出现的现金少于到期的更改,或者如果您无法返回确切的更改。返回<code>{status: &quot;CLOSED&quot;, change: [...]}</code>使用cash-in-drawer作为密钥<code>change</code>的值,如果它等于更改到期。否则,返回<code>{status: &quot;OPEN&quot;, change: [...]}</code> ,以硬币和账单中的更改到期,按从最高到最低的顺序排序,作为<code>change</code>密钥的值。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 <table class="table table-striped"><tbody><tr><th>货币单位</th><th></th></tr><tr><td>一分钱</td><td> 0.01美元PENNY </td></tr><tr><td></td><td> 0.05美元NICKEL </td></tr><tr><td>十分钱</td><td> 0.1美元DIME </td></tr><tr><td> 25美分硬币</td><td> 0.25美元(季) </td></tr><tr><td>美元</td><td> 1美元美元 </td></tr><tr><td>五美元</td><td> 5美元 </td></tr><tr><td>十美元</td><td> 10美元10日 </td></tr><tr><td>二十美元</td><td> 20美元二十美元 </td></tr><tr><td>一百元</td><td> 100美元一百 </td></tr></tbody></table></section> <section id='description'>
编写一个用于收银机的函数<code>checkCashRegister()</code>,传入售价为第一个参数(<code>price</code>)、支付金额为第二个参数(<code>cash</code>)、收银机內的金额为第三个参数(<code>cid</code>)。
<code>cid</code>是包含货币面值的二维数组。
函数<code>checkCashRegister()</code>必须返回含有<code>status</code>键值和<code>change</code>键值的对象。
如果收银机內的金额少于应找回的零钱数,或者你无法返回确切的数目时,返回<code>{status: "INSUFFICIENT_FUNDS", change: []}</code>
如果收银机內的金额等于应找回的零钱数,返回<code>{status: "CLOSED", change: [...]}</code>,其中<code>change</code>键值是收银机內的金额。
否则,返回<code>{status: "OPEN", change: [...]}</code>,其中<code>change</code>键值是应找回的零钱数,并且它的面值由高到低排序。
<table class='table table-striped'><tr><th>货币单位</th><th>面值</th></tr><tr><td>Penny</td><td>$0.01 (PENNY)</td></tr><tr><td>Nickel</td><td>$0.05 (NICKEL)</td></tr><tr><td>Dime</td><td>$0.1 (DIME)</td></tr><tr><td>Quarter</td><td>$0.25 (QUARTER)</td></tr><tr><td>Dollar</td><td>$1 (DOLLAR)</td></tr><tr><td>Five Dollars</td><td>$5 (FIVE)</td></tr><tr><td>Ten Dollars</td><td>$10 (TEN)</td></tr><tr><td>Twenty Dollars</td><td>$20 (TWENTY)</td></tr><tr><td>One-hundred Dollars</td><td>$100 (ONE HUNDRED)</td></tr></table>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,17 +28,17 @@ localeTitle: 收银机
```yml ```yml
tests: tests:
- text: '<code>checkCashRegister(19.5, 20, [[&quot;PENNY&quot;, 1.01], [&quot;NICKEL&quot;, 2.05], [&quot;DIME&quot;, 3.1], [&quot;QUARTER&quot;, 4.25], [&quot;ONE&quot;, 90], [&quot;FIVE&quot;, 55], [&quot;TEN&quot;, 20], [&quot;TWENTY&quot;, 60], [&quot;ONE HUNDRED&quot;, 100]])</code>应该返回一个物体。' - text: "<code>checkCashRegister(19.5, 20, [['PENNY', 1.01], ['NICKEL', 2.05], ['DIME', 3.1], ['QUARTER', 4.25], ['ONE', 90], ['FIVE', 55], ['TEN', 20], ['TWENTY', 60], ['ONE HUNDRED', 100]])</code>应该返回一个对象。"
testString: assert.deepEqual(Object.prototype.toString.call(checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])), '[object Object]'); testString: assert.deepEqual(Object.prototype.toString.call(checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])), '[object Object]');
- text: '<code>checkCashRegister(19.5, 20, [[&quot;PENNY&quot;, 1.01], [&quot;NICKEL&quot;, 2.05], [&quot;DIME&quot;, 3.1], [&quot;QUARTER&quot;, 4.25], [&quot;ONE&quot;, 90], [&quot;FIVE&quot;, 55], [&quot;TEN&quot;, 20], [&quot;TWENTY&quot;, 60], [&quot;ONE HUNDRED&quot;, 100]])</code>应返回<code>{status: &quot;OPEN&quot;, change: [[&quot;QUARTER&quot;, 0.5]]}</code> 。' - text: '<code>checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])</code>应返回<code>{status: "OPEN", change: [["QUARTER", 0.5]]}</code>。'
testString: 'assert.deepEqual(checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]), {status: "OPEN", change: [["QUARTER", 0.5]]});' testString: 'assert.deepEqual(checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]), {status: "OPEN", change: [["QUARTER", 0.5]]});'
- text: '<code>checkCashRegister(3.26, 100, [[&quot;PENNY&quot;, 1.01], [&quot;NICKEL&quot;, 2.05], [&quot;DIME&quot;, 3.1], [&quot;QUARTER&quot;, 4.25], [&quot;ONE&quot;, 90], [&quot;FIVE&quot;, 55], [&quot;TEN&quot;, 20], [&quot;TWENTY&quot;, 60], [&quot;ONE HUNDRED&quot;, 100]])</code>应返回<code>{status: &quot;OPEN&quot;, change: [[&quot;TWENTY&quot;, 60], [&quot;TEN&quot;, 20], [&quot;FIVE&quot;, 15], [&quot;ONE&quot;, 1], [&quot;QUARTER&quot;, 0.5], [&quot;DIME&quot;, 0.2], [&quot;PENNY&quot;, 0.04]]}</code> 。' - text: '<code>checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])</code>应返回<code>{status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}</code>。'
testString: 'assert.deepEqual(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]), {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]});' testString: 'assert.deepEqual(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]), {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]});'
- text: '<code>checkCashRegister(19.5, 20, [[&quot;PENNY&quot;, 0.01], [&quot;NICKEL&quot;, 0], [&quot;DIME&quot;, 0], [&quot;QUARTER&quot;, 0], [&quot;ONE&quot;, 0], [&quot;FIVE&quot;, 0], [&quot;TEN&quot;, 0], [&quot;TWENTY&quot;, 0], [&quot;ONE HUNDRED&quot;, 0]])</code>应返回<code>{status: &quot;INSUFFICIENT_FUNDS&quot;, change: []}</code> 。' - text: '<code>checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])</code>应返回<code>{status: "INSUFFICIENT_FUNDS", change: []}</code>。'
testString: 'assert.deepEqual(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]), {status: "INSUFFICIENT_FUNDS", change: []});' testString: 'assert.deepEqual(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]), {status: "INSUFFICIENT_FUNDS", change: []});'
- text: '<code>checkCashRegister(19.5, 20, [[&quot;PENNY&quot;, 0.01], [&quot;NICKEL&quot;, 0], [&quot;DIME&quot;, 0], [&quot;QUARTER&quot;, 0], [&quot;ONE&quot;, 1], [&quot;FIVE&quot;, 0], [&quot;TEN&quot;, 0], [&quot;TWENTY&quot;, 0], [&quot;ONE HUNDRED&quot;, 0]])</code>应返回<code>{status: &quot;INSUFFICIENT_FUNDS&quot;, change: []}</code> 。' - text: '<code>checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])</code>应返回<code>{status: "INSUFFICIENT_FUNDS", change: []}</code>。'
testString: 'assert.deepEqual(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]), {status: "INSUFFICIENT_FUNDS", change: []});' testString: 'assert.deepEqual(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]), {status: "INSUFFICIENT_FUNDS", change: []});'
- text: '<code>checkCashRegister(19.5, 20, [[&quot;PENNY&quot;, 0.5], [&quot;NICKEL&quot;, 0], [&quot;DIME&quot;, 0], [&quot;QUARTER&quot;, 0], [&quot;ONE&quot;, 0], [&quot;FIVE&quot;, 0], [&quot;TEN&quot;, 0], [&quot;TWENTY&quot;, 0], [&quot;ONE HUNDRED&quot;, 0]])</code>应返回<code>{status: &quot;CLOSED&quot;, change: [[&quot;PENNY&quot;, 0.5], [&quot;NICKEL&quot;, 0], [&quot;DIME&quot;, 0], [&quot;QUARTER&quot;, 0], [&quot;ONE&quot;, 0], [&quot;FIVE&quot;, 0], [&quot;TEN&quot;, 0], [&quot;TWENTY&quot;, 0], [&quot;ONE HUNDRED&quot;, 0]]}</code> 。' - text: '<code>checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])</code>应返回<code>{status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}</code>。'
testString: 'assert.deepEqual(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]), {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]});' testString: 'assert.deepEqual(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]), {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]});'
``` ```
@ -60,7 +69,6 @@ function checkCashRegister(price, cash, cid) {
// ["ONE HUNDRED", 100]] // ["ONE HUNDRED", 100]]
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]); checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
``` ```
</div> </div>
@ -72,8 +80,58 @@ checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], [
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```js
// solution required var denom = [
{ name: 'ONE HUNDRED', val: 100},
{ name: 'TWENTY', val: 20},
{ name: 'TEN', val: 10},
{ name: 'FIVE', val: 5},
{ name: 'ONE', val: 1},
{ name: 'QUARTER', val: 0.25},
{ name: 'DIME', val: 0.1},
{ name: 'NICKEL', val: 0.05},
{ name: 'PENNY', val: 0.01}
];
function checkCashRegister(price, cash, cid) {
var output = {status: null, change: []};
var change = cash - price;
var register = cid.reduce(function(acc, curr) {
acc.total += curr[1];
acc[curr[0]] = curr[1];
return acc;
}, {total: 0});
if(register.total === change) {
output.status = 'CLOSED';
output.change = cid;
return output;
}
if(register.total < change) {
output.status = 'INSUFFICIENT_FUNDS';
return output;
}
var change_arr = denom.reduce(function(acc, curr) {
var value = 0;
while(register[curr.name] > 0 && change >= curr.val) {
change -= curr.val;
register[curr.name] -= curr.val;
value += curr.val;
change = Math.round(change * 100) / 100;
}
if(value > 0) {
acc.push([ curr.name, value ]);
}
return acc;
}, []);
if(change_arr.length < 1 || change > 0) {
output.status = 'INSUFFICIENT_FUNDS';
return output;
}
output.status = 'OPEN';
output.change = change_arr;
return output;
}
``` ```
/section> </section>

View File

@ -3,15 +3,22 @@ id: aaa48de84e1ecc7c742e1124
title: Palindrome Checker title: Palindrome Checker
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16004
localeTitle: 回文检查 localeTitle: 回文检查
--- ---
## Description ## Description
<section id="description">如果给定的字符串是回文,则返回<code>true</code> 。否则,返回<code>false</code><dfn>回文</dfn>是一个单词或句子,其拼写方式与前后相同,忽略标点符号,大小写和间距。 <strong>注意</strong> <br>您需要删除<strong>所有非字母数字字符</strong> (标点符号,空格和符号)并将所有内容转换为相同的大小写(小写或大写)以检查回文。我们会通过字符串具有不同的格式,如<code>&quot;racecar&quot;</code> <code>&quot;RaceCar&quot;</code><code>&quot;race CAR&quot;</code>等等。我们还将传递带有特殊符号的字符串,例如<code>&quot;2A3*3a2&quot;</code> <code>&quot;2A3 3a2&quot;</code><code>&quot;2_A3*3#A2&quot;</code> 。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section> <section id='description'>
如果给定的一个字符串是回文,那么返回<code>true</code>,否则返回<code>false</code>
<dfn>palindrome回文</dfn>,指在忽略标点符号、大小写和空格的前提下,正着读和反着读一模一样。
<strong>注意:</strong><br>检查回文时,你需要先除去<strong>所有非字母数字的字符</strong>(标点、空格和符号)并且将所有字符转换成字母大写或字母小写。
我们将会传入不同格式的字符串,例如:<code>"racecar"</code><code>"RaceCar"</code><code>"race CAR"</code>等等。
我们也会传入一些包含特殊符号的字符串,例如<code>"2A3*3a2"</code><code>"2A3 3a2"</code><code>"2_A3*3#A2"</code>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,31 +26,31 @@ localeTitle: 回文检查
```yml ```yml
tests: tests:
- text: <code>palindrome(&quot;eye&quot;)</code>应该返回一个布尔值。 - text: "<code>palindrome('eye')</code>应该返回一个布尔值。"
testString: assert(typeof palindrome("eye") === "boolean"); testString: assert(typeof palindrome("eye") === "boolean");
- text: <code>palindrome(&quot;eye&quot;)</code>应该返回true。 - text: "<code>palindrome('eye')</code>应该返回 true。"
testString: assert(palindrome("eye") === true); testString: assert(palindrome("eye") === true);
- text: <code>palindrome(&quot;_eye&quot;)</code>应该返回true。 - text: "<code>palindrome('_eye')</code>应该返回 true。"
testString: assert(palindrome("_eye") === true); testString: assert(palindrome("_eye") === true);
- text: <code>palindrome(&quot;race car&quot;)</code>应该返回true。 - text: "<code>palindrome('race car')</code>应该返回 true。"
testString: assert(palindrome("race car") === true); testString: assert(palindrome("race car") === true);
- text: <code>palindrome(&quot;not a palindrome&quot;)</code>应该返回false。 - text: "<code>palindrome('not a palindrome')</code>应该返回 false。"
testString: assert(palindrome("not a palindrome") === false); testString: assert(palindrome("not a palindrome") === false);
- text: '<code>palindrome(&quot;A man, a plan, a canal. Panama&quot;)</code>应该回归真实。' - text: "<code>palindrome('A man, a plan, a canal. Panama')</code>应该返回 true。"
testString: assert(palindrome("A man, a plan, a canal. Panama") === true); testString: assert(palindrome("A man, a plan, a canal. Panama") === true);
- text: <code>palindrome(&quot;never odd or even&quot;)</code>应该返回true。 - text: "<code>palindrome('never odd or even')</code>应该返回 true。"
testString: assert(palindrome("never odd or even") === true); testString: assert(palindrome("never odd or even") === true);
- text: <code>palindrome(&quot;nope&quot;)</code>应该返回false。 - text: "<code>palindrome('nope')</code>应该返回 false。"
testString: assert(palindrome("nope") === false); testString: assert(palindrome("nope") === false);
- text: <code>palindrome(&quot;almostomla&quot;)</code>应该返回false。 - text: "<code>palindrome('almostomla')</code>应该返回 false。"
testString: assert(palindrome("almostomla") === false); testString: assert(palindrome("almostomla") === false);
- text: '<code>palindrome(&quot;My age is 0, 0 si ega ym.&quot;)</code>应该返回true。' - text: "<code>palindrome('My age is 0, 0 si ega ym.')</code>应该返回 true。"
testString: assert(palindrome("My age is 0, 0 si ega ym.") === true); testString: assert(palindrome("My age is 0, 0 si ega ym.") === true);
- text: <code>palindrome(&quot;1 eye for of 1 eye.&quot;)</code>应该返回假。 - text: "<code>palindrome('1 eye for of 1 eye.')</code>应该返回 false。"
testString: assert(palindrome("1 eye for of 1 eye.") === false); testString: assert(palindrome("1 eye for of 1 eye.") === false);
- text: '<code>palindrome(&quot;0_0 (: /-\ :) 0-0&quot;)</code>应该返回true。' - text: '<code>palindrome("0_0 (: /-\ :) 0-0")</code>应该返回 true。'
testString: 'assert(palindrome("0_0 (: /-\ :) 0-0") === true);' testString: 'assert(palindrome("0_0 (: /-\ :) 0-0") === true);'
- text: <code>palindrome(&quot;five|\_/|four&quot;)</code>应该返回false。 - text: "<code>palindrome('five|\_/|four')</code>应该返回 false。"
testString: assert(palindrome("five|\_/|four") === false); testString: assert(palindrome("five|\_/|four") === false);
``` ```
@ -64,7 +71,6 @@ function palindrome(str) {
palindrome("eye"); palindrome("eye");
``` ```
</div> </div>
@ -76,8 +82,17 @@ palindrome("eye");
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```js
// solution required function palindrome(str) {
var string = str.toLowerCase().split(/[^A-Za-z0-9]/gi).join('');
var aux = string.split('');
if (aux.join('') === aux.reverse().join('')){
return true;
}
return false;
}
``` ```
/section> </section>

View File

@ -3,15 +3,19 @@ id: a7f4d8f2483413a6ce226cac
title: Roman Numeral Converter title: Roman Numeral Converter
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16044
localeTitle: 罗马数字转换器 localeTitle: 罗马数字转换器
--- ---
## Description ## Description
<section id="description">将给定数字转换为罗马数字。所有<a href="http://www.mathsisfun.com/roman-numerals.html" target="_blank">罗马数字</a>答案都应以大写字母提供。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
把传入的数字转变为罗马数字。
转换后的<a href="http://www.mathsisfun.com/roman-numerals.html" target="_blank">罗马数字</a>字母必须都是大写。
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,57 +23,57 @@ localeTitle: 罗马数字转换器
```yml ```yml
tests: tests:
- text: <code>convertToRoman(2)</code>应该返回“II”。 - text: "<code>convertToRoman(2)</code>应该返回 'II'。"
testString: assert.deepEqual(convertToRoman(2), "II"); testString: assert.deepEqual(convertToRoman(2), "II");
- text: <code>convertToRoman(3)</code>应该返回III”。 - text: "<code>convertToRoman(3)</code>应该返回 'III'。"
testString: assert.deepEqual(convertToRoman(3), "III"); testString: assert.deepEqual(convertToRoman(3), "III");
- text: <code>convertToRoman(4)</code>应该返回“IV”。 - text: "<code>convertToRoman(4)</code>应该返回 'IV'。"
testString: assert.deepEqual(convertToRoman(4), "IV"); testString: assert.deepEqual(convertToRoman(4), "IV");
- text: <code>convertToRoman(5)</code>应该返回“V”。 - text: "<code>convertToRoman(5)</code>应该返回 'V'。"
testString: assert.deepEqual(convertToRoman(5), "V"); testString: assert.deepEqual(convertToRoman(5), "V");
- text: <code>convertToRoman(9)</code>应该返回“IX”。 - text: "<code>convertToRoman(9)</code>应该返回 'IX'。"
testString: assert.deepEqual(convertToRoman(9), "IX"); testString: assert.deepEqual(convertToRoman(9), "IX");
- text: <code>convertToRoman(12)</code>应返回XII”。 - text: "<code>convertToRoman(12)</code>应返回 'XII'。"
testString: assert.deepEqual(convertToRoman(12), "XII"); testString: assert.deepEqual(convertToRoman(12), "XII");
- text: <code>convertToRoman(16)</code>应返回XVI”。 - text: "<code>convertToRoman(16)</code>应返回 'XVI'。"
testString: assert.deepEqual(convertToRoman(16), "XVI"); testString: assert.deepEqual(convertToRoman(16), "XVI");
- text: <code>convertToRoman(29)</code>应该返回XXIX”。 - text: "<code>convertToRoman(29)</code>应该返回 'XXIX'。"
testString: assert.deepEqual(convertToRoman(29), "XXIX"); testString: assert.deepEqual(convertToRoman(29), "XXIX");
- text: <code>convertToRoman(44)</code>应该返回XLIV”。 - text: "<code>convertToRoman(44)</code>应该返回 'XLIV'。"
testString: assert.deepEqual(convertToRoman(44), "XLIV"); testString: assert.deepEqual(convertToRoman(44), "XLIV");
- text: <code>convertToRoman(45)</code>应该返回XLV - text: "<code>convertToRoman(45)</code>应该返回 'XLV'。"
testString: assert.deepEqual(convertToRoman(45), "XLV"); testString: assert.deepEqual(convertToRoman(45), "XLV");
- text: <code>convertToRoman(68)</code>应返回LXVIII - text: "<code>convertToRoman(68)</code>应返回 'LXVIII'。"
testString: assert.deepEqual(convertToRoman(68), "LXVIII"); testString: assert.deepEqual(convertToRoman(68), "LXVIII");
- text: <code>convertToRoman(83)</code>应返回LXXXIII - text: "<code>convertToRoman(83)</code>应返回 'LXXXIII'。"
testString: assert.deepEqual(convertToRoman(83), "LXXXIII"); testString: assert.deepEqual(convertToRoman(83), "LXXXIII");
- text: <code>convertToRoman(97)</code>应该返回XCVII - text: "<code>convertToRoman(97)</code>应该返回 'XCVII'。"
testString: assert.deepEqual(convertToRoman(97), "XCVII"); testString: assert.deepEqual(convertToRoman(97), "XCVII");
- text: <code>convertToRoman(99)</code>应返回XCIX - text: "<code>convertToRoman(99)</code>应返回 'XCIX'。"
testString: assert.deepEqual(convertToRoman(99), "XCIX"); testString: assert.deepEqual(convertToRoman(99), "XCIX");
- text: <code>convertToRoman(400)</code>应返回“CD” - text: "<code>convertToRoman(400)</code>应返回 'CD'。"
testString: assert.deepEqual(convertToRoman(400), "CD"); testString: assert.deepEqual(convertToRoman(400), "CD");
- text: <code>convertToRoman(500)</code>应返回“D” - text: "<code>convertToRoman(500)</code>应返回 'D'。"
testString: assert.deepEqual(convertToRoman(500), "D"); testString: assert.deepEqual(convertToRoman(500), "D");
- text: <code>convertToRoman(501)</code>应返回“DI” - text: "<code>convertToRoman(501)</code>应返回 'DI'。"
testString: assert.deepEqual(convertToRoman(501), "DI"); testString: assert.deepEqual(convertToRoman(501), "DI");
- text: <code>convertToRoman(649)</code>应返回DCXLIX - text: "<code>convertToRoman(649)</code>应返回 'DCXLIX'。"
testString: assert.deepEqual(convertToRoman(649), "DCXLIX"); testString: assert.deepEqual(convertToRoman(649), "DCXLIX");
- text: <code>convertToRoman(798)</code>应返回DCCXCVIII - text: "<code>convertToRoman(798)</code>应返回 'DCCXCVIII'。"
testString: assert.deepEqual(convertToRoman(798), "DCCXCVIII"); testString: assert.deepEqual(convertToRoman(798), "DCCXCVIII");
- text: <code>convertToRoman(891)</code>应返回DCCCXCI - text: "<code>convertToRoman(891)</code>应返回 'DCCCXCI'。"
testString: assert.deepEqual(convertToRoman(891), "DCCCXCI"); testString: assert.deepEqual(convertToRoman(891), "DCCCXCI");
- text: <code>convertToRoman(1000)</code>应该返回“M” - text: "<code>convertToRoman(1000)</code>应该返回 'M'。"
testString: assert.deepEqual(convertToRoman(1000), "M"); testString: assert.deepEqual(convertToRoman(1000), "M");
- text: <code>convertToRoman(1004)</code>应返回MIV - text: "<code>convertToRoman(1004)</code>应返回 'MIV'。"
testString: assert.deepEqual(convertToRoman(1004), "MIV"); testString: assert.deepEqual(convertToRoman(1004), "MIV");
- text: <code>convertToRoman(1006)</code>应返回MVI - text: "<code>convertToRoman(1006)</code>应返回 'MVI'。"
testString: assert.deepEqual(convertToRoman(1006), "MVI"); testString: assert.deepEqual(convertToRoman(1006), "MVI");
- text: <code>convertToRoman(1023)</code>应返回MXXIII - text: "<code>convertToRoman(1023)</code>应返回 'MXXIII'。"
testString: assert.deepEqual(convertToRoman(1023), "MXXIII"); testString: assert.deepEqual(convertToRoman(1023), "MXXIII");
- text: <code>convertToRoman(2014)</code>应返回MMXIV - text: "<code>convertToRoman(2014)</code>应返回 'MMXIV'。"
testString: assert.deepEqual(convertToRoman(2014), "MMXIV"); testString: assert.deepEqual(convertToRoman(2014), "MMXIV");
- text: <code>convertToRoman(3999)</code>应返回MMMCMXCIX - text: "<code>convertToRoman(3999)</code>应返回 'MMMCMXCIX'。"
testString: assert.deepEqual(convertToRoman(3999), "MMMCMXCIX"); testString: assert.deepEqual(convertToRoman(3999), "MMMCMXCIX");
``` ```
@ -87,7 +91,6 @@ function convertToRoman(num) {
} }
convertToRoman(36); convertToRoman(36);
``` ```
</div> </div>
@ -99,8 +102,20 @@ convertToRoman(36);
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```js
// solution required function convertToRoman(num) {
var ref = [['M', 1000], ['CM', 900], ['D', 500], ['CD', 400], ['C', 100], ['XC', 90], ['L', 50], ['XL', 40], ['X', 10], ['IX', 9], ['V', 5], ['IV', 4], ['I', 1]];
var res = [];
ref.forEach(function(p) {
while (num >= p[1]) {
res.push(p[0]);
num -= p[1];
}
});
return res.join('');
}
``` ```
/section>
</section>

View File

@ -3,15 +3,21 @@ id: aff0395860f5d3034dc0bfc9
title: Telephone Number Validator title: Telephone Number Validator
challengeType: 5 challengeType: 5
isRequired: true isRequired: true
videoUrl: '' forumTopicId: 16090
localeTitle: 电话号码验证器 localeTitle: 电话号码验证器
--- ---
## Description ## Description
<section id="description">如果传递的字符串看起来像有效的美国电话号码,则返回<code>true</code> 。用户可以按照他们选择的方式填写表单字段,只要其具有有效美国号码的格式即可。以下是美国数字的有效格式示例(有关其他变体,请参阅下面的测试): <blockquote> 555-555-5555 <br> (555)555-5555 <br> 555555-5555 <br> 555 555 5555 <br> 5555555555 <br> 1 555 555 5555 </blockquote>对于此挑战,您将看到一个字符串,如<code>800-692-7753</code><code>8oo-six427676;laskdjf</code> 。您的工作是根据上面提供的任何格式组合验证或拒绝美国电话号码。区号是必需的。如果提供了国家/地区代码,则必须确认国家/地区代码为<code>1</code> 。如果字符串是有效的美国电话号码,则返回<code>true</code> ;否则返回<code>false</code> 。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
如果传入的字符串是一个有效的美国电话号码格式,则返回<code>true</code>
只要是有效的美国电话号码的格式,用户可以按照他们的方式填写表单中的电话号码。以下是一些正确的例子(其他格式变形请参考以下例子):
<blockquote>555-555-5555<br>(555)555-5555<br>(555) 555-5555<br>555 555 5555<br>5555555555<br>1 555 555 5555</blockquote>
在这个挑战中,你将会看到例如<code>800-692-7753</code>或者<code>8oo-six427676;laskdjf</code>的号码。你的任务是根据上面不同的格式组合,判断它是否美国号码。区号是必须的。如果提供国家代码,则必须确认国家代码为<code>1</code>。如果这是有效的美国电话就返回<code>true</code>,否则返回<code>false</code>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,59 +25,59 @@ localeTitle: 电话号码验证器
```yml ```yml
tests: tests:
- text: <code>telephoneCheck(&quot;555-555-5555&quot;)</code>应该返回一个布尔值。 - text: "<code>telephoneCheck('555-555-5555')</code>应该返回布尔值。"
testString: assert(typeof telephoneCheck("555-555-5555") === "boolean"); testString: assert(typeof telephoneCheck("555-555-5555") === "boolean");
- text: <code>telephoneCheck(&quot;1 555-555-5555&quot;)</code>应该返回true。 - text: "<code>telephoneCheck('1 555-555-5555')</code>应该返回 true。"
testString: assert(telephoneCheck("1 555-555-5555") === true); testString: assert(telephoneCheck("1 555-555-5555") === true);
- text: <code>telephoneCheck(&quot;1 (555) 555-5555&quot;)</code>应该返回true。 - text: "<code>telephoneCheck('1 (555) 555-5555')</code>应该返回 true。"
testString: assert(telephoneCheck("1 (555) 555-5555") === true); testString: assert(telephoneCheck("1 (555) 555-5555") === true);
- text: <code>telephoneCheck(&quot;5555555555&quot;)</code>应该返回true。 - text: "<code>telephoneCheck('5555555555')</code>应该返回 true。"
testString: assert(telephoneCheck("5555555555") === true); testString: assert(telephoneCheck("5555555555") === true);
- text: <code>telephoneCheck(&quot;555-555-5555&quot;)</code>应该返回true。 - text: "<code>telephoneCheck('555-555-5555')</code>应该返回 true。"
testString: assert(telephoneCheck("555-555-5555") === true); testString: assert(telephoneCheck("555-555-5555") === true);
- text: <code>telephoneCheck(&quot;(555)555-5555&quot;)</code>应该返回true。 - text: "<code>telephoneCheck('(555)555-5555')</code>应该返回 true。"
testString: assert(telephoneCheck("(555)555-5555") === true); testString: assert(telephoneCheck("(555)555-5555") === true);
- text: <code>telephoneCheck(&quot;1(555)555-5555&quot;)</code>应该返回true。 - text: "<code>telephoneCheck('1(555)555-5555')</code>应该返回 true。"
testString: assert(telephoneCheck("1(555)555-5555") === true); testString: assert(telephoneCheck("1(555)555-5555") === true);
- text: <code>telephoneCheck(&quot;555-5555&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('555-5555')</code>应该返回 false。"
testString: assert(telephoneCheck("555-5555") === false); testString: assert(telephoneCheck("555-5555") === false);
- text: <code>telephoneCheck(&quot;5555555&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('5555555')</code>应该返回 false。"
testString: assert(telephoneCheck("5555555") === false); testString: assert(telephoneCheck("5555555") === false);
- text: <code>telephoneCheck(&quot;1 555)555-5555&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('1 555)555-5555')</code>应该返回 false。"
testString: assert(telephoneCheck("1 555)555-5555") === false); testString: assert(telephoneCheck("1 555)555-5555") === false);
- text: <code>telephoneCheck(&quot;1 555 555 5555&quot;)</code>应该返回true。 - text: "<code>telephoneCheck('1 555 555 5555')</code>应该返回 true。"
testString: assert(telephoneCheck("1 555 555 5555") === true); testString: assert(telephoneCheck("1 555 555 5555") === true);
- text: <code>telephoneCheck(&quot;1 456 789 4444&quot;)</code>应该返回true。 - text: "<code>telephoneCheck('1 456 789 4444')</code>应该返回 true。"
testString: assert(telephoneCheck("1 456 789 4444") === true); testString: assert(telephoneCheck("1 456 789 4444") === true);
- text: '<code>telephoneCheck(&quot;123**&amp;!!asdf#&quot;)</code>应该返回false。' - text: "<code>telephoneCheck('123**&!!asdf#')</code>应该返回 false。"
testString: assert(telephoneCheck("123**&!!asdf#") === false); testString: assert(telephoneCheck("123**&!!asdf#") === false);
- text: <code>telephoneCheck(&quot;55555555&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('55555555')</code>应该返回 false。"
testString: assert(telephoneCheck("55555555") === false); testString: assert(telephoneCheck("55555555") === false);
- text: <code>telephoneCheck(&quot;(6054756961)&quot;)</code>应该返回false - text: "<code>telephoneCheck('(6054756961)')</code>应该返回 false。"
testString: assert(telephoneCheck("(6054756961)") === false); testString: assert(telephoneCheck("(6054756961)") === false);
- text: <code>telephoneCheck(&quot;2 (757) 622-7382&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('2 (757) 622-7382')</code>应该返回 false。"
testString: assert(telephoneCheck("2 (757) 622-7382") === false); testString: assert(telephoneCheck("2 (757) 622-7382") === false);
- text: <code>telephoneCheck(&quot;0 (757) 622-7382&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('0 (757) 622-7382')</code>应该返回 false。"
testString: assert(telephoneCheck("0 (757) 622-7382") === false); testString: assert(telephoneCheck("0 (757) 622-7382") === false);
- text: <code>telephoneCheck(&quot;-1 (757) 622-7382&quot;)</code>应该返回false - text: "<code>telephoneCheck('-1 (757) 622-7382')</code>应该返回 false。"
testString: assert(telephoneCheck("-1 (757) 622-7382") === false); testString: assert(telephoneCheck("-1 (757) 622-7382") === false);
- text: <code>telephoneCheck(&quot;2 757 622-7382&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('2 757 622-7382')</code>应该返回 false。"
testString: assert(telephoneCheck("2 757 622-7382") === false); testString: assert(telephoneCheck("2 757 622-7382") === false);
- text: <code>telephoneCheck(&quot;10 (757) 622-7382&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('10 (757) 622-7382')</code>应该返回 false。"
testString: assert(telephoneCheck("10 (757) 622-7382") === false); testString: assert(telephoneCheck("10 (757) 622-7382") === false);
- text: <code>telephoneCheck(&quot;27576227382&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('27576227382')</code>应该返回 false。"
testString: assert(telephoneCheck("27576227382") === false); testString: assert(telephoneCheck("27576227382") === false);
- text: <code>telephoneCheck(&quot;(275)76227382&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('(275)76227382')</code>应该返回 false。"
testString: assert(telephoneCheck("(275)76227382") === false); testString: assert(telephoneCheck("(275)76227382") === false);
- text: <code>telephoneCheck(&quot;2(757)6227382&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('2(757)6227382')</code>应该返回 false。"
testString: assert(telephoneCheck("2(757)6227382") === false); testString: assert(telephoneCheck("2(757)6227382") === false);
- text: <code>telephoneCheck(&quot;2(757)622-7382&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('2(757)622-7382')</code>应该返回 false。"
testString: assert(telephoneCheck("2(757)622-7382") === false); testString: assert(telephoneCheck("2(757)622-7382") === false);
- text: <code>telephoneCheck(&quot;555)-555-5555&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('555)-555-5555')</code>应该返回 false。"
testString: assert(telephoneCheck("555)-555-5555") === false); testString: assert(telephoneCheck("555)-555-5555") === false);
- text: <code>telephoneCheck(&quot;(555-555-5555&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('(555-555-5555')</code>应该返回 false。"
testString: assert(telephoneCheck("(555-555-5555") === false); testString: assert(telephoneCheck("(555-555-5555") === false);
- text: <code>telephoneCheck(&quot;(555)5(55?)-5555&quot;)</code>应该返回false。 - text: "<code>telephoneCheck('(555)5(55?)-5555')</code>应该返回 false。"
testString: assert(telephoneCheck("(555)5(55?)-5555") === false); testString: assert(telephoneCheck("(555)5(55?)-5555") === false);
``` ```
@ -90,7 +96,6 @@ function telephoneCheck(str) {
} }
telephoneCheck("555-555-5555"); telephoneCheck("555-555-5555");
``` ```
</div> </div>
@ -102,8 +107,15 @@ telephoneCheck("555-555-5555");
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```js
// solution required var re = /^([+]?1[\s]?)?((?:[(](?:[2-9]1[02-9]|[2-9][02-8][0-9])[)][\s]?)|(?:(?:[2-9]1[02-9]|[2-9][02-8][0-9])[\s.-]?)){1}([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2}[\s.-]?){1}([0-9]{4}){1}$/;
function telephoneCheck(str) {
return re.test(str);
}
telephoneCheck("555-555-5555");
``` ```
/section> </section>