From 193d13e7bab7ecb2f9264032eeb109d6efd935eb Mon Sep 17 00:00:00 2001 From: ZhichengChen Date: Mon, 7 Sep 2020 16:17:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(i18n):=20update=20Chinese=20translation=20o?= =?UTF-8?q?f=20JavaScript=20Algorithms=20and=20Da=E2=80=A6=20(#38494)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Zhicheng Chen Co-authored-by: S1ngS1ng --- .../caesars-cipher.chinese.md | 50 ++++++++--- .../cash-register.chinese.md | 82 ++++++++++++++++--- .../palindrome-checker.chinese.md | 55 ++++++++----- .../roman-numeral-converter.chinese.md | 79 ++++++++++-------- .../telephone-number-validator.chinese.md | 78 ++++++++++-------- 5 files changed, 237 insertions(+), 107 deletions(-) diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/caesars-cipher.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/caesars-cipher.chinese.md index 1eebec07f3..c8a02e0603 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/caesars-cipher.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/caesars-cipher.chinese.md @@ -3,15 +3,21 @@ id: 56533eb9ac21ba0edf2244e2 title: Caesars Cipher challengeType: 5 isRequired: true -videoUrl: '' +forumTopicId: 16003 localeTitle: 凯撒密码 --- ## Description -
最简单和最广为人知的密码之一Caesar cipher ,也称为shift cipher 。在shift cipher中,字母的含义被移动一些设定量。一种常见的现代用途是ROT13密码,其中字母的值移动了13个位置。因此'A'''N','B'''O'等等。编写一个函数,它将ROT13编码的字符串作为输入并返回一个已解码的字符串。所有字母都是大写的。不要转换任何非字母字符(即空格,标点符号),但要传递它们。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+凯撒密码是最简单和最广为人知的密码之一,也被称为移位密码。在移位密码中,明文中的字母通过按照一个固定数目进行偏移后被替换成新的字母。 +ROT13 是一个被广泛使用的编码技术,明文中的所有字母都被移动 13 位。因此,'A' ↔ 'N', 'B' ↔ 'O' 等等。 +请编写一个函数,用于解码一个被 ROT13 编码的字符串,然后返回解码后的结果。 +所有解码后的字母都必须为字母大写。请不要解码非字母的字符(例如,空格、标点符号),但你需要在结果中保留它们。 +
## Instructions -
+
+
## Tests @@ -19,13 +25,13 @@ localeTitle: 凯撒密码 ```yml tests: - - text: rot13("SERR PBQR PNZC")应解码为FREE CODE CAMP + - text: "rot13('SERR PBQR PNZC')应解码为FREE CODE CAMP。" testString: assert(rot13("SERR PBQR PNZC") === "FREE CODE CAMP"); - - text: rot13("SERR CVMMN!")应该解码为FREE PIZZA! rot13("SERR CVMMN!") FREE PIZZA! + - text: "rot13('SERR CVMMN!')应解码为FREE PIZZA!。" testString: assert(rot13("SERR CVMMN!") === "FREE PIZZA!"); - - text: rot13("SERR YBIR?")应解码为FREE LOVE? + - text: "rot13('SERR YBIR?')应解码为FREE LOVE?。" testString: assert(rot13("SERR YBIR?") === "FREE LOVE?"); - - text: rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.")应该在rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.")解码到THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. + - text: "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 rot13("SERR PBQR PNZC"); - ``` @@ -57,8 +62,33 @@ rot13("SERR PBQR PNZC"); ## Solution
+ ```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> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register.chinese.md index d31d857020..210dce1676 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register.chinese.md @@ -3,15 +3,24 @@ id: aa2e6f85cab2ab736c9a9b24 title: Cash Register isRequired: true challengeType: 5 -videoUrl: '' +forumTopicId: 16012 localeTitle: 收银机 --- ## Description -
设计一个收银抽屉功能checkCashRegister() ,它接受购买价格作为第一个参数( price ),支付作为第二个参数( cash ),以及现金抽屉( cid )作为第三个参数。 cid是列出可用货币的2D数组。 checkCashRegister()函数应始终返回带有status键和change键的对象。返回{status: "INSUFFICIENT_FUNDS", change: []}如果出现的现金少于到期的更改,或者如果您无法返回确切的更改。返回{status: "CLOSED", change: [...]}使用cash-in-drawer作为密钥change的值,如果它等于更改到期。否则,返回{status: "OPEN", change: [...]} ,以硬币和账单中的更改到期,按从最高到最低的顺序排序,作为change密钥的值。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
货币单位
一分钱 0.01美元(PENNY)
0.05美元(NICKEL)
十分钱 0.1美元(DIME)
25美分硬币 0.25美元(季)
美元 1美元(美元)
五美元 5美元(五)
十美元 10美元(10日)
二十美元 20美元(二十美元)
一百元 100美元(一百)
+
+编写一个用于收银机的函数checkCashRegister(),传入售价为第一个参数(price)、支付金额为第二个参数(cash)、收银机內的金额为第三个参数(cid)。 +cid是包含货币面值的二维数组。 +函数checkCashRegister()必须返回含有status键值和change键值的对象。 +如果收银机內的金额少于应找回的零钱数,或者你无法返回确切的数目时,返回{status: "INSUFFICIENT_FUNDS", change: []}。 +如果收银机內的金额等于应找回的零钱数,返回{status: "CLOSED", change: [...]},其中change键值是收银机內的金额。 +否则,返回{status: "OPEN", change: [...]},其中change键值是应找回的零钱数,并且它的面值由高到低排序。 +
货币单位面值
Penny$0.01 (PENNY)
Nickel$0.05 (NICKEL)
Dime$0.1 (DIME)
Quarter$0.25 (QUARTER)
Dollar$1 (DOLLAR)
Five Dollars$5 (FIVE)
Ten Dollars$10 (TEN)
Twenty Dollars$20 (TWENTY)
One-hundred Dollars$100 (ONE HUNDRED)
+
## Instructions -
+
+
## Tests @@ -19,17 +28,17 @@ localeTitle: 收银机 ```yml tests: - - text: '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]])应该返回一个物体。' + - text: "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]])应该返回一个对象。" 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: '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: '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: '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: '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: '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: '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: '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: '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: '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]]} 。' + - text: '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]] 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]]); - ``` @@ -72,8 +80,58 @@ checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], [ ## Solution
+ ```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> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker.chinese.md index 7531a9766c..aa2e1178ec 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker.chinese.md @@ -3,15 +3,22 @@ id: aaa48de84e1ecc7c742e1124 title: Palindrome Checker isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 回文检查 +forumTopicId: 16004 +localeTitle: 回文检查器 --- ## Description -
如果给定的字符串是回文,则返回true 。否则,返回false回文是一个单词或句子,其拼写方式与前后相同,忽略标点符号,大小写和间距。 注意
您需要删除所有非字母数字字符 (标点符号,空格和符号)并将所有内容转换为相同的大小写(小写或大写)以检查回文。我们会通过字符串具有不同的格式,如"racecar""RaceCar""race CAR"等等。我们还将传递带有特殊符号的字符串,例如"2A3*3a2""2A3 3a2""2_A3*3#A2" 。如果卡住,请记得使用Read-Search-Ask 。编写自己的代码。
+
+如果给定的一个字符串是回文,那么返回true,否则返回false。 +palindrome(回文),指在忽略标点符号、大小写和空格的前提下,正着读和反着读一模一样。 +注意:
检查回文时,你需要先除去所有非字母数字的字符(标点、空格和符号)并且将所有字符转换成字母大写或字母小写。 +我们将会传入不同格式的字符串,例如:"racecar""RaceCar""race CAR"等等。 +我们也会传入一些包含特殊符号的字符串,例如"2A3*3a2""2A3 3a2""2_A3*3#A2"。 +
## Instructions -
+
+
## Tests @@ -19,31 +26,31 @@ localeTitle: 回文检查 ```yml tests: - - text: palindrome("eye")应该返回一个布尔值。 + - text: "palindrome('eye')应该返回一个布尔值。" testString: assert(typeof palindrome("eye") === "boolean"); - - text: palindrome("eye")应该返回true。 + - text: "palindrome('eye')应该返回 true。" testString: assert(palindrome("eye") === true); - - text: palindrome("_eye")应该返回true。 + - text: "palindrome('_eye')应该返回 true。" testString: assert(palindrome("_eye") === true); - - text: palindrome("race car")应该返回true。 + - text: "palindrome('race car')应该返回 true。" testString: assert(palindrome("race car") === true); - - text: palindrome("not a palindrome")应该返回false。 + - text: "palindrome('not a palindrome')应该返回 false。" testString: assert(palindrome("not a palindrome") === false); - - text: 'palindrome("A man, a plan, a canal. Panama")应该回归真实。' + - text: "palindrome('A man, a plan, a canal. Panama')应该返回 true。" testString: assert(palindrome("A man, a plan, a canal. Panama") === true); - - text: palindrome("never odd or even")应该返回true。 + - text: "palindrome('never odd or even')应该返回 true。" testString: assert(palindrome("never odd or even") === true); - - text: palindrome("nope")应该返回false。 + - text: "palindrome('nope')应该返回 false。" testString: assert(palindrome("nope") === false); - - text: palindrome("almostomla")应该返回false。 + - text: "palindrome('almostomla')应该返回 false。" testString: assert(palindrome("almostomla") === false); - - text: 'palindrome("My age is 0, 0 si ega ym.")应该返回true。' + - text: "palindrome('My age is 0, 0 si ega ym.')应该返回 true。" testString: assert(palindrome("My age is 0, 0 si ega ym.") === true); - - text: palindrome("1 eye for of 1 eye.")应该返回假。 + - text: "palindrome('1 eye for of 1 eye.')应该返回 false。" testString: assert(palindrome("1 eye for of 1 eye.") === false); - - text: 'palindrome("0_0 (: /-\ :) 0-0")应该返回true。' + - text: 'palindrome("0_0 (: /-\ :) 0-0")应该返回 true。' testString: 'assert(palindrome("0_0 (: /-\ :) 0-0") === true);' - - text: palindrome("five|\_/|four")应该返回false。 + - text: "palindrome('five|\_/|four')应该返回 false。" testString: assert(palindrome("five|\_/|four") === false); ``` @@ -64,7 +71,6 @@ function palindrome(str) { palindrome("eye"); - ``` @@ -76,8 +82,17 @@ palindrome("eye"); ## Solution
+ ```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> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter.chinese.md index 6e268c12c7..1cfb882738 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter.chinese.md @@ -3,15 +3,19 @@ id: a7f4d8f2483413a6ce226cac title: Roman Numeral Converter isRequired: true challengeType: 5 -videoUrl: '' +forumTopicId: 16044 localeTitle: 罗马数字转换器 --- ## Description -
将给定数字转换为罗马数字。所有罗马数字答案都应以大写字母提供。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+把传入的数字转变为罗马数字。 +转换后的罗马数字字母必须都是大写。 +
## Instructions -
+
+
## Tests @@ -19,57 +23,57 @@ localeTitle: 罗马数字转换器 ```yml tests: - - text: convertToRoman(2)应该返回“II”。 + - text: "convertToRoman(2)应该返回 'II'。" testString: assert.deepEqual(convertToRoman(2), "II"); - - text: convertToRoman(3)应该返回“III”。 + - text: "convertToRoman(3)应该返回 'III'。" testString: assert.deepEqual(convertToRoman(3), "III"); - - text: convertToRoman(4)应该返回“IV”。 + - text: "convertToRoman(4)应该返回 'IV'。" testString: assert.deepEqual(convertToRoman(4), "IV"); - - text: convertToRoman(5)应该返回“V”。 + - text: "convertToRoman(5)应该返回 'V'。" testString: assert.deepEqual(convertToRoman(5), "V"); - - text: convertToRoman(9)应该返回“IX”。 + - text: "convertToRoman(9)应该返回 'IX'。" testString: assert.deepEqual(convertToRoman(9), "IX"); - - text: convertToRoman(12)应返回“XII”。 + - text: "convertToRoman(12)应该返回 'XII'。" testString: assert.deepEqual(convertToRoman(12), "XII"); - - text: convertToRoman(16)应返回“XVI”。 + - text: "convertToRoman(16)应该返回 'XVI'。" testString: assert.deepEqual(convertToRoman(16), "XVI"); - - text: convertToRoman(29)应该返回“XXIX”。 + - text: "convertToRoman(29)应该返回 'XXIX'。" testString: assert.deepEqual(convertToRoman(29), "XXIX"); - - text: convertToRoman(44)应该返回“XLIV”。 + - text: "convertToRoman(44)应该返回 'XLIV'。" testString: assert.deepEqual(convertToRoman(44), "XLIV"); - - text: convertToRoman(45)应该返回“XLV” + - text: "convertToRoman(45)应该返回 'XLV'。" testString: assert.deepEqual(convertToRoman(45), "XLV"); - - text: convertToRoman(68)应返回“LXVIII” + - text: "convertToRoman(68)应该返回 'LXVIII'。" testString: assert.deepEqual(convertToRoman(68), "LXVIII"); - - text: convertToRoman(83)应返回“LXXXIII” + - text: "convertToRoman(83)应该返回 'LXXXIII'。" testString: assert.deepEqual(convertToRoman(83), "LXXXIII"); - - text: convertToRoman(97)应该返回“XCVII” + - text: "convertToRoman(97)应该返回 'XCVII'。" testString: assert.deepEqual(convertToRoman(97), "XCVII"); - - text: convertToRoman(99)应返回“XCIX” + - text: "convertToRoman(99)应该返回 'XCIX'。" testString: assert.deepEqual(convertToRoman(99), "XCIX"); - - text: convertToRoman(400)应返回“CD” + - text: "convertToRoman(400)应该返回 'CD'。" testString: assert.deepEqual(convertToRoman(400), "CD"); - - text: convertToRoman(500)应返回“D” + - text: "convertToRoman(500)应该返回 'D'。" testString: assert.deepEqual(convertToRoman(500), "D"); - - text: convertToRoman(501)应返回“DI” + - text: "convertToRoman(501)应该返回 'DI'。" testString: assert.deepEqual(convertToRoman(501), "DI"); - - text: convertToRoman(649)应返回“DCXLIX” + - text: "convertToRoman(649)应该返回 'DCXLIX'。" testString: assert.deepEqual(convertToRoman(649), "DCXLIX"); - - text: convertToRoman(798)应返回“DCCXCVIII” + - text: "convertToRoman(798)应该返回 'DCCXCVIII'。" testString: assert.deepEqual(convertToRoman(798), "DCCXCVIII"); - - text: convertToRoman(891)应返回“DCCCXCI” + - text: "convertToRoman(891)应该返回 'DCCCXCI'。" testString: assert.deepEqual(convertToRoman(891), "DCCCXCI"); - - text: convertToRoman(1000)应该返回“M” + - text: "convertToRoman(1000)应该返回 'M'。" testString: assert.deepEqual(convertToRoman(1000), "M"); - - text: convertToRoman(1004)应返回“MIV” + - text: "convertToRoman(1004)应该返回 'MIV'。" testString: assert.deepEqual(convertToRoman(1004), "MIV"); - - text: convertToRoman(1006)应返回“MVI” + - text: "convertToRoman(1006)应该返回 'MVI'。" testString: assert.deepEqual(convertToRoman(1006), "MVI"); - - text: convertToRoman(1023)应返回“MXXIII” + - text: "convertToRoman(1023)应该返回 'MXXIII'。" testString: assert.deepEqual(convertToRoman(1023), "MXXIII"); - - text: convertToRoman(2014)应返回“MMXIV” + - text: "convertToRoman(2014)应该返回 'MMXIV'。" testString: assert.deepEqual(convertToRoman(2014), "MMXIV"); - - text: convertToRoman(3999)应返回“MMMCMXCIX” + - text: "convertToRoman(3999)应该返回 'MMMCMXCIX'。" testString: assert.deepEqual(convertToRoman(3999), "MMMCMXCIX"); ``` @@ -87,7 +91,6 @@ function convertToRoman(num) { } convertToRoman(36); - ``` @@ -99,8 +102,20 @@ convertToRoman(36); ## Solution
+ ```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> + +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/telephone-number-validator.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/telephone-number-validator.chinese.md index cfd02844c5..f24e5179be 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/telephone-number-validator.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/telephone-number-validator.chinese.md @@ -3,15 +3,21 @@ id: aff0395860f5d3034dc0bfc9 title: Telephone Number Validator challengeType: 5 isRequired: true -videoUrl: '' +forumTopicId: 16090 localeTitle: 电话号码验证器 --- ## Description -
如果传递的字符串看起来像有效的美国电话号码,则返回true 。用户可以按照他们选择的方式填写表单字段,只要其具有有效美国号码的格式即可。以下是美国数字的有效格式示例(有关其他变体,请参阅下面的测试):
555-555-5555
(555)555-5555
(555)555-5555
555 555 5555
5555555555
1 555 555 5555
对于此挑战,您将看到一个字符串,如800-692-77538oo-six427676;laskdjf 。您的工作是根据上面提供的任何格式组合验证或拒绝美国电话号码。区号是必需的。如果提供了国家/地区代码,则必须确认国家/地区代码为1 。如果字符串是有效的美国电话号码,则返回true ;否则返回false 。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+如果传入的字符串是一个有效的美国电话号码格式,则返回true。 +只要是有效的美国电话号码的格式,用户可以按照他们的方式填写表单中的电话号码。以下是一些正确的例子(其他格式变形请参考以下例子): +
555-555-5555
(555)555-5555
(555) 555-5555
555 555 5555
5555555555
1 555 555 5555
+在这个挑战中,你将会看到例如800-692-7753或者8oo-six427676;laskdjf的号码。你的任务是根据上面不同的格式组合,判断它是否美国号码。区号是必须的。如果提供国家代码,则必须确认国家代码为1。如果这是有效的美国电话就返回true,否则返回false。 +
## Instructions -
+
+
## Tests @@ -19,59 +25,59 @@ localeTitle: 电话号码验证器 ```yml tests: - - text: telephoneCheck("555-555-5555")应该返回一个布尔值。 + - text: "telephoneCheck('555-555-5555')应该返回布尔值。" testString: assert(typeof telephoneCheck("555-555-5555") === "boolean"); - - text: telephoneCheck("1 555-555-5555")应该返回true。 + - text: "telephoneCheck('1 555-555-5555')应该返回 true。" testString: assert(telephoneCheck("1 555-555-5555") === true); - - text: telephoneCheck("1 (555) 555-5555")应该返回true。 + - text: "telephoneCheck('1 (555) 555-5555')应该返回 true。" testString: assert(telephoneCheck("1 (555) 555-5555") === true); - - text: telephoneCheck("5555555555")应该返回true。 + - text: "telephoneCheck('5555555555')应该返回 true。" testString: assert(telephoneCheck("5555555555") === true); - - text: telephoneCheck("555-555-5555")应该返回true。 + - text: "telephoneCheck('555-555-5555')应该返回 true。" testString: assert(telephoneCheck("555-555-5555") === true); - - text: telephoneCheck("(555)555-5555")应该返回true。 + - text: "telephoneCheck('(555)555-5555')应该返回 true。" testString: assert(telephoneCheck("(555)555-5555") === true); - - text: telephoneCheck("1(555)555-5555")应该返回true。 + - text: "telephoneCheck('1(555)555-5555')应该返回 true。" testString: assert(telephoneCheck("1(555)555-5555") === true); - - text: telephoneCheck("555-5555")应该返回false。 + - text: "telephoneCheck('555-5555')应该返回 false。" testString: assert(telephoneCheck("555-5555") === false); - - text: telephoneCheck("5555555")应该返回false。 + - text: "telephoneCheck('5555555')应该返回 false。" testString: assert(telephoneCheck("5555555") === false); - - text: telephoneCheck("1 555)555-5555")应该返回false。 + - text: "telephoneCheck('1 555)555-5555')应该返回 false。" testString: assert(telephoneCheck("1 555)555-5555") === false); - - text: telephoneCheck("1 555 555 5555")应该返回true。 + - text: "telephoneCheck('1 555 555 5555')应该返回 true。" testString: assert(telephoneCheck("1 555 555 5555") === true); - - text: telephoneCheck("1 456 789 4444")应该返回true。 + - text: "telephoneCheck('1 456 789 4444')应该返回 true。" testString: assert(telephoneCheck("1 456 789 4444") === true); - - text: 'telephoneCheck("123**&!!asdf#")应该返回false。' + - text: "telephoneCheck('123**&!!asdf#')应该返回 false。" testString: assert(telephoneCheck("123**&!!asdf#") === false); - - text: telephoneCheck("55555555")应该返回false。 + - text: "telephoneCheck('55555555')应该返回 false。" testString: assert(telephoneCheck("55555555") === false); - - text: telephoneCheck("(6054756961)")应该返回false + - text: "telephoneCheck('(6054756961)')应该返回 false。" testString: assert(telephoneCheck("(6054756961)") === false); - - text: telephoneCheck("2 (757) 622-7382")应该返回false。 + - text: "telephoneCheck('2 (757) 622-7382')应该返回 false。" testString: assert(telephoneCheck("2 (757) 622-7382") === false); - - text: telephoneCheck("0 (757) 622-7382")应该返回false。 + - text: "telephoneCheck('0 (757) 622-7382')应该返回 false。" testString: assert(telephoneCheck("0 (757) 622-7382") === false); - - text: telephoneCheck("-1 (757) 622-7382")应该返回false + - text: "telephoneCheck('-1 (757) 622-7382')应该返回 false。" testString: assert(telephoneCheck("-1 (757) 622-7382") === false); - - text: telephoneCheck("2 757 622-7382")应该返回false。 + - text: "telephoneCheck('2 757 622-7382')应该返回 false。" testString: assert(telephoneCheck("2 757 622-7382") === false); - - text: telephoneCheck("10 (757) 622-7382")应该返回false。 + - text: "telephoneCheck('10 (757) 622-7382')应该返回 false。" testString: assert(telephoneCheck("10 (757) 622-7382") === false); - - text: telephoneCheck("27576227382")应该返回false。 + - text: "telephoneCheck('27576227382')应该返回 false。" testString: assert(telephoneCheck("27576227382") === false); - - text: telephoneCheck("(275)76227382")应该返回false。 + - text: "telephoneCheck('(275)76227382')应该返回 false。" testString: assert(telephoneCheck("(275)76227382") === false); - - text: telephoneCheck("2(757)6227382")应该返回false。 + - text: "telephoneCheck('2(757)6227382')应该返回 false。" testString: assert(telephoneCheck("2(757)6227382") === false); - - text: telephoneCheck("2(757)622-7382")应该返回false。 + - text: "telephoneCheck('2(757)622-7382')应该返回 false。" testString: assert(telephoneCheck("2(757)622-7382") === false); - - text: telephoneCheck("555)-555-5555")应该返回false。 + - text: "telephoneCheck('555)-555-5555')应该返回 false。" testString: assert(telephoneCheck("555)-555-5555") === false); - - text: telephoneCheck("(555-555-5555")应该返回false。 + - text: "telephoneCheck('(555-555-5555')应该返回 false。" testString: assert(telephoneCheck("(555-555-5555") === false); - - text: telephoneCheck("(555)5(55?)-5555")应该返回false。 + - text: "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"); - ``` @@ -102,8 +107,15 @@ telephoneCheck("555-555-5555"); ## Solution
+ ```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> +