feat(i18n):Chinese intermediate algorithm scripting (#38442)

Co-authored-by: ZhichengChen <chenzhicheng@dayuwuxian.com>
This commit is contained in:
ZhichengChen
2020-09-07 16:10:29 +08:00
committed by GitHub
parent 2db889099d
commit 653396706e
21 changed files with 643 additions and 272 deletions

View File

@ -3,15 +3,23 @@ id: a97fd23d9b809dac9921074f
title: Arguments Optional title: Arguments Optional
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 14271
localeTitle: 参数可选 localeTitle: 可选参数
--- ---
## Description ## Description
<section id="description">创建一个将两个参数相加的函数。如果只提供了一个参数,则返回一个需要一个参数并返回总和的函数。例如, <code>addTogether(2, 3)</code>应返回<code>5</code> <code>addTogether(2)</code>应返回一个函数。使用单个参数调用此返回函数将返回总和: <code>var sumTwoAnd = addTogether(2);</code> <code>sumTwoAnd(3)</code>返回<code>5</code> 。如果任一参数不是有效数字则返回undefined。如果卡住请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
创建一个将两个参数相加的函数。如果只传入了一个参数,则返回一个函数,需要传入一个参数并返回总和。
比如,<code>addTogether(2, 3)</code>应该返回<code>5</code>。而<code>addTogether(2)</code>应该返回一个函数。
调用这个返回的函数,传入一个值,返回总和:
<code>var sumTwoAnd = addTogether(2);</code>
<code>sumTwoAnd(3)</code>此时应返回<code>5</code>
只要其中任何一个参数不是数字,那就应返回<code>undefined</code>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -45,7 +53,6 @@ function addTogether() {
} }
addTogether(2,3); addTogether(2,3);
``` ```
</div> </div>
@ -57,8 +64,21 @@ addTogether(2,3);
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -3,15 +3,20 @@ id: a8d97bd4c764e91f9d2bda01
title: Binary Agents title: Binary Agents
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 14273
localeTitle: 二元代理商 localeTitle: 二进制转化
--- ---
## Description ## Description
<section id="description">返回传递的二进制字符串的英文翻译句子。二进制字符串将以空格分隔。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section>
<section id='description'>
写一个函数,把输入的二进制字符串转换成英文句子。
二进制字符串将以空格分隔。
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,9 +24,9 @@ localeTitle: 二元代理商
```yml ```yml
tests: tests:
- text: <code>binaryAgent(&quot;01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111&quot;)</code>应该返回“不是篝火有趣!?” - text: "<code>binaryAgent('01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111')</code>应该返回 'Aren&#39;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!?"); 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: <code>binaryAgent(&quot;01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001&quot;)</code>应返回“我爱FreeCodeCamp!” - text: "<code>binaryAgent('01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001')</code>应返回 '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!"); 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"); binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
``` ```
</div> </div>
@ -51,8 +55,11 @@ binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 0110
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -3,15 +3,17 @@ id: a6b0bb188d873cb2c8729495
title: Convert HTML Entities title: Convert HTML Entities
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: ''
localeTitle: 转换HTML实体 localeTitle: 转换HTML实体
--- ---
## Description ## Description
<section id="description">将字符串中的字符<code>&amp;</code> <code>&lt;</code> <code>&gt;</code> <code>&quot;</code> (双引号)和<code>&#39;</code> 撇号转换为相应的HTML实体。如果卡住请记住使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。写下你的自己的代码。 </section> <section id='description'>
在这道题目中,我们需要写一个转换 HTML entity 的函数。需要转换的 HTML entity 有<code>&</code><code><</code><code>></code><code>"</code>(双引号)和<code>'</code>(单引号)。
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,19 +21,19 @@ localeTitle: 转换HTML实体
```yml ```yml
tests: tests:
- text: <code>convertHTML(&quot;Dolce &amp; Gabbana&quot;)</code>应该返回<code>Dolce &amp;amp; Gabbana</code> - text: "<code>convertHTML('Dolce & Gabbana')</code>应该返回<code>Dolce &&#8203;amp; Gabbana</code>。"
testString: assert.match(convertHTML("Dolce & Gabbana"), /Dolce &amp; Gabbana/); testString: assert.match(convertHTML("Dolce & Gabbana"), /Dolce &amp; Gabbana/);
- text: <code>convertHTML(&quot;Hamburgers &lt; Pizza &lt; Tacos&quot;)</code>应该返回<code>Hamburgers &amp;lt; Pizza &amp;lt; Tacos</code> - text: "<code>convertHTML('Hamburgers < Pizza < Tacos')</code>应该返回<code>Hamburgers &&#8203;lt; Pizza &&#8203;lt; Tacos</code>。"
testString: assert.match(convertHTML("Hamburgers < Pizza < Tacos"), /Hamburgers &lt; Pizza &lt; Tacos/); testString: assert.match(convertHTML("Hamburgers < Pizza < Tacos"), /Hamburgers &lt; Pizza &lt; Tacos/);
- text: <code>convertHTML(&quot;Sixty &gt; twelve&quot;)</code>应返回<code>Sixty &amp;gt; twelve</code> - text: "<code>convertHTML('Sixty > twelve')</code>应返回<code>Sixty &&#8203;gt; twelve</code>。"
testString: assert.match(convertHTML("Sixty > twelve"), /Sixty &gt; twelve/); testString: assert.match(convertHTML("Sixty > twelve"), /Sixty &gt; twelve/);
- text: '<code>convertHTML(&#39;Stuff in &quot;quotation marks&quot;&#39;)</code>应该<code>convertHTML(&#39;Stuff in &quot;quotation marks&quot;&#39;)</code>返回<code>Stuff in &amp;quot;quotation marks&amp;quot;</code> 。' - text: "<code>convertHTML(&apos;Stuff in \"quotation marks\"&apos;)</code>应该返回<code>Stuff in &&#8203;quot;quotation marks&&#8203;quot;</code>。"
testString: assert.match(convertHTML('Stuff in "quotation marks"'), /Stuff in &quot;quotation marks&quot;/); testString: assert.match(convertHTML('Stuff in "quotation marks"'), /Stuff in &quot;quotation marks&quot;/);
- text: '<code>convertHTML(&quot;Schindler&#39;s List&quot;)</code>应该返回<code>Schindler&amp;apos;s List</code> 。' - text: "<code>convertHTML('Schindler&apos;s List')</code>应该返回<code>Schindler&&#8203;apos;s List</code>。"
testString: assert.match(convertHTML("Schindler's List"), /Schindler&apos;s List/); testString: assert.match(convertHTML("Schindler's List"), /Schindler&apos;s List/);
- text: <code>convertHTML(&quot;&lt;&gt;&quot;)</code>应返回<code>&amp;lt;&amp;gt;</code> - text: "<code>convertHTML('<>')</code>应返回<code>&&#8203;lt;&&#8203;gt;</code>。"
testString: assert.match(convertHTML('<>'), /&lt;&gt;/); testString: assert.match(convertHTML('<>'), /&lt;&gt;/);
- text: <code>convertHTML(&quot;abc&quot;)</code>应该返回<code>abc</code> - text: "<code>convertHTML('abc')</code>应该返回<code>abc</code>。"
testString: assert.strictEqual(convertHTML('abc'), 'abc'); testString: assert.strictEqual(convertHTML('abc'), 'abc');
``` ```
@ -50,7 +52,6 @@ function convertHTML(str) {
} }
convertHTML("Dolce & Gabbana"); convertHTML("Dolce & Gabbana");
``` ```
</div> </div>
@ -62,8 +63,19 @@ convertHTML("Dolce & Gabbana");
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```js
// solution required var MAP = { '&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&apos;'};
function convertHTML(str) {
return str.replace(/[&<>"']/g, function(c) {
return MAP[c];
});
}
``` ```
/section> </section>

View File

@ -3,15 +3,19 @@ id: a5de63ebea8dbee56860f4f2
title: Diff Two Arrays title: Diff Two Arrays
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16008
localeTitle: 分两个阵列 localeTitle: 分两个数组
--- ---
## Description ## Description
<section id="description">比较两个数组并返回一个新数组,其中只有在两个给定数组中的一个中找到的任何项,但不能同时返回两个数组。换句话说,返回两个数组的对称差异。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 <strong>注意</strong> <br>您可以按任何顺序返回包含其元素的数组。 </section> <section id='description'>
在这道题目中,我们需要写一个函数,比较两个数组,返回一个新的数组。这个新数组需要包含传入的两个数组所有元素中,仅在其中一个数组里出现的元素。如果某个元素同时出现在两个数组中,则不应包含在返回的数组里。换言之,我们需要返回这两个数组的对称差。
<strong>注意:</strong><br>返回数组中的元素可任意排序。
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,35 +23,35 @@ localeTitle: 差分两个阵列
```yml ```yml
tests: tests:
- text: '<code>diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])</code>应该返回一个数组。' - text: <code>diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])</code>应该返回一个数组。
testString: assert(typeof diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) === "object"); testString: assert(typeof diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) === "object");
- text: '<code>[&quot;diorite&quot;, &quot;andesite&quot;, &quot;grass&quot;, &quot;dirt&quot;, &quot;pink wool&quot;, &quot;dead shrub&quot;], [&quot;diorite&quot;, &quot;andesite&quot;, &quot;grass&quot;, &quot;dirt&quot;, &quot;dead shrub&quot;]</code>应该返回<code>[&quot;pink wool&quot;]</code> 。' - text: "<code>['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'], ['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']</code>应该返回<code>['pink wool']</code>。"
testString: assert.sameMembers(diffArray(["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: '<code>[&quot;diorite&quot;, &quot;andesite&quot;, &quot;grass&quot;, &quot;dirt&quot;, &quot;pink wool&quot;, &quot;dead shrub&quot;], [&quot;diorite&quot;, &quot;andesite&quot;, &quot;grass&quot;, &quot;dirt&quot;, &quot;dead shrub&quot;]</code>应该返回一个包含一个项目的数组。' - text: "<code>['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'], ['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']</code>应该返回一个长度为 1 的数组。"
testString: assert(diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]).length === 1); testString: assert(diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]).length === 1);
- text: '<code>[&quot;andesite&quot;, &quot;grass&quot;, &quot;dirt&quot;, &quot;pink wool&quot;, &quot;dead shrub&quot;], [&quot;diorite&quot;, &quot;andesite&quot;, &quot;grass&quot;, &quot;dirt&quot;, &quot;dead shrub&quot;]</code>应该返回<code>[&quot;diorite&quot;, &quot;pink wool&quot;]</code> 。' - text: "<code>['andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'], ['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']</code>应该返回<code>['diorite', 'pink wool']</code>。"
testString: assert.sameMembers(diffArray(["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: '<code>[&quot;andesite&quot;, &quot;grass&quot;, &quot;dirt&quot;, &quot;pink wool&quot;, &quot;dead shrub&quot;], [&quot;diorite&quot;, &quot;andesite&quot;, &quot;grass&quot;, &quot;dirt&quot;, &quot;dead shrub&quot;]</code>应该返回一个数组有两个项目。' - text: "<code>['andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'], ['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']</code>应该返回一个长度为 2 的数组。"
testString: assert(diffArray(["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]).length === 2); testString: assert(diffArray(["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]).length === 2);
- text: '<code>[&quot;andesite&quot;, &quot;grass&quot;, &quot;dirt&quot;, &quot;dead shrub&quot;], [&quot;andesite&quot;, &quot;grass&quot;, &quot;dirt&quot;, &quot;dead shrub&quot;]</code>应该返回<code>[]</code> 。' - text: "<code>['andesite', 'grass', 'dirt', 'dead shrub'], ['andesite', 'grass', 'dirt', 'dead shrub']</code>应该返回<code>[]</code>。"
testString: assert.sameMembers(diffArray(["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: '<code>[&quot;andesite&quot;, &quot;grass&quot;, &quot;dirt&quot;, &quot;dead shrub&quot;], [&quot;andesite&quot;, &quot;grass&quot;, &quot;dirt&quot;, &quot;dead shrub&quot;]</code>应返回一个空数组。' - text: "<code>['andesite', 'grass', 'dirt', 'dead shrub'], ['andesite', 'grass', 'dirt', 'dead shrub']</code>应返回一个空数组。"
testString: assert(diffArray(["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]).length === 0); testString: assert(diffArray(["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]).length === 0);
- text: '<code>[1, 2, 3, 5], [1, 2, 3, 4, 5]</code>应该返回<code>[4]</code> 。' - text: <code>[1, 2, 3, 5], [1, 2, 3, 4, 5]</code>应该返回<code>[4]</code>
testString: assert.sameMembers(diffArray([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: '<code>[1, 2, 3, 5], [1, 2, 3, 4, 5]</code>应该返回一个带有一个项目的数组。' - text: <code>[1, 2, 3, 5], [1, 2, 3, 4, 5]</code>应该返回一个长度为 1 的数组。
testString: assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1); testString: assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1);
- text: '<code>[1, &quot;calf&quot;, 3, &quot;piglet&quot;], [1, &quot;calf&quot;, 3, 4]</code>应返回<code>[&quot;piglet&quot;, 4]</code> 。' - text: "<code>[1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]</code>应返回<code>['piglet', 4]</code>。"
testString: assert.sameMembers(diffArray([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: '<code>[1, &quot;calf&quot;, 3, &quot;piglet&quot;], [1, &quot;calf&quot;, 3, 4]</code>应该返回一个包含两个项目的数组。' - text: "<code>[1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]</code>应该返回一个长度为 2 的数组。"
testString: assert(diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]).length === 2); testString: assert(diffArray([1, "calf", 3, "piglet"], [1, "calf", 3, 4]).length === 2);
- text: '<code>[], [&quot;snuffleupagus&quot;, &quot;cookie monster&quot;, &quot;elmo&quot;]</code>应该返回<code>[&quot;snuffleupagus&quot;, &quot;cookie monster&quot;, &quot;elmo&quot;]</code> 。' - text: "<code>[], ['snuffleupagus', 'cookie monster', 'elmo']</code>应该返回<code>['snuffleupagus', 'cookie monster', 'elmo']</code>。"
testString: assert.sameMembers(diffArray([], ["snuffleupagus", "cookie monster", "elmo"]), ["snuffleupagus", "cookie monster", "elmo"]); testString: assert.sameMembers(diffArray([], ["snuffleupagus", "cookie monster", "elmo"]), ["snuffleupagus", "cookie monster", "elmo"]);
- text: '<code>[], [&quot;snuffleupagus&quot;, &quot;cookie monster&quot;, &quot;elmo&quot;]</code>应该返回一个包含三个项目的数组。' - text: "<code>[], ['snuffleupagus', 'cookie monster', 'elmo']</code>应该返回一个长度为 3 的数组。"
testString: assert(diffArray([], ["snuffleupagus", "cookie monster", "elmo"]).length === 3); testString: assert(diffArray([], ["snuffleupagus", "cookie monster", "elmo"]).length === 3);
- text: '<code>[1, &quot;calf&quot;, 3, &quot;piglet&quot;], [7, &quot;filly&quot;]</code> <code>[1, &quot;calf&quot;, 3, &quot;piglet&quot;, 7, &quot;filly&quot;]</code> <code>[1, &quot;calf&quot;, 3, &quot;piglet&quot;], [7, &quot;filly&quot;]</code>应该返回<code>[1, &quot;calf&quot;, 3, &quot;piglet&quot;, 7, &quot;filly&quot;]</code> 。' - text: "<code>[1, 'calf', 3, 'piglet'], [7, 'filly']</code>应该返回<code>[1, 'calf', 3, 'piglet', 7, 'filly']</code>。"
testString: assert.sameMembers(diffArray([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: '<code>[1, &quot;calf&quot;, 3, &quot;piglet&quot;], [7, &quot;filly&quot;]</code>应该返回一个包含六个项目的数组。' - text: "<code>[1, 'calf', 3, 'piglet'], [7, 'filly']</code>应该返回一个长度为 6 的数组。"
testString: assert(diffArray([1, "calf", 3, "piglet"], [7, "filly"]).length === 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]); diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
``` ```
</div> </div>
@ -79,8 +82,28 @@ diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
## Solution ## Solution
<section id='solution'> <section id='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;
}
```
</section>

