diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional.chinese.md index 799348b62b..909f5bb354 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional.chinese.md @@ -3,15 +3,23 @@ id: a97fd23d9b809dac9921074f title: Arguments Optional isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 参数可选 +forumTopicId: 14271 +localeTitle: 可选参数 --- ## Description -
创建一个将两个参数相加的函数。如果只提供了一个参数,则返回一个需要一个参数并返回总和的函数。例如, addTogether(2, 3)应返回5addTogether(2)应返回一个函数。使用单个参数调用此返回函数将返回总和: var sumTwoAnd = addTogether(2); sumTwoAnd(3)返回5 。如果任一参数不是有效数字,则返回undefined。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+创建一个将两个参数相加的函数。如果只传入了一个参数,则返回一个函数,需要传入一个参数并返回总和。 +比如,addTogether(2, 3)应该返回5。而addTogether(2)应该返回一个函数。 +调用这个返回的函数,传入一个值,返回总和: +var sumTwoAnd = addTogether(2); +sumTwoAnd(3)此时应返回5。 +只要其中任何一个参数不是数字,那就应返回undefined。 +
## Instructions -
+
+
## Tests @@ -45,7 +53,6 @@ function addTogether() { } addTogether(2,3); - ``` @@ -57,8 +64,21 @@ addTogether(2,3); ## Solution
+ ```js -// solution required +function addTogether() { + var a = arguments[0]; + if (toString.call(a) !== '[object Number]') return; + if (arguments.length === 1) { + return function(b) { + if (toString.call(b) !== '[object Number]') return; + return a + b; + }; + } + var b = arguments[1]; + if (toString.call(b) !== '[object Number]') return; + return a + arguments[1]; +} ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents.chinese.md index c5347f44a0..1206fc01cd 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/binary-agents.chinese.md @@ -3,15 +3,20 @@ id: a8d97bd4c764e91f9d2bda01 title: Binary Agents isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 二元代理商 +forumTopicId: 14273 +localeTitle: 二进制转化 --- ## Description -
返回传递的二进制字符串的英文翻译句子。二进制字符串将以空格分隔。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+ +
+写一个函数,把输入的二进制字符串转换成英文句子。 +二进制字符串将以空格分隔。 +
## Instructions -
+
+
## Tests @@ -19,9 +24,9 @@ localeTitle: 二元代理商 ```yml tests: - - text: binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111")应该返回“不是篝火有趣!?” + - text: "binaryAgent('01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111')应该返回 'Aren't bonfires fun!?'。" testString: assert.deepEqual(binaryAgent('01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111'), "Aren't bonfires fun!?"); - - text: binaryAgent("01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001")应返回“我爱FreeCodeCamp!” + - text: "binaryAgent('01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001')应该返回 'I love FreeCodeCamp!'。" testString: assert.deepEqual(binaryAgent('01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001'), "I love FreeCodeCamp!"); ``` @@ -39,7 +44,6 @@ function binaryAgent(str) { } binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"); - ``` @@ -51,8 +55,11 @@ binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 0110 ## Solution
+ ```js -// solution required +function binaryAgent(str) { + return str.split(' ').map(function(s) { return parseInt(s, 2); }).map(function(b) { return String.fromCharCode(b);}).join(''); +} ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/convert-html-entities.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/convert-html-entities.chinese.md index b20ba74c91..483e300f38 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/convert-html-entities.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/convert-html-entities.chinese.md @@ -3,15 +3,17 @@ id: a6b0bb188d873cb2c8729495 title: Convert HTML Entities isRequired: true challengeType: 5 -videoUrl: '' localeTitle: 转换HTML实体 --- ## Description -
将字符串中的字符&<>" (双引号)和' (撇号)转换为相应的HTML实体。如果卡住,请记住使用Read-Search-Ask 。尝试配对程序。写下你的自己的代码。
+
+在这道题目中,我们需要写一个转换 HTML entity 的函数。需要转换的 HTML entity 有&<>"(双引号)和'(单引号)。 +
## Instructions -
+
+
## Tests @@ -19,19 +21,19 @@ localeTitle: 转换HTML实体 ```yml tests: - - text: convertHTML("Dolce & Gabbana")应该返回Dolce &​amp; Gabbana 。 + - text: "convertHTML('Dolce & Gabbana')应该返回Dolce &​amp; Gabbana。" testString: assert.match(convertHTML("Dolce & Gabbana"), /Dolce & Gabbana/); - - text: convertHTML("Hamburgers < Pizza < Tacos")应该返回Hamburgers &​lt; Pizza &​lt; Tacos 。 + - text: "convertHTML('Hamburgers < Pizza < Tacos')应该返回Hamburgers &​lt; Pizza &​lt; Tacos。" testString: assert.match(convertHTML("Hamburgers < Pizza < Tacos"), /Hamburgers < Pizza < Tacos/); - - text: convertHTML("Sixty > twelve")应返回Sixty &​gt; twelve 。 + - text: "convertHTML('Sixty > twelve')应该返回Sixty &​gt; twelve。" testString: assert.match(convertHTML("Sixty > twelve"), /Sixty > twelve/); - - text: 'convertHTML('Stuff in "quotation marks"')应该convertHTML('Stuff in "quotation marks"')返回Stuff in &​quot;quotation marks&​quot; 。' + - text: "convertHTML('Stuff in \"quotation marks\"')应该返回Stuff in &​quot;quotation marks&​quot;。" testString: assert.match(convertHTML('Stuff in "quotation marks"'), /Stuff in "quotation marks"/); - - text: 'convertHTML("Schindler's List")应该返回Schindler&​apos;s List 。' + - text: "convertHTML('Schindler's List')应该返回Schindler&​apos;s List。" testString: assert.match(convertHTML("Schindler's List"), /Schindler's List/); - - text: convertHTML("<>")应返回&​lt;&​gt; 。 + - text: "convertHTML('<>')应该返回&​lt;&​gt;。" testString: assert.match(convertHTML('<>'), /<>/); - - text: convertHTML("abc")应该返回abc 。 + - text: "convertHTML('abc')应该返回abc。" testString: assert.strictEqual(convertHTML('abc'), 'abc'); ``` @@ -50,7 +52,6 @@ function convertHTML(str) { } convertHTML("Dolce & Gabbana"); - ``` @@ -62,8 +63,19 @@ convertHTML("Dolce & Gabbana"); ## Solution
+ ```js -// solution required +var MAP = { '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": '''}; + +function convertHTML(str) { + return str.replace(/[&<>"']/g, function(c) { + return MAP[c]; + }); +} ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/diff-two-arrays.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/diff-two-arrays.chinese.md index 132da13897..07126cb22f 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/diff-two-arrays.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/diff-two-arrays.chinese.md @@ -3,15 +3,19 @@ id: a5de63ebea8dbee56860f4f2 title: Diff Two Arrays isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 差分两个阵列 +forumTopicId: 16008 +localeTitle: 区分两个数组 --- ## Description -
比较两个数组并返回一个新数组,其中只有在两个给定数组中的一个中找到的任何项,但不能同时返回两个数组。换句话说,返回两个数组的对称差异。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。 注意
您可以按任何顺序返回包含其元素的数组。
+
+在这道题目中,我们需要写一个函数,比较两个数组,返回一个新的数组。这个新数组需要包含传入的两个数组所有元素中,仅在其中一个数组里出现的元素。如果某个元素同时出现在两个数组中,则不应包含在返回的数组里。换言之,我们需要返回这两个数组的对称差。 +注意:
返回数组中的元素可任意排序。 +
## Instructions -
+
+
## Tests @@ -19,35 +23,35 @@ localeTitle: 差分两个阵列 ```yml tests: - - text: 'diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])应该返回一个数组。' + - text: diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])应该返回一个数组。 testString: assert(typeof diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) === "object"); - - text: '["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]应该返回["pink wool"] 。' + - text: "['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'], ['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']应该返回['pink wool']。" testString: assert.sameMembers(diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]), ["pink wool"]); - - text: '["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]应该返回一个包含一个项目的数组。' + - text: "['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'], ['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']应该返回一个长度为 1 的数组。" testString: assert(diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]).length === 1); - - text: '["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]应该返回["diorite", "pink wool"] 。' + - text: "['andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'], ['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']应该返回['diorite', 'pink wool']。" testString: assert.sameMembers(diffArray(["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]), ["diorite", "pink wool"]); - - text: '["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]应该返回一个数组有两个项目。' + - text: "['andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'], ['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']应该返回一个长度为 2 的数组。" testString: assert(diffArray(["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]).length === 2); - - text: '["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]应该返回[] 。' + - text: "['andesite', 'grass', 'dirt', 'dead shrub'], ['andesite', 'grass', 'dirt', 'dead shrub']应该返回[]。" testString: assert.sameMembers(diffArray(["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]), []); - - text: '["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]应返回一个空数组。' + - text: "['andesite', 'grass', 'dirt', 'dead shrub'], ['andesite', 'grass', 'dirt', 'dead shrub']应该返回一个空数组。" testString: assert(diffArray(["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]).length === 0); - - text: '[1, 2, 3, 5], [1, 2, 3, 4, 5]应该返回[4] 。' + - text: [1, 2, 3, 5], [1, 2, 3, 4, 5]应该返回[4]。 testString: assert.sameMembers(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]), [4]); - - text: '[1, 2, 3, 5], [1, 2, 3, 4, 5]应该返回一个带有一个项目的数组。' + - text: [1, 2, 3, 5], [1, 2, 3, 4, 5]应该返回一个长度为 1 的数组。 testString: assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1); - - text: '[1, "calf", 3, "piglet"], [1, "calf", 3, 4]应返回["piglet", 4] 。' + - text: "[1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]应该返回['piglet', 4]。" testString: assert.sameMembers(diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]), ["piglet", 4]); - - text: '[1, "calf", 3, "piglet"], [1, "calf", 3, 4]应该返回一个包含两个项目的数组。' + - text: "[1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]应该返回一个长度为 2 的数组。" testString: assert(diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]).length === 2); - - text: '[], ["snuffleupagus", "cookie monster", "elmo"]应该返回["snuffleupagus", "cookie monster", "elmo"] 。' + - text: "[], ['snuffleupagus', 'cookie monster', 'elmo']应该返回['snuffleupagus', 'cookie monster', 'elmo']。" testString: assert.sameMembers(diffArray([], ["snuffleupagus", "cookie monster", "elmo"]), ["snuffleupagus", "cookie monster", "elmo"]); - - text: '[], ["snuffleupagus", "cookie monster", "elmo"]应该返回一个包含三个项目的数组。' + - text: "[], ['snuffleupagus', 'cookie monster', 'elmo']应该返回一个长度为 3 的数组。" testString: assert(diffArray([], ["snuffleupagus", "cookie monster", "elmo"]).length === 3); - - text: '[1, "calf", 3, "piglet"], [7, "filly"] [1, "calf", 3, "piglet", 7, "filly"] [1, "calf", 3, "piglet"], [7, "filly"]应该返回[1, "calf", 3, "piglet", 7, "filly"] 。' + - text: "[1, 'calf', 3, 'piglet'], [7, 'filly']应该返回[1, 'calf', 3, 'piglet', 7, 'filly']。" testString: assert.sameMembers(diffArray([1, "calf", 3, "piglet"], [7, "filly"]), [1, "calf", 3, "piglet", 7, "filly"]); - - text: '[1, "calf", 3, "piglet"], [7, "filly"]应该返回一个包含六个项目的数组。' + - text: "[1, 'calf', 3, 'piglet'], [7, 'filly']应该返回一个长度为 6 的数组。" testString: assert(diffArray([1, "calf", 3, "piglet"], [7, "filly"]).length === 6); ``` @@ -67,7 +71,6 @@ function diffArray(arr1, arr2) { } diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]); - ``` @@ -79,8 +82,28 @@ diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]); ## Solution
-```js -// solution required -``` -/section> +```js +function diffArray(arr1, arr2) { + var newArr = []; + var h1 = Object.create(null); + arr1.forEach(function(e) { + h1[e] = e; + }); + + var h2 = Object.create(null); + arr2.forEach(function(e) { + h2[e] = e; + }); + + Object.keys(h1).forEach(function(e) { + if (!(e in h2)) newArr.push(h1[e]); + }); + Object.keys(h2).forEach(function(e) { + if (!(e in h1)) newArr.push(h2[e]); + }); + // Same, same; but different. + return newArr; +} +``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/dna-pairing.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/dna-pairing.chinese.md index 66a5ac8671..774ea09256 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/dna-pairing.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/dna-pairing.chinese.md @@ -3,15 +3,22 @@ id: afd15382cdfb22c9efe8b7de title: DNA Pairing isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: DNA配对 +forumTopicId: 16009 +localeTitle: DNA 配对 --- ## Description -
DNA链缺少配对元素。获取每个字符,获取其对,并将结果作为二维数组返回。 碱基对是一对AT和CG。将缺少的元素与提供的字符匹配。将提供的字符作为每个数组中的第一个元素返回。例如,对于输入GCG,返回[[“G”,“C”],[“C”,“G”],[“G”,“C”]]字符及其对在一个中配对数组,并将所有数组分组到一个封装数组中。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+DNA 链缺少配对元素。对于每个字符,获取与其配对的元素,并将结果作为二维数组返回。 +碱基对 是一对 AT 和 CG。将缺少的元素与提供的字符匹配。 +将提供的字符作为每个数组中的第一个元素返回。 +例如,对于输入 GCG,返回[[“G”, “C”],[“C”, “G”],[“G”, “C”]]。 +字符及与其配对的元素在一个数组中。再将所有数组放到一个封装数组中。 +
## Instructions -
+
+
## Tests @@ -19,11 +26,11 @@ localeTitle: DNA配对 ```yml tests: - - text: 'pairElement("ATCGA")应返回[["A","T"],["T","A"],["C","G"],["G","C"],["A","T"]] 。' + - text: "pairElement('ATCGA')应该返回[['A','T'],['T','A'],['C','G'],['G','C'],['A','T']]。" testString: assert.deepEqual(pairElement("ATCGA"),[["A","T"],["T","A"],["C","G"],["G","C"],["A","T"]]); - - text: 'pairElement("TTGAG")应返回[["T","A"],["T","A"],["G","C"],["A","T"],["G","C"]] 。' + - text: "pairElement('TTGAG')应该返回[['T','A'],['T','A'],['G','C'],['A','T'],['G','C']]。" testString: assert.deepEqual(pairElement("TTGAG"),[["T","A"],["T","A"],["G","C"],["A","T"],["G","C"]]); - - text: 'pairElement("CTCTA")应返回[["C","G"],["T","A"],["C","G"],["T","A"],["A","T"]] 。' + - text: "pairElement('CTCTA')应该返回[['C','G'],['T','A'],['C','G'],['T','A'],['A','T']]。" testString: assert.deepEqual(pairElement("CTCTA"),[["C","G"],["T","A"],["C","G"],["T","A"],["A","T"]]); ``` @@ -41,7 +48,6 @@ function pairElement(str) { } pairElement("GCG"); - ``` @@ -53,8 +59,17 @@ pairElement("GCG"); ## Solution
+ ```js -// solution required +var lookup = Object.create(null); +lookup.A = 'T'; +lookup.T = 'A'; +lookup.C = 'G'; +lookup.G = 'C'; + +function pairElement(str) { + return str.split('').map(function(p) {return [p, lookup[p]];}); +} ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it.chinese.md index d91076d1bc..4769e03666 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it.chinese.md @@ -3,15 +3,19 @@ id: a5deed1811a43193f9f1c841 title: Drop it isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 算了吧 +forumTopicId: 16010 +localeTitle: 筛选出数组中满足条件的元素 --- ## Description -
给定数组arr ,迭代并从第一个元素(0索引)开始删除每个元素,直到函数func在迭代元素通过它时返回true 。然后在条件满足后返回数组的其余部分,否则, arr应作为空数组返回。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+给定数组arr,从数组的第一个元素开始,用函数func来检查数组的每个元素并删除,直到某个元素传入函数func时返回true。函数最终的返回值也是一个数组,它由原数组中第一个使得functrue的元素及其之后的所有元素组成。 +如果数组中的所有元素都不能让functrue,则返回空数组[]。 +
## Instructions -
+
+
## Tests @@ -19,17 +23,17 @@ localeTitle: 算了吧 ```yml tests: - - text: 'dropElements([1, 2, 3, 4], function(n) {return n >= 3;})应该返回[3, 4] 。' + - text: dropElements([1, 2, 3, 4], function(n) {return n >= 3;})应该返回[3, 4]。 testString: assert.deepEqual(dropElements([1, 2, 3, 4], function(n) {return n >= 3;}), [3, 4]); - - text: 'dropElements([0, 1, 0, 1], function(n) {return n === 1;})应该返回[1, 0, 1] dropElements([0, 1, 0, 1], function(n) {return n === 1;}) [1, 0, 1] 。' + - text: dropElements([0, 1, 0, 1], function(n) {return n === 1;})应该返回[1, 0, 1]。 testString: assert.deepEqual(dropElements([0, 1, 0, 1], function(n) {return n === 1;}), [1, 0, 1]); - - text: 'dropElements([1, 2, 3], function(n) {return n > 0;})应该返回[1, 2, 3] 。' + - text: dropElements([1, 2, 3], function(n) {return n > 0;})应该返回[1, 2, 3]。 testString: assert.deepEqual(dropElements([1, 2, 3], function(n) {return n > 0;}), [1, 2, 3]); - - text: 'dropElements([1, 2, 3, 4], function(n) {return n > 5;})应返回[] 。' + - text: dropElements([1, 2, 3, 4], function(n) {return n > 5;})应该返回[]。 testString: assert.deepEqual(dropElements([1, 2, 3, 4], function(n) {return n > 5;}), []); - - text: 'dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;})应该返回[7, 4] dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;}) [7, 4] 。' + - text: dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;})应该返回[7, 4]。 testString: assert.deepEqual(dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;}), [7, 4]); - - text: 'dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;})应该返回[3, 9, 2] dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}) [3, 9, 2] 。' + - text: dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;})应该返回[3, 9, 2]。 testString: assert.deepEqual(dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}), [3, 9, 2]); ``` @@ -48,7 +52,6 @@ function dropElements(arr, func) { } dropElements([1, 2, 3], function(n) {return n < 3; }); - ``` @@ -60,8 +63,15 @@ dropElements([1, 2, 3], function(n) {return n < 3; }); ## Solution
+ ```js -// solution required +function dropElements(arr, func) { + // Drop them elements. + while (arr.length && !func(arr[0])) { + arr.shift(); + } + return arr; +} ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/everything-be-true.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/everything-be-true.chinese.md index 39974462b1..89879ce76c 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/everything-be-true.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/everything-be-true.chinese.md @@ -3,15 +3,21 @@ id: a10d2431ad0c6a099a4b8b52 title: Everything Be True isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 一切都是真的 +forumTopicId: 16011 +localeTitle: 真假值判断 --- ## Description -
检查谓词(第二个参数)是否对集合的所有元素(第一个参数)都是真实的 。换句话说,您将获得一个对象的数组集合。谓语pre将一个对象的属性,你需要返回true ,如果它的值是truthy 。否则,返回false 。在JavaScript中, truthy值是在布尔上下文中计算时转换为true的值。请记住,您可以通过点表示法或[]表示法访问对象属性。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+检查谓词(第二个参数)是否对集合的所有元素(第一个参数)都是truthy(真实的)。 +换句话说,你将获得一个对象的数组集合。谓词pre是一个对象的属性,如果它的值是truthy(真实的) ,则返回true,否则,返回false 。 +JavaScript 中,如果一个值在 Boolean 的上下文中(比如if语句)可以被执行为true,那么这个值就被认为是truthy的。 +注意,你可以选择使用.[]来访问对象属性对应的值。 +
## Instructions -
+
+
## Tests @@ -19,23 +25,23 @@ localeTitle: 一切都是真的 ```yml tests: - - text: 'truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")应该返回true。' + - text: 'truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")应该返回true。' testString: 'assert.strictEqual(truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"), true);' - - text: 'truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")应该返回false。' + - text: 'truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")应该返回false。' testString: 'assert.strictEqual(truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"), false);' - - text: 'truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age")应该返回false。' + - text: 'truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age")应该返回false。' testString: 'assert.strictEqual(truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 2}, {"user": "Dipsy", "sex": "male", "age": 0}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age"), false);' - - text: 'truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastFoward", "onBoat": null}], "onBoat")应该返回false' - testString: 'assert.strictEqual(truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastForward", "onBoat": null}], "onBoat"), false);' - - text: 'truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastFoward", "onBoat": true}], "onBoat")应该返回true' - testString: 'assert.strictEqual(truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastForward", "onBoat": true}], "onBoat"), true);' - - text: 'truthCheck([{"single": "yes"}], "single")应该返回true' + - text: 'truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastFoward", "onBoat": null}], "onBoat")应该返回false。' + testString: 'assert.strictEqual(truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastFoward", "onBoat": null}], "onBoat"), false);' + - text: 'truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastFoward", "onBoat": true}], "onBoat")应该返回true。' + testString: 'assert.strictEqual(truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastFoward", "onBoat": true}], "onBoat"), true);' + - text: 'truthCheck([{"single": "yes"}], "single")应该返回true。' testString: 'assert.strictEqual(truthCheck([{"single": "yes"}], "single"), true);' - - text: 'truthCheck([{"single": ""}, {"single": "double"}], "single")应该返回false' + - text: 'truthCheck([{"single": ""}, {"single": "double"}], "single")应该返回false。' testString: 'assert.strictEqual(truthCheck([{"single": ""}, {"single": "double"}], "single"), false);' - - text: 'truthCheck([{"single": "double"}, {"single": undefined}], "single")应返回false' + - text: 'truthCheck([{"single": "double"}, {"single": undefined}], "single")应该返回false。' testString: 'assert.strictEqual(truthCheck([{"single": "double"}, {"single": undefined}], "single"), false);' - - text: 'truthCheck([{"single": "double"}, {"single": NaN}], "single")应返回false' + - text: 'truthCheck([{"single": "double"}, {"single": NaN}], "single")应该返回false。' testString: 'assert.strictEqual(truthCheck([{"single": "double"}, {"single": NaN}], "single"), false);' ``` @@ -54,7 +60,6 @@ function truthCheck(collection, pre) { } truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"); - ``` @@ -66,8 +71,12 @@ truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "ma ## Solution
+ ```js -// solution required +function truthCheck(collection, pre) { + // Does everyone have one of these? + return collection.every(function(e) { return e[pre]; }); +} ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/make-a-person.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/make-a-person.chinese.md index 1849611048..05b25c8276 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/make-a-person.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/make-a-person.chinese.md @@ -2,15 +2,32 @@ id: a2f1d72d9b908d0bd72bb9f6 title: Make a Person challengeType: 5 -videoUrl: '' -localeTitle: 做一个人 +forumTopicId: 16020 +localeTitle: 构造一个 Person 类 --- ## Description -
使用以下方法填写对象构造函数:
getFirstName()getLastName()getFullName()setFirstName(first)setLastName(last)setFullName(firstAndLast)
运行测试以查看每个方法的预期输出。采用参数的方法必须只接受一个参数,并且必须是一个字符串。这些方法必须是与对象交互的唯一可用方法。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+在这道题目中,我们需要写一个构造器(constructor)函数。它只接收一个字符串参数firstAndLast,这个参数代表一个英文名的全名(姓和名)。这个构造函数创建出的实例需要具有以下方法: + +```js +getFirstName() +getLastName() +getFullName() +setFirstName(first) +setLastName(last) +setFullName(firstAndLast) +``` + +你可以点击 “运行测试”,然后就可以在底下的控制台中看到每个测试用例执行的情况。 +方法接收一个字符串格式的参数。 +这些方法必须是与对象进行交互的唯一可用方法。 +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -18,29 +35,29 @@ localeTitle: 做一个人 ```yml tests: - - text: Object.keys(bob).length应该返回6。 + - text: Object.keys(bob).length应该返回 6。 testString: assert.deepEqual(Object.keys(bob).length, 6); - - text: bob instanceof Person应该返回true。 + - text: bob instanceof Person应该返回true。 testString: assert.deepEqual(bob instanceof Person, true); - - text: bob.firstName应返回undefined。 + - text: bob.firstName应该返回undefined。 testString: assert.deepEqual(bob.firstName, undefined); - - text: bob.lastName应返回undefined。 + - text: bob.lastName应该返回undefined。 testString: assert.deepEqual(bob.lastName, undefined); - - text: bob.getFirstName()应返回“Bob”。 + - text: "bob.getFirstName()应该返回 'Bob'。" testString: assert.deepEqual(bob.getFirstName(), 'Bob'); - - text: bob.getLastName()应返回“Ross”。 + - text: "bob.getLastName()应该返回 'Ross'。" testString: assert.deepEqual(bob.getLastName(), 'Ross'); - - text: bob.getFullName()应该返回“Bob Ross”。 + - text: "bob.getFullName()应该返回 'Bob Ross'。" testString: assert.deepEqual(bob.getFullName(), 'Bob Ross'); - - text: bob.getFullName()应该在bob.setFirstName("Haskell")之后返回“Haskell Ross”。 + - text: "调用bob.setFirstName('Haskell')之后,bob.getFullName()应该返回 'Haskell Ross'。" testString: assert.strictEqual((function () { bob.setFirstName("Haskell"); return bob.getFullName(); })(), 'Haskell Ross'); - - text: bob.getFullName()应该在bob.setLastName("Curry")之后返回“Haskell Curry”。 + - text: "调用bob.setLastName('Curry')之后,bob.getFullName()应该返回 'Haskell Curry'。" testString: assert.strictEqual((function () { var _bob=new Person('Haskell Ross'); _bob.setLastName("Curry"); return _bob.getFullName(); })(), 'Haskell Curry'); - - text: bob.getFullName()应该在bob.setFullName("Haskell Curry")之后返回“Haskell Curry”。 + - text: "调用bob.setFullName('Haskell Curry')之后,bob.getFullName()应该返回 'Haskell Curry'。" testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getFullName(); })(), 'Haskell Curry'); - - text: bob.getFirstName()应该在bob.setFullName("Haskell Curry")之后返回“Haskell”。 + - text: "调用bob.setFullName('Haskell Curry')之后,bob.getFirstName()应该返回 'Haskell'。" testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getFirstName(); })(), 'Haskell'); - - text: bob.getLastName()应该在bob.setFullName("Haskell Curry")之后返回“Curry”。 + - text: "调用bob.setFullName('Haskell Curry')之后,bob.getLastName()应该返回 'Curry'。" testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getLastName(); })(), 'Curry'); ``` @@ -63,7 +80,6 @@ var Person = function(firstAndLast) { var bob = new Person('Bob Ross'); bob.getFullName(); - ``` @@ -75,8 +91,47 @@ bob.getFullName(); ## Solution
+ ```js -// solution required +var Person = function(firstAndLast) { + + var firstName, lastName; + + function updateName(str) { + firstName = str.split(" ")[0]; + lastName = str.split(" ")[1]; + } + + updateName(firstAndLast); + + this.getFirstName = function(){ + return firstName; + }; + + this.getLastName = function(){ + return lastName; + }; + + this.getFullName = function(){ + return firstName + " " + lastName; + }; + + this.setFirstName = function(str){ + firstName = str; + }; + + + this.setLastName = function(str){ + lastName = str; + }; + + this.setFullName = function(str){ + updateName(str); + }; +}; + +var bob = new Person('Bob Ross'); +bob.getFullName(); ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/map-the-debris.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/map-the-debris.chinese.md index 3ebb472b2c..25c9fb98ba 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/map-the-debris.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/map-the-debris.chinese.md @@ -2,15 +2,23 @@ id: af4afb223120f7348cdfc9fd title: Map the Debris challengeType: 5 -videoUrl: '' -localeTitle: 映射碎片 +forumTopicId: 16021 +localeTitle: 绘制碎片图 --- ## Description -
返回一个新数组,将元素的平均高度转换为轨道周期(以秒为单位)。该数组将包含{name: 'name', avgAlt: avgAlt}格式的对象。您可以在维基百科上阅读有关轨道周期的信息 。值应四舍五入到最接近的整数。轨道上的身体是地球。地球半径为6367.4447公里,地球的GM值为398600.4418 km 3 s -2 。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+在这道题目中,我们需要写一个计算天体轨道周期的函数,它接收一个对象数组参数arr,对象中包含表示天体名称的name属性,及表示天体表面平均海拔的avgAlt属性。就像这样:{name: 'name', avgAlt: avgAlt}。 +这个函数的返回值也是一个对象数组,应保留原对象中的name属性和值,然后根据avgAlt属性的值求出轨道周期(单位是秒),并赋值给orbitalPeriod属性。返回值中不应保留原数据中的avgAlt属性及其对应的值。 +你可以在这条维基百科的链接中找到轨道周期的计算公式。 +轨道周期的计算以地球为基准(即环绕地球的轨道周期),计算结果应取整到最接近的整数。 +地球的半径是 6367.4447 千米,地球的 GM 值为 398600.4418 km3s-2。 +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -18,9 +26,9 @@ localeTitle: 映射碎片 ```yml tests: - - text: 'orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}])应返回[{name: "sputnik", orbitalPeriod: 86400}] 。' + - text: 'orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}])应该返回[{name: "sputnik", orbitalPeriod: 86400}]。' testString: 'assert.deepEqual(orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]), [{name: "sputnik", orbitalPeriod: 86400}]);' - - text: 'orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}])应返回[{name : "iss", orbitalPeriod: 5557}, {name: "hubble", orbitalPeriod: 5734}, {name: "moon", orbitalPeriod: 2377399}] 。' + - text: 'orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}])应该返回[{name : "iss", orbitalPeriod: 5557}, {name: "hubble", orbitalPeriod: 5734}, {name: "moon", orbitalPeriod: 2377399}]。' testString: 'assert.deepEqual(orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}]), [{name : "iss", orbitalPeriod: 5557}, {name: "hubble", orbitalPeriod: 5734}, {name: "moon", orbitalPeriod: 2377399}]);' ``` @@ -40,7 +48,6 @@ function orbitalPeriod(arr) { } orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]); - ``` @@ -52,8 +59,22 @@ orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]); ## Solution
+ ```js -// solution required +function orbitalPeriod(arr) { + var GM = 398600.4418; + var earthRadius = 6367.4447; + var TAU = 2 * Math.PI; + return arr.map(function(obj) { + return { + name: obj.name, + orbitalPeriod: Math.round(TAU * Math.sqrt(Math.pow(obj.avgAlt+earthRadius, 3)/GM)) + }; + }); +} + +orbitalPeriod([{name : "sputkin", avgAlt : 35873.5553}]); + ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters.chinese.md index 3758c90684..6af2453457 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters.chinese.md @@ -3,15 +3,19 @@ id: af7588ade1100bde429baf20 title: Missing letters isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 遗失的信件 +localeTitle: 丢失的字母 --- ## Description -
在传递的字母范围内找到丢失的字母并将其返回。如果范围内存在所有字母,则返回undefined。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+在这道题目中,我们需要写一个函数,找到传入的字符串里缺失的字母并返回它。 +判断缺失的依据是字母顺序,比如 abcdfg 中缺失了 e。而 abcdef 中就没有字母缺失,此时我们需要返回undefined。 +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -19,15 +23,15 @@ localeTitle: 遗失的信件 ```yml tests: - - text: fearNotLetter("abce")应返回“d”。 + - text: "fearNotLetter('abce')应该返回 'd'。" testString: assert.deepEqual(fearNotLetter('abce'), 'd'); - - text: fearNotLetter("abcdefghjklmno")应该返回“i”。 + - text: "fearNotLetter('abcdefghjklmno')应该返回 'i'。" testString: assert.deepEqual(fearNotLetter('abcdefghjklmno'), 'i'); - - text: fearNotLetter("stvwx")应该返回“u”。 + - text: "fearNotLetter('stvwx')应该返回 'u'。" testString: assert.deepEqual(fearNotLetter('stvwx'), 'u'); - - text: fearNotLetter("bcdf")应返回“e”。 + - text: "fearNotLetter('bcdf')应该返回 'e'。" testString: assert.deepEqual(fearNotLetter('bcdf'), 'e'); - - text: fearNotLetter("abcdefghijklmnopqrstuvwxyz")应返回undefined。 + - text: "fearNotLetter('abcdefghijklmnopqrstuvwxyz')应该返回undefined。" testString: assert.isUndefined(fearNotLetter('abcdefghijklmnopqrstuvwxyz')); ``` @@ -45,7 +49,6 @@ function fearNotLetter(str) { } fearNotLetter("abce"); - ``` @@ -57,8 +60,18 @@ fearNotLetter("abce"); ## Solution
+ ```js -// solution required +function fearNotLetter (str) { + for (var i = str.charCodeAt(0); i <= str.charCodeAt(str.length - 1); i++) { + var letter = String.fromCharCode(i); + if (str.indexOf(letter) === -1) { + return letter; + } + } + + return undefined; +} ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin.chinese.md index 5f2e2da46d..32dc83f539 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin.chinese.md @@ -3,15 +3,23 @@ id: aa7697ea2477d1316795783b title: Pig Latin isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 猪拉丁文 +forumTopicId: 16039 +localeTitle: 儿童黑话 --- ## Description -
将提供的字符串翻译为pig latin。 Pig Latin使用英语单词的第一个辅音(或辅音簇),将其移到单词的末尾并加上“ay”后缀。如果一个单词以元音开头,你只需添加“way”到最后。输入字符串保证全部为小写英文单词。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+在这道题目中,我们需要写一个函数,把传入的字符串翻译成“儿童黑话”。 +儿童黑话的基本转换规则很简单,只需要把一个英文单词的第一个辅音字母或第一组辅音簇移到单词的结尾,并在后面加上ay即可。在英语中,字母 a、e、i、o、u 为元音,其余的字母均为辅音。辅音簇的意思是连续的多个辅音字母。 +额外地,如果单词本身是以元音开头的,那只需要在结尾加上way。 +额外地,如果单词不包含元音,那只需要在结尾加上ay。 +在本题中,传入的单词一定会是英文单词,且所有字母均为小写。 +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -19,19 +27,19 @@ localeTitle: 猪拉丁文 ```yml tests: - - text: translatePigLatin("california")应该返回“aliforniacay”。 + - text: "translatePigLatin('california')应该返回 'aliforniacay'。" testString: assert.deepEqual(translatePigLatin("california"), "aliforniacay"); - - text: translatePigLatin("paragraphs")应该返回“aragraphspay”。 + - text: "translatePigLatin('paragraphs')应该返回 'aragraphspay'。" testString: assert.deepEqual(translatePigLatin("paragraphs"), "aragraphspay"); - - text: translatePigLatin("glove")应该返回“oveglay”。 + - text: "translatePigLatin('glove')应该返回 'oveglay'。" testString: assert.deepEqual(translatePigLatin("glove"), "oveglay"); - - text: translatePigLatin("algorithm")应返回“algorithmway”。 + - text: "translatePigLatin('algorithm')应该返回 'algorithmway'。" testString: assert.deepEqual(translatePigLatin("algorithm"), "algorithmway"); - - text: translatePigLatin("eight")应该返回“八通”。 + - text: "translatePigLatin('eight')应该返回 'eightway'。" testString: assert.deepEqual(translatePigLatin("eight"), "eightway"); - - text: 应该处理第一个元音出现在单词末尾的单词。 + - text: "你的代码应该能处理第一个 vowel 在单词中间的情况。比如translatePigLatin('schwartz') 应该返回 'artzschway'" testString: assert.deepEqual(translatePigLatin("schwartz"), "artzschway"); - - text: 应该处理没有元音的单词。 + - text: "你的代码应当能够处理单词中不含元音字母的情况。比如translatePigLatin('rhythm')应该返回 'rhythmay'。" testString: assert.deepEqual(translatePigLatin("rhythm"), "rhythmay"); ``` @@ -49,7 +57,6 @@ function translatePigLatin(str) { } translatePigLatin("consonant"); - ``` @@ -61,8 +68,20 @@ translatePigLatin("consonant"); ## Solution
-```js -// solution required -``` -/section> +```js +function translatePigLatin(str) { + if (isVowel(str.charAt(0))) return str + "way"; + var front = []; + str = str.split(''); + while (str.length && !isVowel(str[0])) { + front.push(str.shift()); + } + return [].concat(str, front).join('') + 'ay'; +} + +function isVowel(c) { + return ['a', 'e', 'i', 'o', 'u'].indexOf(c.toLowerCase()) !== -1; +} +``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/search-and-replace.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/search-and-replace.chinese.md index 80fe3718fa..390be8d9bf 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/search-and-replace.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/search-and-replace.chinese.md @@ -3,15 +3,23 @@ id: a0b5010f579e69b815e7c5d6 title: Search and Replace isRequired: true challengeType: 5 -videoUrl: '' +forumTopicId: 16045 localeTitle: 搜索和替换 --- ## Description -
使用提供的参数执行搜索并替换句子并返回新句子。第一个参数是执行搜索和替换的句子。第二个参数是你要替换的词(之前)。第三个参数是你将用(后)替换第二个参数。 注意
在更换原始单词时保留原始单词中第一个字符的大小写。例如,如果您的意思是将“Book”替换为“dog”,则应将其替换为“Dog”。如果卡住,请记住使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+在这道题目中,我们需要写一个字符串的搜索与替换函数,它的返回值为完成替换后的新字符串。 +这个函数接收的第一个参数为待替换的句子。 +第二个参数为句中需要被替换的单词。 +第三个参数为替换后的单词。 +注意:
你需要保留被替换单词首字母的大小写格式。即如果传入的第二个参数为 "Book",第三个参数为 "dog",那么替换后的结果应为 "Dog" +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -19,15 +27,15 @@ localeTitle: 搜索和替换 ```yml tests: - - text: 'myReplace("Let us go to the store", "store", "mall")应该返回“让我们去商场”。' + - text: "myReplace('Let us go to the store', 'store', 'mall')应该返回 'Let us go to the mall'。" testString: assert.deepEqual(myReplace("Let us go to the store", "store", "mall"), "Let us go to the mall"); - - text: 'myReplace("He is Sleeping on the couch", "Sleeping", "sitting")应该回归“他正坐在沙发上”。' + - text: "myReplace('He is Sleeping on the couch', 'Sleeping', 'sitting')应该返回 'He is Sitting on the couch'。" testString: assert.deepEqual(myReplace("He is Sleeping on the couch", "Sleeping", "sitting"), "He is Sitting on the couch"); - - text: 'myReplace("This has a spellngi error", "spellngi", "spelling")应该返回“这有一个拼写错误”。' + - text: "myReplace('This has a spellngi error', 'spellngi', 'spelling')应该返回 'This has a spelling error'。" testString: assert.deepEqual(myReplace("This has a spellngi error", "spellngi", "spelling"), "This has a spelling error"); - - text: 'myReplace("His name is Tom", "Tom", "john")应该回归“他的名字是约翰”。' + - text: "myReplace('His name is Tom', 'Tom', 'john')应该返回 'His name is John'。" testString: assert.deepEqual(myReplace("His name is Tom", "Tom", "john"), "His name is John"); - - text: 'myReplace("Let us get back to more Coding", "Coding", "algorithms")应该返回“让我们回到更多算法”。' + - text: "myReplace('Let us get back to more Coding', 'Coding', 'algorithms')应该返回 'Let us get back to more Algorithms'。" testString: assert.deepEqual(myReplace("Let us get back to more Coding", "Coding", "algorithms"), "Let us get back to more Algorithms"); ``` @@ -45,7 +53,6 @@ function myReplace(str, before, after) { } myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped"); - ``` @@ -57,8 +64,16 @@ myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped"); ## Solution
+ ```js -// solution required +function myReplace(str, before, after) { + if (before.charAt(0) === before.charAt(0).toUpperCase()) { + after = after.charAt(0).toUpperCase() + after.substring(1); + } else { + after = after.charAt(0).toLowerCase() + after.substring(1); + } + return str.replace(before, after); +} ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy.chinese.md index 953ec96b7f..63966d7fd8 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy.chinese.md @@ -3,15 +3,20 @@ id: a39963a4c10bc8b4d4f06d7e title: Seek and Destroy isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 寻找和摧毁 +forumTopicId: 16046 +localeTitle: 瞄准和消灭 --- ## Description -
您将获得一个初始数组(驱逐舰函数中的第一个参数),后跟一个或多个参数。从初始数组中删除与这些参数具有相同值的所有元素。 注意
你必须使用arguments对象。如果卡住,请记得使用Read-Search-Ask 。编写自己的代码。
+
+在这道题目中,我们要写一个叫destroyer的函数。传给它的第一个参数是数组,我们称他为初始数组。后续的参数数量是不确定的,可能有一个或多个。你需要做的是,从初始数组中移除所有与后续参数相等的元素,并返回移除元素后的数组。 +注意:
你可以使用arguments对象,也可以使用...,即“剩余参数”(Rest Parameters)语法。 +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -19,17 +24,17 @@ localeTitle: 寻找和摧毁 ```yml tests: - - text: 'destroyer([1, 2, 3, 1, 2, 3], 2, 3)应该返回[1, 1] 。' + - text: destroyer([1, 2, 3, 1, 2, 3], 2, 3)应该返回[1, 1]。 testString: assert.deepEqual(destroyer([1, 2, 3, 1, 2, 3], 2, 3), [1, 1]); - - text: 'destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3)应该返回[1, 5, 1] 。' + - text: destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3)应该返回[1, 5, 1]。 testString: assert.deepEqual(destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3), [1, 5, 1]); - - text: 'destroyer([3, 5, 1, 2, 2], 2, 3, 5)应该返回[1] 。' + - text: destroyer([3, 5, 1, 2, 2], 2, 3, 5)应该返回[1]。 testString: assert.deepEqual(destroyer([3, 5, 1, 2, 2], 2, 3, 5), [1]); - - text: 'destroyer([2, 3, 2, 3], 2, 3)应该返回[] 。' + - text: destroyer([2, 3, 2, 3], 2, 3)应该返回[]。 testString: assert.deepEqual(destroyer([2, 3, 2, 3], 2, 3), []); - - text: 'destroyer(["tree", "hamburger", 53], "tree", 53)应该返回["hamburger"] 。' + - text: "destroyer(['tree', 'hamburger', 53], 'tree', 53)应该返回['hamburger']。" testString: assert.deepEqual(destroyer(["tree", "hamburger", 53], "tree", 53), ["hamburger"]); - - text: 'destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan")应该返回[12,92,65] 。' + - text: "destroyer(['possum', 'trollo', 12, 'safari', 'hotdog', 92, 65, 'grandma', 'bugati', 'trojan', 'yacht'], 'yacht', 'possum', 'trollo', 'safari', 'hotdog', 'grandma', 'bugati', 'trojan')应该返回[12,92,65]。" testString: assert.deepEqual(destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan"), [12,92,65]); ``` @@ -48,7 +53,6 @@ function destroyer(arr) { } destroyer([1, 2, 3, 1, 2, 3], 2, 3); - ``` @@ -60,8 +64,19 @@ destroyer([1, 2, 3, 1, 2, 3], 2, 3); ## Solution
+ ```js -// solution required +function destroyer(arr) { + var hash = Object.create(null); + [].slice.call(arguments, 1).forEach(function(e) { + hash[e] = true; + }); + // Remove all the values + return arr.filter(function(e) { return !(e in hash);}); +} + +destroyer([1, 2, 3, 1, 2, 3], 2, 3); + ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple.chinese.md index 652f1cb2f4..4836e7eeed 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple.chinese.md @@ -3,15 +3,21 @@ id: ae9defd7acaf69703ab432ea title: Smallest Common Multiple isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 最小的共同多重 +forumTopicId: 16075 +localeTitle: 最小公倍数 --- ## Description -
找到所提供参数的最小公倍数,可以均匀地除以这些参数,以及这些参数之间范围内的所有序号。范围将是两个数字的数组,不一定按数字顺序排列。例如,如果给定1和3,找到1和3的最小公倍数,它们也可以被1到3 之间的所有数字整除。这里的答案是6.记得使用Read-Search-Ask如果你得到卡住。尝试配对程序。编写自己的代码。
+
+在这道题目中,我们需要写一个函数,它接收一个包含两个数字的数组参数arr,它的返回值为这两个数字范围内所有数字(包含这两个数字)的最小公倍数。 +注意,较小数不一定总是出现在数组的第一个元素。 +比如,传入[1, 3],那么函数的返回结果应为 1、2、3 的最小公倍数,即为 6。 +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -19,17 +25,17 @@ localeTitle: 最小的共同多重 ```yml tests: - - text: 'smallestCommons([1, 5])应返回一个数字。' + - text: smallestCommons([1, 5])应该返回一个数字。 testString: assert.deepEqual(typeof smallestCommons([1, 5]), 'number'); - - text: 'smallestCommons([1, 5])应该返回60。' + - text: smallestCommons([1, 5])应该返回 60。 testString: assert.deepEqual(smallestCommons([1, 5]), 60); - - text: 'smallestCommons([5, 1])应该返回60。' + - text: smallestCommons([5, 1])应该返回 60。 testString: assert.deepEqual(smallestCommons([5, 1]), 60); - - text: 'smallestCommons([2, 10]) 2,10 smallestCommons([2, 10])应返回2520。' + - text: smallestCommons([2, 10])应该返回 2520。. testString: assert.deepEqual(smallestCommons([2, 10]), 2520); - - text: 'smallestCommons([1, 13]) 1,13 smallestCommons([1, 13])应返回360360。' + - text: smallestCommons([1, 13])应该返回 360360。 testString: assert.deepEqual(smallestCommons([1, 13]), 360360); - - text: 'smallestCommons([23, 18]) 23,18 smallestCommons([23, 18])应返回6056820。' + - text: smallestCommons([23, 18])应该返回 6056820。 testString: assert.deepEqual(smallestCommons([23, 18]), 6056820); ``` @@ -48,7 +54,6 @@ function smallestCommons(arr) { smallestCommons([1,5]); - ``` @@ -60,8 +65,27 @@ smallestCommons([1,5]); ## Solution
+ ```js -// solution required +function gcd(a, b) { + while (b !== 0) { + a = [b, b = a % b][0]; + } + return a; +} + +function lcm(a, b) { + return (a * b) / gcd(a, b); +} + +function smallestCommons(arr) { + arr.sort(function(a,b) {return a-b;}); + var rng = []; + for (var i = arr[0]; i <= arr[1]; i++) { + rng.push(i); + } + return rng.reduce(lcm); +} ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union.chinese.md index e2f4eab524..d405d47d30 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union.chinese.md @@ -3,15 +3,22 @@ id: a105e963526e7de52b219be9 title: Sorted Union isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 排序联盟 +forumTopicId: 16077 +localeTitle: 集合排序 --- ## Description -
编写一个带有两个或更多数组的函数,并按原始提供的数组的顺序返回一个新的唯一值数组。换句话说,所有数组中存在的所有值都应包含在它们的原始顺序中,但在最终数组中没有重复。唯一编号应按其原始顺序排序,但最终数组不应按数字顺序排序。检查断言测试以获取示例。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+在这道题目中,我们需要写一个函数,它接收两个或多个数组为参数。我们需要对这些数组中所有元素进行去除重复元素的处理,并以数组的形式返回去重结果。 +换句话说,所有数组中出现的所有值都应按其原始顺序包括在内,但最终数组中不得重复。 +唯一数字应按其原始顺序排序,但最终数组不应按数字顺序排序。 +如有疑问,请先浏览下方的测试用例。 +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -19,11 +26,11 @@ localeTitle: 排序联盟 ```yml tests: - - text: 'uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])应该返回[1, 3, 2, 5, 4] uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]) [1, 3, 2, 5, 4] 。' + - text: uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])应该返回[1, 3, 2, 5, 4]。 testString: assert.deepEqual(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]), [1, 3, 2, 5, 4]); - - text: 'uniteUnique([1, 2, 3], [5, 2, 1])应该返回[1, 2, 3, 5] uniteUnique([1, 2, 3], [5, 2, 1]) [1, 2, 3, 5] 。' + - text: uniteUnique([1, 2, 3], [5, 2, 1])应该返回[1, 2, 3, 5]。 testString: assert.deepEqual(uniteUnique([1, 2, 3], [5, 2, 1]), [1, 2, 3, 5]); - - text: 'uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8])应该返回[1, 2, 3, 5, 4, 6, 7, 8] uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]) [1, 2, 3, 5, 4, 6, 7, 8] 。' + - text: uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8])应该返回[1, 2, 3, 5, 4, 6, 7, 8]。 testString: assert.deepEqual(uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]), [1, 2, 3, 5, 4, 6, 7, 8]); ``` @@ -41,7 +48,6 @@ function uniteUnique(arr) { } uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]); - ``` @@ -53,8 +59,13 @@ uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]); ## Solution
+ ```js -// solution required +function uniteUnique(arr) { + return [].slice.call(arguments).reduce(function(a, b) { + return [].concat(a, b.filter(function(e) {return a.indexOf(e) === -1;})); + }, []); +} ``` -/section> +
\ No newline at end of file diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case.chinese.md index 97a3eac64d..5ef600568b 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case.chinese.md @@ -3,15 +3,19 @@ id: a103376db3ba46b2d50db289 title: Spinal Tap Case isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 脊椎龙头套 +forumTopicId: 16078 +localeTitle: 短线连接格式 --- ## Description -
将字符串转换为脊柱案例。脊柱情况是全小写单词连接的破折号。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+在这道题目中,我们需要写一个函数,把一个字符串转换为“短线连接格式”。短线连接格式的意思是,所有字母都是小写,且用-连接。比如,对于Hello World,应该转换为hello-world;对于I love_Javascript-VeryMuch,应该转换为i-love-javascript-very-much。 +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -19,15 +23,15 @@ localeTitle: 脊椎龙头套 ```yml tests: - - text: spinalCase("This Is Spinal Tap")应该返回"this-is-spinal-tap" 。 + - text: "spinalCase('This Is Spinal Tap')应该返回'this-is-spinal-tap'。" testString: assert.deepEqual(spinalCase("This Is Spinal Tap"), "this-is-spinal-tap"); - - text: spinalCase("thisIsSpinal Tap")应该返回"this-is-spinal-tap" 。 + - text: "spinalCase('thisIsSpinalTap')应该返回'this-is-spinal-tap'。" testString: assert.strictEqual(spinalCase('thisIsSpinalTap'), "this-is-spinal-tap"); - - text: spinalCase("The_Andy_ Griffith_Show")应该返回"the-andy-griffith-show" 。 + - text: "spinalCase('The_Andy_Griffith_Show')应该返回'the-andy-griffith-show'。" testString: assert.strictEqual(spinalCase("The_Andy_Griffith_Show"), "the-andy-griffith-show"); - - text: spinalCase("Teletubbies say Eh-oh")应该返回"teletubbies-say-eh-oh" 。 + - text: "spinalCase('Teletubbies say Eh-oh')应该返回'teletubbies-say-eh-oh'。" testString: assert.strictEqual(spinalCase("Teletubbies say Eh-oh"), "teletubbies-say-eh-oh"); - - text: spinalCase("AllThe-small Things")应该归还"all-the-small-things" 。 + - text: "spinalCase('AllThe-small Things')应该返回'all-the-small-things'。" testString: assert.strictEqual(spinalCase("AllThe-small Things"), "all-the-small-things"); ``` @@ -47,7 +51,6 @@ function spinalCase(str) { } spinalCase('This Is Spinal Tap'); - ``` @@ -59,8 +62,14 @@ spinalCase('This Is Spinal Tap'); ## Solution
+ ```js -// solution required +function spinalCase(str) { + // "It's such a fine line between stupid, and clever." + // --David St. Hubbins + str = str.replace(/([a-z](?=[A-Z]))/g, '$1 '); + return str.toLowerCase().replace(/\ |\_/g, '-'); +} ``` -/section> +
\ No newline at end of file diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller.chinese.md index 4e2ad5f956..c665b7e9fb 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller.chinese.md @@ -3,15 +3,19 @@ id: ab306dbdcc907c7ddfc30830 title: Steamroller isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 压路机 +forumTopicId: 16079 +localeTitle: 扁平化 --- ## Description -
展平嵌套数组。您必须考虑不同的嵌套级别。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+在这道题目中,我们需要写一个数组扁平化的函数。 +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -19,13 +23,13 @@ localeTitle: 压路机 ```yml tests: - - text: 'steamrollArray([[["a"]], [["b"]]])应返回["a", "b"] 。' + - text: "steamrollArray([[['a']], [['b']]])应该返回['a', 'b']。" testString: assert.deepEqual(steamrollArray([[["a"]], [["b"]]]), ["a", "b"]); - - text: 'steamrollArray([1, [2], [3, [[4]]]])应该返回[1, 2, 3, 4] 。' + - text: steamrollArray([1, [2], [3, [[4]]]])应该返回[1, 2, 3, 4]。 testString: assert.deepEqual(steamrollArray([1, [2], [3, [[4]]]]), [1, 2, 3, 4]); - - text: 'steamrollArray([1, [], [3, [[4]]]])应该返回[1, 3, 4] 。' + - text: steamrollArray([1, [], [3, [[4]]]])应该返回[1, 3, 4]。 testString: assert.deepEqual(steamrollArray([1, [], [3, [[4]]]]), [1, 3, 4]); - - text: 'steamrollArray([1, {}, [3, [[4]]]])应返回[1, {}, 3, 4] 。' + - text: steamrollArray([1, {}, [3, [[4]]]])应该返回[1, {}, 3, 4]。 testString: assert.deepEqual(steamrollArray([1, {}, [3, [[4]]]]), [1, {}, 3, 4]); ``` @@ -44,7 +48,6 @@ function steamrollArray(arr) { } steamrollArray([1, [2], [3, [[4]]]]); - ``` @@ -56,8 +59,20 @@ steamrollArray([1, [2], [3, [[4]]]]); ## Solution
+ ```js -// solution required +function steamrollArray(arr) { + if (!Array.isArray(arr)) { + return [arr]; + } + var out = []; + arr.forEach(function(e) { + steamrollArray(e).forEach(function(v) { + out.push(v); + }); + }); + return out; +} ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-numbers-in-a-range.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-numbers-in-a-range.chinese.md index 79276d8f0c..1337581145 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-numbers-in-a-range.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-numbers-in-a-range.chinese.md @@ -3,15 +3,23 @@ id: a3566b1109230028080c9345 title: Sum All Numbers in a Range isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 求和范围中的所有数字 +forumTopicId: 16083 +localeTitle: 范围内的数字求和 --- ## Description -
我们将通过两个数字的数组。返回这两个数字的总和加上它们之间所有数字的总和。最低的数字并不总是第一位。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+给出一个含有两个数字的数组,我们需要写一个函数,让它返回这两个数字间所有数字(包含这两个数字)的总和。 + +例如,sumAll([4,1]) 应该返回 10,因为从 1 到 4 (包含 1、4)的所有数字的和是 10。 + +如果你遇到了问题,请点击帮助。 + +
## Instructions -
+
+
## Tests @@ -19,15 +27,15 @@ localeTitle: 求和范围中的所有数字 ```yml tests: - - text: 'sumAll([1, 4])应该返回一个数字。' + - text: sumAll([1, 4])应该返回一个数字。 testString: assert(typeof sumAll([1, 4]) === 'number'); - - text: 'sumAll([1, 4])应该返回10。' + - text: sumAll([1, 4])应该返回 10。 testString: assert.deepEqual(sumAll([1, 4]), 10); - - text: 'sumAll([4, 1])应该返回10。' + - text: sumAll([4, 1])应该返回 10。 testString: assert.deepEqual(sumAll([4, 1]), 10); - - text: 'sumAll([5, 10])应该返回45。' + - text: sumAll([5, 10])应该返回 45。 testString: assert.deepEqual(sumAll([5, 10]), 45); - - text: 'sumAll([10, 5])应该返回45。' + - text: sumAll([10, 5])应该返回 45。 testString: assert.deepEqual(sumAll([10, 5]), 45); ``` @@ -45,7 +53,6 @@ function sumAll(arr) { } sumAll([1, 4]); - ``` @@ -57,8 +64,15 @@ sumAll([1, 4]); ## Solution
-```js -// solution required -``` -/section> +```js +function sumAll(arr) { + var sum = 0; + arr.sort(function(a,b) {return a-b;}); + for (var i = arr[0]; i <= arr[1]; i++) { + sum += i; + } + return sum; +} +``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers.chinese.md index 20bd140c3a..357932ddca 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers.chinese.md @@ -3,15 +3,21 @@ id: a5229172f011153519423690 title: Sum All Odd Fibonacci Numbers isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 求所有奇数斐波纳契数 +forumTopicId: 16084 +localeTitle: 求斐波那契数组中的奇数之和 --- ## Description -
给定正整数num ,返回小于或等于num的所有奇数Fibonacci数的总和。 Fibonacci序列中的前两个数字是1和1.序列中的每个附加数字是前两个数字的总和。 Fibonacci序列的前六个数字是sumFibs(10)和8.例如, sumFibs(10)应该返回10因为小于或等于10所有奇数Fibonacci数都是sumFibs(10)和5.如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+在这道题目中,我们需要写一个函数,参数为一个正整数num。它的作用是计算斐波那契数列中,小于或等于num的奇数之和。 +斐波那契数列中,第一和第二个数字都是 1,后面的每个数字由之前两数相加得出。斐波那契数列的前六个数字分别为:1、1、2、3、5、8。 +比如,sumFibs(10)应该返回10。因为斐波那契数列中,比10小的数字只有 1、1、3、5。 +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -21,15 +27,15 @@ localeTitle: 求所有奇数斐波纳契数 tests: - text: sumFibs(1)应该返回一个数字。 testString: assert(typeof sumFibs(1) === "number"); - - text: sumFibs(1000)应该返回1785。 + - text: sumFibs(1000)应该返回 1785。 testString: assert(sumFibs(1000) === 1785); - - text: sumFibs(4000000)应返回4613732。 + - text: sumFibs(4000000)应该返回 4613732。 testString: assert(sumFibs(4000000) === 4613732); - - text: sumFibs(4)应该返回5。 + - text: sumFibs(4)应该返回 5。 testString: assert(sumFibs(4) === 5); - - text: sumFibs(75024)应该返回60696。 + - text: sumFibs(75024)应该返回 60696。 testString: assert(sumFibs(75024) === 60696); - - text: sumFibs(75025)应该返回135721。 + - text: sumFibs(75025)应该返回 135721。 testString: assert(sumFibs(75025) === 135721); ``` @@ -47,7 +53,6 @@ function sumFibs(num) { } sumFibs(4); - ``` @@ -59,8 +64,19 @@ sumFibs(4); ## Solution
-```js -// solution required -``` -/section> +```js +function sumFibs(num) { + var a = 1; + var b = 1; + var s = 0; + while (a <= num) { + if (a % 2 !== 0) { + s += a; + } + a = [b, b=b+a][0]; + } + return s; +} +``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-primes.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-primes.chinese.md index 53c7838a7f..92e954dc55 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-primes.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-primes.chinese.md @@ -3,15 +3,26 @@ id: a3bfc1673c0526e06d3ac698 title: Sum All Primes isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: Sum All Primes +forumTopicId: 16085 +localeTitle: 对所有素数求和 --- ## Description -
将所有素数加起来并包括所提供的数字。素数被定义为大于1的数,并且只有两个除数,一个和一个除数。例如,2是素数,因为它只能被1和2整除。提供的号码可能不是主要的。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
+
+ +质数是大于 1 且仅可以被 1 和自己整除的数。 +比如,2 就是一个质数,因为它只可以被 1 和 2(它本身)整除。 +相反,4 不是质数,因为它可以被 1, 2 和 4 整除。 + +重写 `sumPrimes` 使其返回所有小于或等于该数字的质数 +的和。 + +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -21,9 +32,9 @@ localeTitle: Sum All Primes tests: - text: sumPrimes(10)应该返回一个数字。 testString: assert.deepEqual(typeof sumPrimes(10), 'number'); - - text: sumPrimes(10)应该返回17。 + - text: sumPrimes(10)应该返回 17。 testString: assert.deepEqual(sumPrimes(10), 17); - - text: sumPrimes(977)应该返回73156。 + - text: sumPrimes(977)应该返回 73156。 testString: assert.deepEqual(sumPrimes(977), 73156); ``` @@ -41,7 +52,6 @@ function sumPrimes(num) { } sumPrimes(10); - ``` @@ -53,8 +63,33 @@ sumPrimes(10); ## Solution
+ ```js -// solution required +function eratosthenesArray(n) { + var primes = []; + if (n > 2) { + var half = n>>1; + var sieve = Array(half); + for (var i = 1, limit = Math.sqrt(n)>>1; i <= limit; i++) { + if (!sieve[i]) { + for (var step = 2*i+1, j = (step*step)>>1; j < half; j+=step) { + sieve[j] = true; + } + } + } + primes.push(2); + for (var p = 1; p < half; p++) { + if (!sieve[p]) primes.push(2*p+1); + } + } + return primes; +} + +function sumPrimes(num) { + return eratosthenesArray(num+1).reduce(function(a,b) {return a+b;}, 0); +} + +sumPrimes(10); ``` -/section> +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou.chinese.md index 761c2b51df..ef6a013c1f 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou.chinese.md @@ -3,15 +3,20 @@ id: a8e512fbe388ac2f9198f0fa title: Wherefore art thou isRequired: true challengeType: 5 -videoUrl: '' -localeTitle: 因此,你是艺术家 +forumTopicId: 16092 +localeTitle: 罗密欧与朱丽叶 --- ## Description -
创建一个查看对象数组(第一个参数)的函数,并返回具有匹配的名称和值对的所有对象的数组(第二个参数)。如果要包含在返回的数组中,则源对象的每个名称和值对都必须存在于集合中的对象中。例如,如果第一个参数是[{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }] ,第二个参数是{ last: "Capulet" } ,然后你必须从数组(第一个参数)返回第三个对象,因为它包含名称及其值,它作为第二个参数传递。如果卡住,请记得使用Read-Search-Ask 。编写自己的代码。
+
+在这道题目中,我们要写一个函数,它接收两个参数:第一个参数是对象数组,第二个参数是一个对象。我们需要从对象数组中找出与第二个参数相等或包含第二个参数的所有对象,并以对象数组的形式返回。其中,相等的意思是原数组中的对象与第二个参数中对象的所有键值对完全相等;包含的意思是只要第二个参数中对象的所有键存在于原数组对象中,且它们对应的值相同即可。 +比如,如果第一个参数是[{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }],第二个参数是{ last: "Capulet" }。那么你需要以对象数组的形式返回第一个参数中的第三个元素,因为它包含第二个参数中定义的键last,且对应的值"Capulet"相同 +如果你遇到了问题,请点击帮助。 +
## Instructions -
+
+
## Tests @@ -19,17 +24,17 @@ localeTitle: 因此,你是艺术家 ```yml tests: - - text: 'whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })应该返回[{ first: "Tybalt", last: "Capulet" }]' + - text: 'whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })应该返回[{ first: "Tybalt", last: "Capulet" }]。' testString: 'assert.deepEqual(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }), [{ first: "Tybalt", last: "Capulet" }]);' - - text: 'whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })应返回[{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }] 。' + - text: 'whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })应该返回[{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]。' testString: 'assert.deepEqual(whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 }), [{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]);' - - text: 'whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })应该返回[{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }] 。' + - text: 'whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })应该返回[{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }]。' testString: 'assert.deepEqual(whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }), [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }]);' - - text: 'whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 })应该返回[{ "apple": 1, "bat": 2, "cookie": 2 }] 。' + - text: 'whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 })应该返回[{ "apple": 1, "bat": 2, "cookie": 2 }]。' testString: 'assert.deepEqual(whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 }), [{ "apple": 1, "bat": 2, "cookie": 2 }]);' - - text: 'whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }], { "apple": 1, "bat": 2 })应该返回[{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }] 。' + - text: 'whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }], { "apple": 1, "bat": 2 })应该返回[{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }]。' testString: 'assert.deepEqual(whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, {"bat":2}], { "apple": 1, "bat": 2 }), [{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }]);' - - text: 'whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3})应该返回[]' + - text: 'whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3})应该返回[]。' testString: 'assert.deepEqual(whatIsInAName([{ "a": 1, "b": 2, "c": 3 }], { "a": 1, "b": 9999, "c": 3 }), []);' ``` @@ -53,7 +58,6 @@ function whatIsInAName(collection, source) { } whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }); - ``` @@ -65,8 +69,17 @@ whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: ## Solution
-```js -// solution required -``` -/section> +```js +function whatIsInAName(collection, source) { + var arr = []; + var keys = Object.keys(source); + collection.forEach(function(e) { + if(keys.every(function(key) {return e[key] === source[key];})) { + arr.push(e); + } + }); + return arr; +} +``` +