View File

@ -3,15 +3,22 @@ id: afd15382cdfb22c9efe8b7de
title: DNA Pairing title: DNA Pairing
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16009
localeTitle: DNA 配对 localeTitle: DNA 配对
--- ---
## Description ## Description
<section id="description"> DNA链缺少配对元素。获取每个字符获取其对并将结果作为二维数组返回。 <a href="http://en.wikipedia.org/wiki/Base_pair" target="_blank">碱基对</a>是一对AT和CG。将缺少的元素与提供的字符匹配。将提供的字符作为每个数组中的第一个元素返回。例如对于输入GCG返回[[“G”“C”][“C”“G”][“G”“C”]]字符及其对在一个中配对数组,并将所有数组分组到一个封装数组中。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
DNA 链缺少配对元素。对于每个字符,获取与其配对的元素,并将结果作为二维数组返回。
<a href="http://en.wikipedia.org/wiki/Base_pair" target="_blank">碱基对</a> 是一对 AT 和 CG。将缺少的元素与提供的字符匹配。
将提供的字符作为每个数组中的第一个元素返回。
例如,对于输入 GCG返回[[“G”, “C”][“C”, “G”][“G”, “C”]]。
字符及与其配对的元素在一个数组中。再将所有数组放到一个封装数组中。
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,11 +26,11 @@ localeTitle: DNA配对
```yml ```yml
tests: tests:
- text: '<code>pairElement(&quot;ATCGA&quot;)</code>应返回<code>[[&quot;A&quot;,&quot;T&quot;],[&quot;T&quot;,&quot;A&quot;],[&quot;C&quot;,&quot;G&quot;],[&quot;G&quot;,&quot;C&quot;],[&quot;A&quot;,&quot;T&quot;]]</code> 。' - text: "<code>pairElement('ATCGA')</code>应返回<code>[['A','T'],['T','A'],['C','G'],['G','C'],['A','T']]</code>。"
testString: assert.deepEqual(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: '<code>pairElement(&quot;TTGAG&quot;)</code>应返回<code>[[&quot;T&quot;,&quot;A&quot;],[&quot;T&quot;,&quot;A&quot;],[&quot;G&quot;,&quot;C&quot;],[&quot;A&quot;,&quot;T&quot;],[&quot;G&quot;,&quot;C&quot;]]</code> 。' - text: "<code>pairElement('TTGAG')</code>应返回<code>[['T','A'],['T','A'],['G','C'],['A','T'],['G','C']]</code>。"
testString: assert.deepEqual(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: '<code>pairElement(&quot;CTCTA&quot;)</code>应返回<code>[[&quot;C&quot;,&quot;G&quot;],[&quot;T&quot;,&quot;A&quot;],[&quot;C&quot;,&quot;G&quot;],[&quot;T&quot;,&quot;A&quot;],[&quot;A&quot;,&quot;T&quot;]]</code> 。' - text: "<code>pairElement('CTCTA')</code>应返回<code>[['C','G'],['T','A'],['C','G'],['T','A'],['A','T']]</code>。"
testString: assert.deepEqual(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"); pairElement("GCG");
``` ```
</div> </div>
@ -53,8 +59,17 @@ pairElement("GCG");
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -3,15 +3,19 @@ id: a5deed1811a43193f9f1c841
title: Drop it title: Drop it
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16010
localeTitle: 算了吧 localeTitle: 筛选出数组中满足条件的元素
--- ---
## Description ## Description
<section id="description">给定数组<code>arr</code> 迭代并从第一个元素0索引开始删除每个元素直到函数<code>func</code>在迭代元素通过它时返回<code>true</code> 。然后在条件满足后返回数组的其余部分,否则, <code>arr</code>应作为空数组返回。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
给定数组<code>arr</code>,从数组的第一个元素开始,用函数<code>func</code>来检查数组的每个元素并删除,直到某个元素传入函数<code>func</code>时返回<code>true</code>。函数最终的返回值也是一个数组,它由原数组中第一个使得<code>func</code><code>true</code>的元素及其之后的所有元素组成。
如果数组中的所有元素都不能让<code>func</code><code>true</code>,则返回空数组<code>[]</code>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,17 +23,17 @@ localeTitle: 算了吧
```yml ```yml
tests: tests:
- text: '<code>dropElements([1, 2, 3, 4], function(n) {return n &gt;= 3;})</code>应该返回<code>[3, 4]</code> 。' - text: <code>dropElements([1, 2, 3, 4], function(n) {return n >= 3;})</code>应该返回<code>[3, 4]</code>
testString: assert.deepEqual(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: '<code>dropElements([0, 1, 0, 1], function(n) {return n === 1;})</code>应该返回<code>[1, 0, 1]</code> <code>dropElements([0, 1, 0, 1], function(n) {return n === 1;})</code> <code>[1, 0, 1]</code> 。' - text: <code>dropElements([0, 1, 0, 1], function(n) {return n === 1;})</code>应该返回<code>[1, 0, 1]</code>
testString: assert.deepEqual(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: '<code>dropElements([1, 2, 3], function(n) {return n &gt; 0;})</code>应该返回<code>[1, 2, 3]</code> 。' - text: <code>dropElements([1, 2, 3], function(n) {return n > 0;})</code>应该返回<code>[1, 2, 3]</code>
testString: assert.deepEqual(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: '<code>dropElements([1, 2, 3, 4], function(n) {return n &gt; 5;})</code>应返回<code>[]</code> 。' - text: <code>dropElements([1, 2, 3, 4], function(n) {return n > 5;})</code>应返回<code>[]</code>
testString: assert.deepEqual(dropElements([1, 2, 3, 4], function(n) {return n > 5;}), []); testString: assert.deepEqual(dropElements([1, 2, 3, 4], function(n) {return n > 5;}), []);
- text: '<code>dropElements([1, 2, 3, 7, 4], function(n) {return n &gt; 3;})</code>应该返回<code>[7, 4]</code> <code>dropElements([1, 2, 3, 7, 4], function(n) {return n &gt; 3;})</code> <code>[7, 4]</code> 。' - text: <code>dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;})</code>应该返回<code>[7, 4]</code>
testString: assert.deepEqual(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: '<code>dropElements([1, 2, 3, 9, 2], function(n) {return n &gt; 2;})</code>应该返回<code>[3, 9, 2]</code> <code>dropElements([1, 2, 3, 9, 2], function(n) {return n &gt; 2;})</code> <code>[3, 9, 2]</code> 。' - text: <code>dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;})</code>应该返回<code>[3, 9, 2]</code>
testString: assert.deepEqual(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; }); dropElements([1, 2, 3], function(n) {return n < 3; });
``` ```
</div> </div>
@ -60,8 +63,15 @@ dropElements([1, 2, 3], function(n) {return n < 3; });
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```js
// solution required function dropElements(arr, func) {
// Drop them elements.
while (arr.length && !func(arr[0])) {
arr.shift();
}
return arr;
}
``` ```
/section> </section>

View File

@ -3,15 +3,21 @@ id: a10d2431ad0c6a099a4b8b52
title: Everything Be True title: Everything Be True
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16011
localeTitle: 一切都是真的 localeTitle: 真假值判断
--- ---
## Description ## Description
<section id="description">检查谓词(第二个参数)是否对集合的所有元素(第一个参数)都是<dfn>真实的</dfn> 。换句话说,您将获得一个对象的数组集合。谓语<code>pre</code>将一个对象的属性,你需要返回<code>true</code> ,如果它的值是<code>truthy</code> 。否则,返回<code>false</code> 。在JavaScript中 <code>truthy</code>值是在布尔上下文中计算时转换为<code>true</code>的值。请记住,您可以通过点表示法或<code>[]</code>表示法访问对象属性。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
检查谓词(第二个参数)是否对集合的所有元素(第一个参数)都是<code>truthy</code>(真实的)。
换句话说,你将获得一个对象的数组集合。谓词<code>pre</code>是一个对象的属性,如果它的值是<code>truthy</code>(真实的) ,则返回<code>true</code>,否则,返回<code>false</code>
JavaScript 中,如果一个值在 Boolean 的上下文中(比如<code>if</code>语句)可以被执行为<code>true</code>,那么这个值就被认为是<code>truthy</code>的。
注意,你可以选择使用<code>.</code><code>[]</code>来访问对象属性对应的值。
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,23 +25,23 @@ localeTitle: 一切都是真的
```yml ```yml
tests: tests:
- text: '<code>truthCheck([{&quot;user&quot;: &quot;Tinky-Winky&quot;, &quot;sex&quot;: &quot;male&quot;}, {&quot;user&quot;: &quot;Dipsy&quot;, &quot;sex&quot;: &quot;male&quot;}, {&quot;user&quot;: &quot;Laa-Laa&quot;, &quot;sex&quot;: &quot;female&quot;}, {&quot;user&quot;: &quot;Po&quot;, &quot;sex&quot;: &quot;female&quot;}], &quot;sex&quot;)</code>应该返回true。' - text: '<code>truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")</code>应该返回<code>true</code>。'
testString: 'assert.strictEqual(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: '<code>truthCheck([{&quot;user&quot;: &quot;Tinky-Winky&quot;, &quot;sex&quot;: &quot;male&quot;}, {&quot;user&quot;: &quot;Dipsy&quot;}, {&quot;user&quot;: &quot;Laa-Laa&quot;, &quot;sex&quot;: &quot;female&quot;}, {&quot;user&quot;: &quot;Po&quot;, &quot;sex&quot;: &quot;female&quot;}], &quot;sex&quot;)</code>应该返回false。' - text: '<code>truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")</code>应该返回<code>false</code>。'
testString: 'assert.strictEqual(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: '<code>truthCheck([{&quot;user&quot;: &quot;Tinky-Winky&quot;, &quot;sex&quot;: &quot;male&quot;, &quot;age&quot;: 0}, {&quot;user&quot;: &quot;Dipsy&quot;, &quot;sex&quot;: &quot;male&quot;, &quot;age&quot;: 3}, {&quot;user&quot;: &quot;Laa-Laa&quot;, &quot;sex&quot;: &quot;female&quot;, &quot;age&quot;: 5}, {&quot;user&quot;: &quot;Po&quot;, &quot;sex&quot;: &quot;female&quot;, &quot;age&quot;: 4}], &quot;age&quot;)</code>应该返回false。' - text: '<code>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")</code>应该返回<code>false</code>。'
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);' 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: '<code>truthCheck([{&quot;name&quot;: &quot;Pete&quot;, &quot;onBoat&quot;: true}, {&quot;name&quot;: &quot;Repeat&quot;, &quot;onBoat&quot;: true}, {&quot;name&quot;: &quot;FastFoward&quot;, &quot;onBoat&quot;: null}], &quot;onBoat&quot;)</code>应该返回false' - text: '<code>truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastFoward", "onBoat": null}], "onBoat")</code>应该返回<code>false</code>。'
testString: 'assert.strictEqual(truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastForward", "onBoat": null}], "onBoat"), false);' testString: 'assert.strictEqual(truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastFoward", "onBoat": null}], "onBoat"), false);'
- text: '<code>truthCheck([{&quot;name&quot;: &quot;Pete&quot;, &quot;onBoat&quot;: true}, {&quot;name&quot;: &quot;Repeat&quot;, &quot;onBoat&quot;: true, &quot;alias&quot;: &quot;Repete&quot;}, {&quot;name&quot;: &quot;FastFoward&quot;, &quot;onBoat&quot;: true}], &quot;onBoat&quot;)</code>应该返回true' - text: '<code>truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastFoward", "onBoat": true}], "onBoat")</code>应该返回<code>true</code>。'
testString: 'assert.strictEqual(truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastForward", "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: '<code>truthCheck([{&quot;single&quot;: &quot;yes&quot;}], &quot;single&quot;)</code>应该返回true' - text: '<code>truthCheck([{"single": "yes"}], "single")</code>应该返回<code>true</code>。'
testString: 'assert.strictEqual(truthCheck([{"single": "yes"}], "single"), true);' testString: 'assert.strictEqual(truthCheck([{"single": "yes"}], "single"), true);'
- text: '<code>truthCheck([{&quot;single&quot;: &quot;&quot;}, {&quot;single&quot;: &quot;double&quot;}], &quot;single&quot;)</code>应该返回false' - text: '<code>truthCheck([{"single": ""}, {"single": "double"}], "single")</code>应该返回<code>false</code>。'
testString: 'assert.strictEqual(truthCheck([{"single": ""}, {"single": "double"}], "single"), false);' testString: 'assert.strictEqual(truthCheck([{"single": ""}, {"single": "double"}], "single"), false);'
- text: '<code>truthCheck([{&quot;single&quot;: &quot;double&quot;}, {&quot;single&quot;: undefined}], &quot;single&quot;)</code>应返回false' - text: '<code>truthCheck([{"single": "double"}, {"single": undefined}], "single")</code>应返回<code>false</code>。'
testString: 'assert.strictEqual(truthCheck([{"single": "double"}, {"single": undefined}], "single"), false);' testString: 'assert.strictEqual(truthCheck([{"single": "double"}, {"single": undefined}], "single"), false);'
- text: '<code>truthCheck([{&quot;single&quot;: &quot;double&quot;}, {&quot;single&quot;: NaN}], &quot;single&quot;)</code>应返回false' - text: '<code>truthCheck([{"single": "double"}, {"single": NaN}], "single")</code>应返回<code>false</code>。'
testString: 'assert.strictEqual(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"); truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
``` ```
</div> </div>
@ -66,8 +71,12 @@ truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "ma
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```js
// solution required function truthCheck(collection, pre) {
// Does everyone have one of these?
return collection.every(function(e) { return e[pre]; });
}
``` ```
/section> </section>

View File

@ -2,15 +2,32 @@
id: a2f1d72d9b908d0bd72bb9f6 id: a2f1d72d9b908d0bd72bb9f6
title: Make a Person title: Make a Person
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16020
localeTitle: 做一个人 localeTitle: 构造一个 Person 类
--- ---
## Description ## Description
<section id="description">使用以下方法填写对象构造函数: <blockquote> getFirstNamegetLastNamegetFullNamesetFirstNamefirstsetLastNamelastsetFullNamefirstAndLast </blockquote>运行测试以查看每个方法的预期输出。采用参数的方法必须只接受一个参数,并且必须是一个字符串。这些方法必须是与对象交互的唯一可用方法。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
在这道题目中我们需要写一个构造器constructor函数。它只接收一个字符串参数<code>firstAndLast</code>,这个参数代表一个英文名的全名(姓和名)。这个构造函数创建出的实例需要具有以下方法:
```js
getFirstName()
getLastName()
getFullName()
setFirstName(first)
setLastName(last)
setFullName(firstAndLast)
```
你可以点击 “运行测试”,然后就可以在底下的控制台中看到每个测试用例执行的情况。
方法接收一个字符串格式的参数。
这些方法必须是与对象进行交互的唯一可用方法。
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -20,27 +37,27 @@ localeTitle: 做一个人
tests: tests:
- text: <code>Object.keys(bob).length</code>应该返回 6。 - text: <code>Object.keys(bob).length</code>应该返回 6。
testString: assert.deepEqual(Object.keys(bob).length, 6); testString: assert.deepEqual(Object.keys(bob).length, 6);
- text: <code>bob instanceof Person</code>应该返回true - text: <code>bob instanceof Person</code>应该返回<code>true</code>
testString: assert.deepEqual(bob instanceof Person, true); testString: assert.deepEqual(bob instanceof Person, true);
- text: <code>bob.firstName</code>应返回undefined。 - text: <code>bob.firstName</code>应返回<code>undefined</code>
testString: assert.deepEqual(bob.firstName, undefined); testString: assert.deepEqual(bob.firstName, undefined);
- text: <code>bob.lastName</code>应返回undefined。 - text: <code>bob.lastName</code>应返回<code>undefined</code>
testString: assert.deepEqual(bob.lastName, undefined); testString: assert.deepEqual(bob.lastName, undefined);
- text: <code>bob.getFirstName()</code>应返回Bob”。 - text: "<code>bob.getFirstName()</code>应返回 'Bob'。"
testString: assert.deepEqual(bob.getFirstName(), 'Bob'); testString: assert.deepEqual(bob.getFirstName(), 'Bob');
- text: <code>bob.getLastName()</code>应返回Ross”。 - text: "<code>bob.getLastName()</code>应返回 'Ross'。"
testString: assert.deepEqual(bob.getLastName(), 'Ross'); testString: assert.deepEqual(bob.getLastName(), 'Ross');
- text: <code>bob.getFullName()</code>应该返回Bob Ross”。 - text: "<code>bob.getFullName()</code>应该返回 'Bob Ross'。"
testString: assert.deepEqual(bob.getFullName(), 'Bob Ross'); testString: assert.deepEqual(bob.getFullName(), 'Bob Ross');
- text: <code>bob.getFullName()</code>应该在<code>bob.setFirstName(&quot;Haskell&quot;)</code>之后返回“Haskell Ross”。 - text: "调用<code>bob.setFirstName('Haskell')</code>之后,<code>bob.getFullName()</code>应该返回 'Haskell Ross'。"
testString: assert.strictEqual((function () { bob.setFirstName("Haskell"); return bob.getFullName(); })(), 'Haskell Ross'); testString: assert.strictEqual((function () { bob.setFirstName("Haskell"); return bob.getFullName(); })(), 'Haskell Ross');
- text: <code>bob.getFullName()</code>应该在<code>bob.setLastName(&quot;Curry&quot;)</code>之后返回“Haskell Curry”。 - text: "调用<code>bob.setLastName('Curry')</code>之后,<code>bob.getFullName()</code>应该返回 'Haskell Curry'。"
testString: assert.strictEqual((function () { var _bob=new Person('Haskell Ross'); _bob.setLastName("Curry"); return _bob.getFullName(); })(), 'Haskell Curry'); testString: assert.strictEqual((function () { var _bob=new Person('Haskell Ross'); _bob.setLastName("Curry"); return _bob.getFullName(); })(), 'Haskell Curry');
- text: <code>bob.getFullName()</code>应该在<code>bob.setFullName(&quot;Haskell Curry&quot;)</code>之后返回“Haskell Curry”。 - text: "调用<code>bob.setFullName('Haskell Curry')</code>之后,<code>bob.getFullName()</code>应该返回 'Haskell Curry'。"
testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getFullName(); })(), 'Haskell Curry'); testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getFullName(); })(), 'Haskell Curry');
- text: <code>bob.getFirstName()</code>应该在<code>bob.setFullName(&quot;Haskell Curry&quot;)</code>之后返回“Haskell”。 - text: "调用<code>bob.setFullName('Haskell Curry')</code>之后,<code>bob.getFirstName()</code>应该返回 'Haskell'。"
testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getFirstName(); })(), 'Haskell'); testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return bob.getFirstName(); })(), 'Haskell');
- text: <code>bob.getLastName()</code>应该在<code>bob.setFullName(&quot;Haskell Curry&quot;)</code>之后返回“Curry”。 - text: "调用<code>bob.setFullName('Haskell Curry')</code>之后,<code>bob.getLastName()</code>应该返回 'Curry'。"
testString: assert.strictEqual((function () { bob.setFullName("Haskell Curry"); return 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'); var bob = new Person('Bob Ross');
bob.getFullName(); bob.getFullName();
``` ```
</div> </div>
@ -75,8 +91,47 @@ bob.getFullName();
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -2,15 +2,23 @@
id: af4afb223120f7348cdfc9fd id: af4afb223120f7348cdfc9fd
title: Map the Debris title: Map the Debris
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16021
localeTitle: 映射碎片 localeTitle: 绘制碎片
--- ---
## Description ## Description
<section id="description">返回一个新数组,将元素的平均高度转换为轨道周期(以秒为单位)。该数组将包含<code>{name: &#39;name&#39;, avgAlt: avgAlt}</code>格式的对象。您可以<a href="http://en.wikipedia.org/wiki/Orbital_period" target="_blank">在维基百科上</a>阅读有关轨道周期的<a href="http://en.wikipedia.org/wiki/Orbital_period" target="_blank">信息</a> 。值应四舍五入到最接近的整数。轨道上的身体是地球。地球半径为6367.4447公里地球的GM值为398600.4418 km <sup>3</sup> s <sup>-2</sup> 。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
在这道题目中,我们需要写一个计算天体轨道周期的函数,它接收一个对象数组参数<code>arr</code>,对象中包含表示天体名称的<code>name</code>属性,及表示天体表面平均海拔的<code>avgAlt</code>属性。就像这样:<code>{name: 'name', avgAlt: avgAlt}</code>
这个函数的返回值也是一个对象数组,应保留原对象中的<code>name</code>属性和值,然后根据<code>avgAlt</code>属性的值求出轨道周期(单位是秒),并赋值给<code>orbitalPeriod</code>属性。返回值中不应保留原数据中的<code>avgAlt</code>属性及其对应的值。
你可以在这条<a href="http://en.wikipedia.org/wiki/Orbital_period" target='_blank'>维基百科</a>的链接中找到轨道周期的计算公式。
轨道周期的计算以地球为基准(即环绕地球的轨道周期),计算结果应取整到最接近的整数。
地球的半径是 6367.4447 千米,地球的 GM 值为 398600.4418 km<sup>3</sup>s<sup>-2</sup>
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -18,9 +26,9 @@ localeTitle: 映射碎片
```yml ```yml
tests: tests:
- text: '<code>orbitalPeriod([{name : &quot;sputnik&quot;, avgAlt : 35873.5553}])</code>应返回<code>[{name: &quot;sputnik&quot;, orbitalPeriod: 86400}]</code> 。' - text: '<code>orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}])</code>应返回<code>[{name: "sputnik", orbitalPeriod: 86400}]</code>。'
testString: 'assert.deepEqual(orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]), [{name: "sputnik", orbitalPeriod: 86400}]);' testString: 'assert.deepEqual(orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]), [{name: "sputnik", orbitalPeriod: 86400}]);'
- text: '<code>orbitalPeriod([{name: &quot;iss&quot;, avgAlt: 413.6}, {name: &quot;hubble&quot;, avgAlt: 556.7}, {name: &quot;moon&quot;, avgAlt: 378632.553}])</code>应返回<code>[{name : &quot;iss&quot;, orbitalPeriod: 5557}, {name: &quot;hubble&quot;, orbitalPeriod: 5734}, {name: &quot;moon&quot;, orbitalPeriod: 2377399}]</code> 。' - text: '<code>orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}])</code>应返回<code>[{name : "iss", orbitalPeriod: 5557}, {name: "hubble", orbitalPeriod: 5734}, {name: "moon", orbitalPeriod: 2377399}]</code>。'
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}]);' 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}]); orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
``` ```
</div> </div>
@ -52,8 +59,22 @@ orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -3,15 +3,19 @@ id: af7588ade1100bde429baf20
title: Missing letters title: Missing letters
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' localeTitle: 丢失的字母
localeTitle: 遗失的信件
--- ---
## Description ## Description
<section id="description">在传递的字母范围内找到丢失的字母并将其返回。如果范围内存在所有字母则返回undefined。如果卡住请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
在这道题目中,我们需要写一个函数,找到传入的字符串里缺失的字母并返回它。
判断缺失的依据是字母顺序,比如 abcdfg 中缺失了 e。而 abcdef 中就没有字母缺失,此时我们需要返回<code>undefined</code>
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,15 +23,15 @@ localeTitle: 遗失的信件
```yml ```yml
tests: tests:
- text: <code>fearNotLetter(&quot;abce&quot;)</code>应返回“d”。 - text: "<code>fearNotLetter('abce')</code>应返回 'd'。"
testString: assert.deepEqual(fearNotLetter('abce'), 'd'); testString: assert.deepEqual(fearNotLetter('abce'), 'd');
- text: <code>fearNotLetter(&quot;abcdefghjklmno&quot;)</code>应该返回“i”。 - text: "<code>fearNotLetter('abcdefghjklmno')</code>应该返回 'i'。"
testString: assert.deepEqual(fearNotLetter('abcdefghjklmno'), 'i'); testString: assert.deepEqual(fearNotLetter('abcdefghjklmno'), 'i');
- text: <code>fearNotLetter(&quot;stvwx&quot;)</code>应该返回“u”。 - text: "<code>fearNotLetter('stvwx')</code>应该返回 'u'。"
testString: assert.deepEqual(fearNotLetter('stvwx'), 'u'); testString: assert.deepEqual(fearNotLetter('stvwx'), 'u');
- text: <code>fearNotLetter(&quot;bcdf&quot;)</code>应返回“e”。 - text: "<code>fearNotLetter('bcdf')</code>应返回 'e'。"
testString: assert.deepEqual(fearNotLetter('bcdf'), 'e'); testString: assert.deepEqual(fearNotLetter('bcdf'), 'e');
- text: <code>fearNotLetter(&quot;abcdefghijklmnopqrstuvwxyz&quot;)</code>应返回undefined - text: "<code>fearNotLetter('abcdefghijklmnopqrstuvwxyz')</code>应返回<code>undefined</code>。"
testString: assert.isUndefined(fearNotLetter('abcdefghijklmnopqrstuvwxyz')); testString: assert.isUndefined(fearNotLetter('abcdefghijklmnopqrstuvwxyz'));
``` ```
@ -45,7 +49,6 @@ function fearNotLetter(str) {
} }
fearNotLetter("abce"); fearNotLetter("abce");
``` ```
</div> </div>
@ -57,8 +60,18 @@ fearNotLetter("abce");
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -3,15 +3,23 @@ id: aa7697ea2477d1316795783b
title: Pig Latin title: Pig Latin
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16039
localeTitle: 猪拉丁文 localeTitle: 儿童黑话
--- ---
## Description ## Description
<section id="description">将提供的字符串翻译为pig latin。 <a href="http://en.wikipedia.org/wiki/Pig_Latin" target="_blank">Pig Latin</a>使用英语单词的第一个辅音或辅音簇将其移到单词的末尾并加上“ay”后缀。如果一个单词以元音开头你只需添加“way”到最后。输入字符串保证全部为小写英文单词。如果卡住请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
在这道题目中,我们需要写一个函数,把传入的字符串翻译成“儿童黑话”。
<a href="http://en.wikipedia.org/wiki/Pig_Latin" target="_blank">儿童黑话</a>的基本转换规则很简单,只需要把一个英文单词的第一个辅音字母或第一组辅音簇移到单词的结尾,并在后面加上<code>ay</code>即可。在英语中,字母 a、e、i、o、u 为元音,其余的字母均为辅音。辅音簇的意思是连续的多个辅音字母。
额外地,如果单词本身是以元音开头的,那只需要在结尾加上<code>way</code>
额外地,如果单词不包含元音,那只需要在结尾加上<code>ay</code>
在本题中,传入的单词一定会是英文单词,且所有字母均为小写。
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,19 +27,19 @@ localeTitle: 猪拉丁文
```yml ```yml
tests: tests:
- text: <code>translatePigLatin(&quot;california&quot;)</code>应该返回aliforniacay”。 - text: "<code>translatePigLatin('california')</code>应该返回 'aliforniacay'。"
testString: assert.deepEqual(translatePigLatin("california"), "aliforniacay"); testString: assert.deepEqual(translatePigLatin("california"), "aliforniacay");
- text: <code>translatePigLatin(&quot;paragraphs&quot;)</code>应该返回aragraphspay”。 - text: "<code>translatePigLatin('paragraphs')</code>应该返回 'aragraphspay'。"
testString: assert.deepEqual(translatePigLatin("paragraphs"), "aragraphspay"); testString: assert.deepEqual(translatePigLatin("paragraphs"), "aragraphspay");
- text: <code>translatePigLatin(&quot;glove&quot;)</code>应该返回oveglay”。 - text: "<code>translatePigLatin('glove')</code>应该返回 'oveglay'。"
testString: assert.deepEqual(translatePigLatin("glove"), "oveglay"); testString: assert.deepEqual(translatePigLatin("glove"), "oveglay");
- text: <code>translatePigLatin(&quot;algorithm&quot;)</code>应返回algorithmway”。 - text: "<code>translatePigLatin('algorithm')</code>应返回 'algorithmway'。"
testString: assert.deepEqual(translatePigLatin("algorithm"), "algorithmway"); testString: assert.deepEqual(translatePigLatin("algorithm"), "algorithmway");
- text: <code>translatePigLatin(&quot;eight&quot;)</code>应该返回“八通”。 - text: "<code>translatePigLatin('eight')</code>应该返回 'eightway'。"
testString: assert.deepEqual(translatePigLatin("eight"), "eightway"); testString: assert.deepEqual(translatePigLatin("eight"), "eightway");
- text: 应该处理第一个元音出现在单词末尾的单词。 - text: "你的代码应该处理第一个 vowel 在单词中间的情况。比如<code>translatePigLatin('schwartz')</code> 应该返回 'artzschway'"
testString: assert.deepEqual(translatePigLatin("schwartz"), "artzschway"); testString: assert.deepEqual(translatePigLatin("schwartz"), "artzschway");
- text: 应该处理没有元音的单词。 - text: "你的代码应当能够处理单词中不含元音字母的情况。比如<code>translatePigLatin('rhythm')</code>应该返回 'rhythmay'。"
testString: assert.deepEqual(translatePigLatin("rhythm"), "rhythmay"); testString: assert.deepEqual(translatePigLatin("rhythm"), "rhythmay");
``` ```
@ -49,7 +57,6 @@ function translatePigLatin(str) {
} }
translatePigLatin("consonant"); translatePigLatin("consonant");
``` ```
</div> </div>
@ -61,8 +68,20 @@ translatePigLatin("consonant");
## Solution ## Solution
<section id='solution'> <section id='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;
}
```
</section>

View File

@ -3,15 +3,23 @@ id: a0b5010f579e69b815e7c5d6
title: Search and Replace title: Search and Replace
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16045
localeTitle: 搜索和替换 localeTitle: 搜索和替换
--- ---
## Description ## Description
<section id="description">使用提供的参数执行搜索并替换句子并返回新句子。第一个参数是执行搜索和替换的句子。第二个参数是你要替换的词(之前)。第三个参数是你将用(后)替换第二个参数。 <strong>注意</strong> <br>在更换原始单词时保留原始单词中第一个字符的大小写。例如如果您的意思是将“Book”替换为“dog”则应将其替换为“Dog”。如果卡住请记住使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
在这道题目中,我们需要写一个字符串的搜索与替换函数,它的返回值为完成替换后的新字符串。
这个函数接收的第一个参数为待替换的句子。
第二个参数为句中需要被替换的单词。
第三个参数为替换后的单词。
<strong>注意:</strong><br> 你需要保留被替换单词首字母的大小写格式。即如果传入的第二个参数为 "Book",第三个参数为 "dog",那么替换后的结果应为 "Dog"
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,15 +27,15 @@ localeTitle: 搜索和替换
```yml ```yml
tests: tests:
- text: '<code>myReplace(&quot;Let us go to the store&quot;, &quot;store&quot;, &quot;mall&quot;)</code>应该返回“让我们去商场”。' - text: "<code>myReplace('Let us go to the store', 'store', 'mall')</code>应该返回 'Let us go to the mall'。"
testString: assert.deepEqual(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: '<code>myReplace(&quot;He is Sleeping on the couch&quot;, &quot;Sleeping&quot;, &quot;sitting&quot;)</code>应该回归“他正坐在沙发上”。' - text: "<code>myReplace('He is Sleeping on the couch', 'Sleeping', 'sitting')</code>应该返回 'He is Sitting on the couch'。"
testString: assert.deepEqual(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: '<code>myReplace(&quot;This has a spellngi error&quot;, &quot;spellngi&quot;, &quot;spelling&quot;)</code>应该返回“这有一个拼写错误”。' - text: "<code>myReplace('This has a spellngi error', 'spellngi', 'spelling')</code>应该返回 'This has a spelling error'。"
testString: assert.deepEqual(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: '<code>myReplace(&quot;His name is Tom&quot;, &quot;Tom&quot;, &quot;john&quot;)</code>应该回归“他的名字是约翰”。' - text: "<code>myReplace('His name is Tom', 'Tom', 'john')</code>应该返回 'His name is John'。"
testString: assert.deepEqual(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: '<code>myReplace(&quot;Let us get back to more Coding&quot;, &quot;Coding&quot;, &quot;algorithms&quot;)</code>应该返回“让我们回到更多算法”。' - text: "<code>myReplace('Let us get back to more Coding', 'Coding', 'algorithms')</code>应该返回 '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"); 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"); myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
``` ```
</div> </div>
@ -57,8 +64,16 @@ myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -3,15 +3,20 @@ id: a39963a4c10bc8b4d4f06d7e
title: Seek and Destroy title: Seek and Destroy
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16046
localeTitle: 寻找和摧毁 localeTitle: 瞄准和消灭
--- ---
## Description ## Description
<section id="description">您将获得一个初始数组(驱逐舰函数中的第一个参数),后跟一个或多个参数。从初始数组中删除与这些参数具有相同值的所有元素。 <strong>注意</strong> <br>你必须使用<code>arguments</code>对象。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section> <section id='description'>
在这道题目中,我们要写一个叫<code>destroyer</code>的函数。传给它的第一个参数是数组,我们称他为初始数组。后续的参数数量是不确定的,可能有一个或多个。你需要做的是,从初始数组中移除所有与后续参数相等的元素,并返回移除元素后的数组。
<strong>注意:</strong><br> 你可以使用<code>arguments</code>对象,也可以使用<code>...</code>即“剩余参数”Rest Parameters语法。
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,17 +24,17 @@ localeTitle: 寻找和摧毁
```yml ```yml
tests: tests:
- text: '<code>destroyer([1, 2, 3, 1, 2, 3], 2, 3)</code>应该返回<code>[1, 1]</code> 。' - text: <code>destroyer([1, 2, 3, 1, 2, 3], 2, 3)</code>应该返回<code>[1, 1]</code>
testString: assert.deepEqual(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: '<code>destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3)</code>应该返回<code>[1, 5, 1]</code> 。' - text: <code>destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3)</code>应该返回<code>[1, 5, 1]</code>
testString: assert.deepEqual(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: '<code>destroyer([3, 5, 1, 2, 2], 2, 3, 5)</code>应该返回<code>[1]</code> 。' - text: <code>destroyer([3, 5, 1, 2, 2], 2, 3, 5)</code>应该返回<code>[1]</code>
testString: assert.deepEqual(destroyer([3, 5, 1, 2, 2], 2, 3, 5), [1]); testString: assert.deepEqual(destroyer([3, 5, 1, 2, 2], 2, 3, 5), [1]);
- text: '<code>destroyer([2, 3, 2, 3], 2, 3)</code>应该返回<code>[]</code> 。' - text: <code>destroyer([2, 3, 2, 3], 2, 3)</code>应该返回<code>[]</code>
testString: assert.deepEqual(destroyer([2, 3, 2, 3], 2, 3), []); testString: assert.deepEqual(destroyer([2, 3, 2, 3], 2, 3), []);
- text: '<code>destroyer([&quot;tree&quot;, &quot;hamburger&quot;, 53], &quot;tree&quot;, 53)</code>应该返回<code>[&quot;hamburger&quot;]</code> 。' - text: "<code>destroyer(['tree', 'hamburger', 53], 'tree', 53)</code>应该返回<code>['hamburger']</code>。"
testString: assert.deepEqual(destroyer(["tree", "hamburger", 53], "tree", 53), ["hamburger"]); testString: assert.deepEqual(destroyer(["tree", "hamburger", 53], "tree", 53), ["hamburger"]);
- text: '<code>destroyer([&quot;possum&quot;, &quot;trollo&quot;, 12, &quot;safari&quot;, &quot;hotdog&quot;, 92, 65, &quot;grandma&quot;, &quot;bugati&quot;, &quot;trojan&quot;, &quot;yacht&quot;], &quot;yacht&quot;, &quot;possum&quot;, &quot;trollo&quot;, &quot;safari&quot;, &quot;hotdog&quot;, &quot;grandma&quot;, &quot;bugati&quot;, &quot;trojan&quot;)</code>应该返回<code>[12,92,65]</code> 。' - text: "<code>destroyer(['possum', 'trollo', 12, 'safari', 'hotdog', 92, 65, 'grandma', 'bugati', 'trojan', 'yacht'], 'yacht', 'possum', 'trollo', 'safari', 'hotdog', 'grandma', 'bugati', 'trojan')</code>应该返回<code>[12,92,65]</code>。"
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]); 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); destroyer([1, 2, 3, 1, 2, 3], 2, 3);
``` ```
</div> </div>
@ -60,8 +64,19 @@ destroyer([1, 2, 3, 1, 2, 3], 2, 3);
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -3,15 +3,21 @@ id: ae9defd7acaf69703ab432ea
title: Smallest Common Multiple title: Smallest Common Multiple
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16075
localeTitle: 最小的共同多重 localeTitle: 最小公倍数
--- ---
## Description ## Description
<section id="description">找到所提供参数的最小公倍数可以均匀地除以这些参数以及这些参数之间范围内的所有序号。范围将是两个数字的数组不一定按数字顺序排列。例如如果给定1和3找到1和3的最小公倍数它们也可以被1到3 <em>之间</em>的所有数字整除。这里的答案是6.记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a>如果你得到卡住。尝试配对程序。编写自己的代码。 </section> <section id='description'>
在这道题目中,我们需要写一个函数,它接收一个包含两个数字的数组参数<code>arr</code>,它的返回值为这两个数字范围内所有数字(包含这两个数字)的最小公倍数。
注意,较小数不一定总是出现在数组的第一个元素。
比如,传入<code>[1, 3]</code>,那么函数的返回结果应为 1、2、3 的最小公倍数,即为 6。
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,17 +25,17 @@ localeTitle: 最小的共同多重
```yml ```yml
tests: tests:
- text: '<code>smallestCommons([1, 5])</code>应返回一个数字。' - text: <code>smallestCommons([1, 5])</code>应返回一个数字。
testString: assert.deepEqual(typeof smallestCommons([1, 5]), 'number'); testString: assert.deepEqual(typeof smallestCommons([1, 5]), 'number');
- text: '<code>smallestCommons([1, 5])</code>应该返回60。' - text: <code>smallestCommons([1, 5])</code>应该返回 60。
testString: assert.deepEqual(smallestCommons([1, 5]), 60); testString: assert.deepEqual(smallestCommons([1, 5]), 60);
- text: '<code>smallestCommons([5, 1])</code>应该返回60。' - text: <code>smallestCommons([5, 1])</code>应该返回 60。
testString: assert.deepEqual(smallestCommons([5, 1]), 60); testString: assert.deepEqual(smallestCommons([5, 1]), 60);
- text: '<code>smallestCommons([2, 10])</code> 2,10 <code>smallestCommons([2, 10])</code>应返回2520。' - text: <code>smallestCommons([2, 10])</code>应返回 2520。.
testString: assert.deepEqual(smallestCommons([2, 10]), 2520); testString: assert.deepEqual(smallestCommons([2, 10]), 2520);
- text: '<code>smallestCommons([1, 13])</code> 1,13 <code>smallestCommons([1, 13])</code>应返回360360。' - text: <code>smallestCommons([1, 13])</code>应返回 360360。
testString: assert.deepEqual(smallestCommons([1, 13]), 360360); testString: assert.deepEqual(smallestCommons([1, 13]), 360360);
- text: '<code>smallestCommons([23, 18])</code> 23,18 <code>smallestCommons([23, 18])</code>应返回6056820。' - text: <code>smallestCommons([23, 18])</code>应返回 6056820。
testString: assert.deepEqual(smallestCommons([23, 18]), 6056820); testString: assert.deepEqual(smallestCommons([23, 18]), 6056820);
``` ```
@ -48,7 +54,6 @@ function smallestCommons(arr) {
smallestCommons([1,5]); smallestCommons([1,5]);
``` ```
</div> </div>
@ -60,8 +65,27 @@ smallestCommons([1,5]);
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -3,15 +3,22 @@ id: a105e963526e7de52b219be9
title: Sorted Union title: Sorted Union
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16077
localeTitle: 排序联盟 localeTitle: 集合排序
--- ---
## Description ## Description
<section id="description">编写一个带有两个或更多数组的函数,并按原始提供的数组的顺序返回一个新的唯一值数组。换句话说,所有数组中存在的所有值都应包含在它们的原始顺序中,但在最终数组中没有重复。唯一编号应按其原始顺序排序,但最终数组不应按数字顺序排序。检查断言测试以获取示例。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
在这道题目中,我们需要写一个函数,它接收两个或多个数组为参数。我们需要对这些数组中所有元素进行去除重复元素的处理,并以数组的形式返回去重结果。
换句话说,所有数组中出现的所有值都应按其原始顺序包括在内,但最终数组中不得重复。
唯一数字应按其原始顺序排序,但最终数组不应按数字顺序排序。
如有疑问,请先浏览下方的测试用例。
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,11 +26,11 @@ localeTitle: 排序联盟
```yml ```yml
tests: tests:
- text: '<code>uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])</code>应该返回<code>[1, 3, 2, 5, 4]</code> <code>uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])</code> <code>[1, 3, 2, 5, 4]</code> 。' - text: <code>uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])</code>应该返回<code>[1, 3, 2, 5, 4]</code>
testString: assert.deepEqual(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: '<code>uniteUnique([1, 2, 3], [5, 2, 1])</code>应该返回<code>[1, 2, 3, 5]</code> <code>uniteUnique([1, 2, 3], [5, 2, 1])</code> <code>[1, 2, 3, 5]</code> 。' - text: <code>uniteUnique([1, 2, 3], [5, 2, 1])</code>应该返回<code>[1, 2, 3, 5]</code>
testString: assert.deepEqual(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: '<code>uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8])</code>应该返回<code>[1, 2, 3, 5, 4, 6, 7, 8]</code> <code>uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8])</code> <code>[1, 2, 3, 5, 4, 6, 7, 8]</code> 。' - text: <code>uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8])</code>应该返回<code>[1, 2, 3, 5, 4, 6, 7, 8]</code>
testString: assert.deepEqual(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]); uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
``` ```
</div> </div>
@ -53,8 +59,13 @@ uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -3,15 +3,19 @@ id: a103376db3ba46b2d50db289
title: Spinal Tap Case title: Spinal Tap Case
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16078
localeTitle: 脊椎龙头套 localeTitle: 短线连接格式
--- ---
## Description ## Description
<section id="description">将字符串转换为脊柱案例。脊柱情况是全小写单词连接的破折号。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
在这道题目中,我们需要写一个函数,把一个字符串转换为“短线连接格式”。短线连接格式的意思是,所有字母都是小写,且用<code>-</code>连接。比如,对于<code>Hello World</code>,应该转换为<code>hello-world</code>;对于<code>I love_Javascript-VeryMuch</code>,应该转换为<code>i-love-javascript-very-much</code>
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,15 +23,15 @@ localeTitle: 脊椎龙头套
```yml ```yml
tests: tests:
- text: <code>spinalCase(&quot;This Is Spinal Tap&quot;)</code>应该返回<code>&quot;this-is-spinal-tap&quot;</code> - text: "<code>spinalCase('This Is Spinal Tap')</code>应该返回<code>'this-is-spinal-tap'</code>。"
testString: assert.deepEqual(spinalCase("This Is Spinal Tap"), "this-is-spinal-tap"); testString: assert.deepEqual(spinalCase("This Is Spinal Tap"), "this-is-spinal-tap");
- text: <code>spinalCase(&quot;thisIsSpinal Tap&quot;)</code>应该返回<code>&quot;this-is-spinal-tap&quot;</code> - text: "<code>spinalCase('thisIsSpinal<wbr>Tap')</code>应该返回<code>'this-is-spinal-tap'</code>。"
testString: assert.strictEqual(spinalCase('thisIsSpinalTap'), "this-is-spinal-tap"); testString: assert.strictEqual(spinalCase('thisIsSpinalTap'), "this-is-spinal-tap");
- text: <code>spinalCase(&quot;The_Andy_ Griffith_Show&quot;)</code>应该返回<code>&quot;the-andy-griffith-show&quot;</code> - text: "<code>spinalCase('The_Andy_<wbr>Griffith_Show')</code>应该返回<code>'the-andy-griffith-show'</code>。"
testString: assert.strictEqual(spinalCase("The_Andy_Griffith_Show"), "the-andy-griffith-show"); testString: assert.strictEqual(spinalCase("The_Andy_Griffith_Show"), "the-andy-griffith-show");
- text: <code>spinalCase(&quot;Teletubbies say Eh-oh&quot;)</code>应该返回<code>&quot;teletubbies-say-eh-oh&quot;</code> - text: "<code>spinalCase('Teletubbies say Eh-oh')</code>应该返回<code>'teletubbies-say-eh-oh'</code>。"
testString: assert.strictEqual(spinalCase("Teletubbies say Eh-oh"), "teletubbies-say-eh-oh"); testString: assert.strictEqual(spinalCase("Teletubbies say Eh-oh"), "teletubbies-say-eh-oh");
- text: <code>spinalCase(&quot;AllThe-small Things&quot;)</code>应该归还<code>&quot;all-the-small-things&quot;</code> - text: "<code>spinalCase('AllThe-small Things')</code>应该返回<code>'all-the-small-things'</code>。"
testString: assert.strictEqual(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'); spinalCase('This Is Spinal Tap');
``` ```
</div> </div>
@ -59,8 +62,14 @@ spinalCase('This Is Spinal Tap');
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -3,15 +3,19 @@ id: ab306dbdcc907c7ddfc30830
title: Steamroller title: Steamroller
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16079
localeTitle: 压路机 localeTitle: 扁平化
--- ---
## Description ## Description
<section id="description">展平嵌套数组。您必须考虑不同的嵌套级别。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
在这道题目中,我们需要写一个数组扁平化的函数。
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,13 +23,13 @@ localeTitle: 压路机
```yml ```yml
tests: tests:
- text: '<code>steamrollArray([[[&quot;a&quot;]], [[&quot;b&quot;]]])</code>应返回<code>[&quot;a&quot;, &quot;b&quot;]</code> 。' - text: "<code>steamrollArray([[['a']], [['b']]])</code>应返回<code>['a', 'b']</code>。"
testString: assert.deepEqual(steamrollArray([[["a"]], [["b"]]]), ["a", "b"]); testString: assert.deepEqual(steamrollArray([[["a"]], [["b"]]]), ["a", "b"]);
- text: '<code>steamrollArray([1, [2], [3, [[4]]]])</code>应该返回<code>[1, 2, 3, 4]</code> 。' - text: <code>steamrollArray([1, [2], [3, [[4]]]])</code>应该返回<code>[1, 2, 3, 4]</code>
testString: assert.deepEqual(steamrollArray([1, [2], [3, [[4]]]]), [1, 2, 3, 4]); testString: assert.deepEqual(steamrollArray([1, [2], [3, [[4]]]]), [1, 2, 3, 4]);
- text: '<code>steamrollArray([1, [], [3, [[4]]]])</code>应该返回<code>[1, 3, 4]</code> 。' - text: <code>steamrollArray([1, [], [3, [[4]]]])</code>应该返回<code>[1, 3, 4]</code>
testString: assert.deepEqual(steamrollArray([1, [], [3, [[4]]]]), [1, 3, 4]); testString: assert.deepEqual(steamrollArray([1, [], [3, [[4]]]]), [1, 3, 4]);
- text: '<code>steamrollArray([1, {}, [3, [[4]]]])</code>应返回<code>[1, {}, 3, 4]</code> 。' - text: <code>steamrollArray([1, {}, [3, [[4]]]])</code>应返回<code>[1, {}, 3, 4]</code>
testString: assert.deepEqual(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]]]]); steamrollArray([1, [2], [3, [[4]]]]);
``` ```
</div> </div>
@ -56,8 +59,20 @@ steamrollArray([1, [2], [3, [[4]]]]);
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -3,15 +3,23 @@ id: a3566b1109230028080c9345
title: Sum All Numbers in a Range title: Sum All Numbers in a Range
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16083
localeTitle: 求和范围中的所有数字 localeTitle: 范围内的数字求和
--- ---
## Description ## Description
<section id="description">我们将通过两个数字的数组。返回这两个数字的总和加上它们之间所有数字的总和。最低的数字并不总是第一位。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
给出一个含有两个数字的数组,我们需要写一个函数,让它返回这两个数字间所有数字(包含这两个数字)的总和。
例如,<code>sumAll([4,1])</code> 应该返回 <code>10</code>,因为从 1 到 4 (包含 1、4的所有数字的和是 <code>10</code>
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,15 +27,15 @@ localeTitle: 求和范围中的所有数字
```yml ```yml
tests: tests:
- text: '<code>sumAll([1, 4])</code>应该返回一个数字。' - text: <code>sumAll([1, 4])</code>应该返回一个数字。
testString: assert(typeof sumAll([1, 4]) === 'number'); testString: assert(typeof sumAll([1, 4]) === 'number');
- text: '<code>sumAll([1, 4])</code>应该返回10。' - text: <code>sumAll([1, 4])</code>应该返回 10。
testString: assert.deepEqual(sumAll([1, 4]), 10); testString: assert.deepEqual(sumAll([1, 4]), 10);
- text: '<code>sumAll([4, 1])</code>应该返回10。' - text: <code>sumAll([4, 1])</code>应该返回 10。
testString: assert.deepEqual(sumAll([4, 1]), 10); testString: assert.deepEqual(sumAll([4, 1]), 10);
- text: '<code>sumAll([5, 10])</code>应该返回45。' - text: <code>sumAll([5, 10])</code>应该返回 45。
testString: assert.deepEqual(sumAll([5, 10]), 45); testString: assert.deepEqual(sumAll([5, 10]), 45);
- text: '<code>sumAll([10, 5])</code>应该返回45。' - text: <code>sumAll([10, 5])</code>应该返回 45。
testString: assert.deepEqual(sumAll([10, 5]), 45); testString: assert.deepEqual(sumAll([10, 5]), 45);
``` ```
@ -45,7 +53,6 @@ function sumAll(arr) {
} }
sumAll([1, 4]); sumAll([1, 4]);
``` ```
</div> </div>
@ -57,8 +64,15 @@ sumAll([1, 4]);
## Solution ## Solution
<section id='solution'> <section id='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;
}
```
</section>

View File

@ -3,15 +3,21 @@ id: a5229172f011153519423690
title: Sum All Odd Fibonacci Numbers title: Sum All Odd Fibonacci Numbers
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16084
localeTitle: 求所有奇数斐波契数 localeTitle: 求斐波契数组中的奇数之和
--- ---
## Description ## Description
<section id="description">给定正整数<code>num</code> ,返回小于或等于<code>num</code>的所有奇数Fibonacci数的总和。 Fibonacci序列中的前两个数字是1和1.序列中的每个附加数字是前两个数字的总和。 Fibonacci序列的前六个数字是<code>sumFibs(10)</code>和8.例如, <code>sumFibs(10)</code>应该返回<code>10</code>因为小于或等于<code>10</code>所有奇数Fibonacci数都是<code>sumFibs(10)</code>和5.如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
在这道题目中,我们需要写一个函数,参数为一个正整数<code>num</code>。它的作用是计算斐波那契数列中,小于或等于<code>num</code>的奇数之和。
斐波那契数列中,第一和第二个数字都是 1后面的每个数字由之前两数相加得出。斐波那契数列的前六个数字分别为1、1、2、3、5、8。
比如,<code>sumFibs(10)</code>应该返回<code>10</code>。因为斐波那契数列中,比<code>10</code>小的数字只有 1、1、3、5。
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -23,7 +29,7 @@ tests:
testString: assert(typeof sumFibs(1) === "number"); testString: assert(typeof sumFibs(1) === "number");
- text: <code>sumFibs(1000)</code>应该返回 1785。 - text: <code>sumFibs(1000)</code>应该返回 1785。
testString: assert(sumFibs(1000) === 1785); testString: assert(sumFibs(1000) === 1785);
- text: <code>sumFibs(4000000)</code>应返回4613732。 - text: <code>sumFibs(4000000)</code>应返回 4613732。
testString: assert(sumFibs(4000000) === 4613732); testString: assert(sumFibs(4000000) === 4613732);
- text: <code>sumFibs(4)</code>应该返回 5。 - text: <code>sumFibs(4)</code>应该返回 5。
testString: assert(sumFibs(4) === 5); testString: assert(sumFibs(4) === 5);
@ -47,7 +53,6 @@ function sumFibs(num) {
} }
sumFibs(4); sumFibs(4);
``` ```
</div> </div>
@ -59,8 +64,19 @@ sumFibs(4);
## Solution ## Solution
<section id='solution'> <section id='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;
}
```
</section>

View File

@ -3,15 +3,26 @@ id: a3bfc1673c0526e06d3ac698
title: Sum All Primes title: Sum All Primes
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16085
localeTitle: Sum All Primes localeTitle: 对所有素数求和
--- ---
## Description ## Description
<section id="description">将所有素数加起来并包括所提供的数字。素数被定义为大于1的数并且只有两个除数一个和一个除数。例如2是素数因为它只能被1和2整除。提供的号码可能不是主要的。如果卡住请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section> <section id='description'>
质数是大于 1 且仅可以被 1 和自己整除的数。
比如2 就是一个质数,因为它只可以被 1 和 2它本身整除。
相反4 不是质数,因为它可以被 1, 2 和 4 整除。
重写 `sumPrimes` 使其返回所有小于或等于该数字的质数
的和。
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -41,7 +52,6 @@ function sumPrimes(num) {
} }
sumPrimes(10); sumPrimes(10);
``` ```
</div> </div>
@ -53,8 +63,33 @@ sumPrimes(10);
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```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> </section>

View File

@ -3,15 +3,20 @@ id: a8e512fbe388ac2f9198f0fa
title: Wherefore art thou title: Wherefore art thou
isRequired: true isRequired: true
challengeType: 5 challengeType: 5
videoUrl: '' forumTopicId: 16092
localeTitle: 因此,你是艺术家 localeTitle: 罗密欧与朱丽叶
--- ---
## Description ## Description
<section id="description">创建一个查看对象数组(第一个参数)的函数,并返回具有匹配的名称和值对的所有对象的数组(第二个参数)。如果要包含在返回的数组中,则源对象的每个名称和值对都必须存在于集合中的对象中。例如,如果第一个参数是<code>[{ first: &quot;Romeo&quot;, last: &quot;Montague&quot; }, { first: &quot;Mercutio&quot;, last: null }, { first: &quot;Tybalt&quot;, last: &quot;Capulet&quot; }]</code> ,第二个参数是<code>{ last: &quot;Capulet&quot; }</code> ,然后你必须从数组(第一个参数)返回第三个对象,因为它包含名称及其值,它作为第二个参数传递。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section> <section id='description'>
在这道题目中,我们要写一个函数,它接收两个参数:第一个参数是对象数组,第二个参数是一个对象。我们需要从对象数组中找出与第二个参数相等或包含第二个参数的所有对象,并以对象数组的形式返回。其中,相等的意思是原数组中的对象与第二个参数中对象的所有键值对完全相等;包含的意思是只要第二个参数中对象的所有键存在于原数组对象中,且它们对应的值相同即可。
比如,如果第一个参数是<code>[{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }]</code>,第二个参数是<code>{ last: "Capulet" }</code>。那么你需要以对象数组的形式返回第一个参数中的第三个元素,因为它包含第二个参数中定义的键<code>last</code>,且对应的值<code>"Capulet"</code>相同
如果你遇到了问题,请点击<a href='https://forum.freecodecamp.one/t/topic/157' target='_blank'>帮助</a>
</section>
## Instructions ## Instructions
<section id="instructions"> <section id='instructions'>
</section> </section>
## Tests ## Tests
@ -19,17 +24,17 @@ localeTitle: 因此,你是艺术家
```yml ```yml
tests: tests:
- text: '<code>whatIsInAName([{ first: &quot;Romeo&quot;, last: &quot;Montague&quot; }, { first: &quot;Mercutio&quot;, last: null }, { first: &quot;Tybalt&quot;, last: &quot;Capulet&quot; }], { last: &quot;Capulet&quot; })</code>应该返回<code>[{ first: &quot;Tybalt&quot;, last: &quot;Capulet&quot; }]</code>' - text: '<code>whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })</code>应该返回<code>[{ first: "Tybalt", last: "Capulet" }]</code>'
testString: 'assert.deepEqual(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: '<code>whatIsInAName([{ &quot;apple&quot;: 1 }, { &quot;apple&quot;: 1 }, { &quot;apple&quot;: 1, &quot;bat&quot;: 2 }], { &quot;apple&quot;: 1 })</code>应返回<code>[{ &quot;apple&quot;: 1 }, { &quot;apple&quot;: 1 }, { &quot;apple&quot;: 1, &quot;bat&quot;: 2 }]</code> 。' - text: '<code>whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })</code>应返回<code>[{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]</code>。'
testString: 'assert.deepEqual(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: '<code>whatIsInAName([{ &quot;apple&quot;: 1, &quot;bat&quot;: 2 }, { &quot;bat&quot;: 2 }, { &quot;apple&quot;: 1, &quot;bat&quot;: 2, &quot;cookie&quot;: 2 }], { &quot;apple&quot;: 1, &quot;bat&quot;: 2 })</code>应该返回<code>[{ &quot;apple&quot;: 1, &quot;bat&quot;: 2 }, { &quot;apple&quot;: 1, &quot;bat&quot;: 2, &quot;cookie&quot;: 2 }]</code> 。' - text: '<code>whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })</code>应该返回<code>[{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }]</code>。'
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 }]);' 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: '<code>whatIsInAName([{ &quot;apple&quot;: 1, &quot;bat&quot;: 2 }, { &quot;apple&quot;: 1 }, { &quot;apple&quot;: 1, &quot;bat&quot;: 2, &quot;cookie&quot;: 2 }], { &quot;apple&quot;: 1, &quot;cookie&quot;: 2 })</code>应该返回<code>[{ &quot;apple&quot;: 1, &quot;bat&quot;: 2, &quot;cookie&quot;: 2 }]</code> 。' - text: '<code>whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 })</code>应该返回<code>[{ "apple": 1, "bat": 2, "cookie": 2 }]</code>。'
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 }]);' 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: '<code>whatIsInAName([{ &quot;apple&quot;: 1, &quot;bat&quot;: 2 }, { &quot;apple&quot;: 1 }, { &quot;apple&quot;: 1, &quot;bat&quot;: 2, &quot;cookie&quot;: 2 }, { &quot;bat&quot;:2 }], { &quot;apple&quot;: 1, &quot;bat&quot;: 2 })</code>应该返回<code>[{ &quot;apple&quot;: 1, &quot;bat&quot;: 2 }, { &quot;apple&quot;: 1, &quot;bat&quot;: 2, &quot;cookie&quot;:2 }]</code> 。' - text: '<code>whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }], { "apple": 1, "bat": 2 })</code>应该返回<code>[{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }]</code>。'
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 }]);' 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: '<code>whatIsInAName([{&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3}], {&quot;a&quot;: 1, &quot;b&quot;: 9999, &quot;c&quot;: 3})</code>应该返回<code>[]</code>' - text: '<code>whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3})</code>应该返回<code>[]</code>'
testString: 'assert.deepEqual(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" }); whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
``` ```
</div> </div>
@ -65,8 +69,17 @@ whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last:
## Solution ## Solution
<section id='solution'> <section id='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;
}
```
</section>