Add languages Russian, Arabic, Chinese, Portuguese (#18305)
This commit is contained in:
committed by
mrugesh mohapatra
parent
09d3eca712
commit
2ca3a2093f
@ -0,0 +1,74 @@
|
||||
---
|
||||
id: a77dbc43c33f39daa4429b4f
|
||||
title: Boo who
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 嘘谁
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">检查值是否归类为布尔基元。返回true或false。布尔基元是true和false。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>booWho(true)</code>应该返回true。
|
||||
testString: 'assert.strictEqual(booWho(true), true, "<code>booWho(true)</code> should return true.");'
|
||||
- text: <code>booWho(false)</code>应该返回true。
|
||||
testString: 'assert.strictEqual(booWho(false), true, "<code>booWho(false)</code> should return true.");'
|
||||
- text: '<code>booWho([1, 2, 3])</code>应该返回false。'
|
||||
testString: 'assert.strictEqual(booWho([1, 2, 3]), false, "<code>booWho([1, 2, 3])</code> should return false.");'
|
||||
- text: '<code>booWho([].slice)</code>应该返回false。'
|
||||
testString: 'assert.strictEqual(booWho([].slice), false, "<code>booWho([].slice)</code> should return false.");'
|
||||
- text: '<code>booWho({ "a": 1 })</code>应该返回false。'
|
||||
testString: 'assert.strictEqual(booWho({ "a": 1 }), false, "<code>booWho({ "a": 1 })</code> should return false.");'
|
||||
- text: <code>booWho(1)</code>应该返回false。
|
||||
testString: 'assert.strictEqual(booWho(1), false, "<code>booWho(1)</code> should return false.");'
|
||||
- text: <code>booWho(NaN)</code>应该返回false。
|
||||
testString: 'assert.strictEqual(booWho(NaN), false, "<code>booWho(NaN)</code> should return false.");'
|
||||
- text: <code>booWho("a")</code>应该返回false。
|
||||
testString: 'assert.strictEqual(booWho("a"), false, "<code>booWho("a")</code> should return false.");'
|
||||
- text: <code>booWho("true")</code>应该返回false。
|
||||
testString: 'assert.strictEqual(booWho("true"), false, "<code>booWho("true")</code> should return false.");'
|
||||
- text: <code>booWho("false")</code>应该返回false。
|
||||
testString: 'assert.strictEqual(booWho("false"), false, "<code>booWho("false")</code> should return false.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function booWho(bool) {
|
||||
// What is the new fad diet for ghost developers? The Boolean.
|
||||
return bool;
|
||||
}
|
||||
|
||||
booWho(null);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,68 @@
|
||||
---
|
||||
id: a9bd25c716030ec90084d8a1
|
||||
title: Chunky Monkey
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 矮胖的猴子
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">编写一个函数,将数组(第一个参数)拆分为<code>size</code>的长度(第二个参数),并将它们作为二维数组返回。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>chunkArrayInGroups(["a", "b", "c", "d"], 2)</code>应返回<code>[["a", "b"], ["c", "d"]]</code> 。'
|
||||
testString: 'assert.deepEqual(chunkArrayInGroups(["a", "b", "c", "d"], 2), [["a", "b"], ["c", "d"]], "<code>chunkArrayInGroups(["a", "b", "c", "d"], 2)</code> should return <code>[["a", "b"], ["c", "d"]]</code>.");'
|
||||
- text: '<code>chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3)</code>应返回<code>[[0, 1, 2], [3, 4, 5]]</code> 。'
|
||||
testString: 'assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3), [[0, 1, 2], [3, 4, 5]], "<code>chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3)</code> should return <code>[[0, 1, 2], [3, 4, 5]]</code>.");'
|
||||
- text: '<code>chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2)</code>应返回<code>[[0, 1], [2, 3], [4, 5]]</code> 。'
|
||||
testString: 'assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2), [[0, 1], [2, 3], [4, 5]], "<code>chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2)</code> should return <code>[[0, 1], [2, 3], [4, 5]]</code>.");'
|
||||
- text: '<code>chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4)</code>应该返回<code>[[0, 1, 2, 3], [4, 5]]</code> 。'
|
||||
testString: 'assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4), [[0, 1, 2, 3], [4, 5]], "<code>chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4)</code> should return <code>[[0, 1, 2, 3], [4, 5]]</code>.");'
|
||||
- text: '<code>chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3)</code>应该返回<code>[[0, 1, 2], [3, 4, 5], [6]]</code> 。'
|
||||
testString: 'assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3), [[0, 1, 2], [3, 4, 5], [6]], "<code>chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3)</code> should return <code>[[0, 1, 2], [3, 4, 5], [6]]</code>.");'
|
||||
- text: '<code>chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4)</code>应返回<code>[[0, 1, 2, 3], [4, 5, 6, 7], [8]]</code> <code>chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4)</code> <code>[[0, 1, 2, 3], [4, 5, 6, 7], [8]]</code> 。'
|
||||
testString: 'assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4), [[0, 1, 2, 3], [4, 5, 6, 7], [8]], "<code>chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4)</code> should return <code>[[0, 1, 2, 3], [4, 5, 6, 7], [8]]</code>.");'
|
||||
- text: '<code>chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2)</code>应返回<code>[[0, 1], [2, 3], [4, 5], [6, 7], [8]]</code> 。'
|
||||
testString: 'assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2), [[0, 1], [2, 3], [4, 5], [6, 7], [8]], "<code>chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2)</code> should return <code>[[0, 1], [2, 3], [4, 5], [6, 7], [8]]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function chunkArrayInGroups(arr, size) {
|
||||
// Break it up.
|
||||
return arr;
|
||||
}
|
||||
|
||||
chunkArrayInGroups(["a", "b", "c", "d"], 2);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,77 @@
|
||||
---
|
||||
id: acda2fb1324d9b0fa741e6b5
|
||||
title: Confirm the Ending
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 确认结束
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">检查字符串(第一个参数<code>str</code> )是否以给定的目标字符串(第二个参数, <code>target</code> )结束。这个挑战<em>可以</em>通过<code>.endsWith()</code>中引入的<code>.endsWith()</code>方法来解决。但是出于这个挑战的目的,我们希望您使用其中一个JavaScript子字符串方法。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>confirmEnding("Bastian", "n")</code>应该返回true。'
|
||||
testString: 'assert(confirmEnding("Bastian", "n") === true, "<code>confirmEnding("Bastian", "n")</code> should return true.");'
|
||||
- text: '<code>confirmEnding("Congratulation", "on")</code>应该返回true。'
|
||||
testString: 'assert(confirmEnding("Congratulation", "on") === true, "<code>confirmEnding("Congratulation", "on")</code> should return true.");'
|
||||
- text: '<code>confirmEnding("Connor", "n")</code>应返回false。'
|
||||
testString: 'assert(confirmEnding("Connor", "n") === false, "<code>confirmEnding("Connor", "n")</code> should return false.");'
|
||||
- text: '<code>confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification")</code>应该返回false。'
|
||||
testString: 'assert(confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") === false, "<code>confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification")</code> should return false.");'
|
||||
- text: '<code>confirmEnding("He has to give me a new name", "name")</code>应该返回true。'
|
||||
testString: 'assert(confirmEnding("He has to give me a new name", "name") === true, "<code>confirmEnding("He has to give me a new name", "name")</code> should return true.");'
|
||||
- text: '<code>confirmEnding("Open sesame", "same")</code>应该返回true。'
|
||||
testString: 'assert(confirmEnding("Open sesame", "same") === true, "<code>confirmEnding("Open sesame", "same")</code> should return true.");'
|
||||
- text: '<code>confirmEnding("Open sesame", "pen")</code>应该返回false。'
|
||||
testString: 'assert(confirmEnding("Open sesame", "pen") === false, "<code>confirmEnding("Open sesame", "pen")</code> should return false.");'
|
||||
- text: '<code>confirmEnding("Open sesame", "game")</code>应该返回false。'
|
||||
testString: 'assert(confirmEnding("Open sesame", "game") === false, "<code>confirmEnding("Open sesame", "game")</code> should return false.");'
|
||||
- text: '<code>confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain")</code>应该返回虚假。'
|
||||
testString: 'assert(confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") === false, "<code>confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain")</code> should return false.");'
|
||||
- text: '<code>confirmEnding("Abstraction", "action")</code>应该返回true。'
|
||||
testString: 'assert(confirmEnding("Abstraction", "action") === true, "<code>confirmEnding("Abstraction", "action")</code> should return true.");'
|
||||
- text: 不要使用内置方法<code>.endsWith()</code>来解决挑战。
|
||||
testString: 'assert(!(/\.endsWith\(.*?\)\s*?;?/.test(code)) && !(/\["endsWith"\]/.test(code)), "Do not use the built-in method <code>.endsWith()</code> to solve the challenge.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function confirmEnding(str, target) {
|
||||
// "Never give up and good luck will find you."
|
||||
// -- Falcor
|
||||
return str;
|
||||
}
|
||||
|
||||
confirmEnding("Bastian", "n");
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b3
|
||||
title: Convert Celsius to Fahrenheit
|
||||
challengeType: 1
|
||||
isRequired: true
|
||||
videoUrl: ''
|
||||
localeTitle: 将摄氏温度转换为华氏温度
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">从摄氏温度转换为华氏温度的算法是以摄氏度乘以<code>9/5</code>的温度加上<code>32</code> 。您将获得一个可变<code>celsius</code>表示摄氏温度。使用已定义的变量<code>fahrenheit</code>温度,并将其指定为相当于给定摄氏温度的华氏温度。使用上面提到的算法帮助将摄氏温度转换为华氏温度。不要过多担心函数和返回语句,因为它们将在未来的挑战中涵盖。目前,只使用您已经学过的运算符。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>convertToF(0)</code>应该返回一个数字
|
||||
testString: 'assert(typeof convertToF(0) === "number", "<code>convertToF(0)</code> should return a number");'
|
||||
- text: <code>convertToF(-30)</code>应该返回值<code>-22</code>
|
||||
testString: 'assert(convertToF(-30) === -22, "<code>convertToF(-30)</code> should return a value of <code>-22</code>");'
|
||||
- text: <code>convertToF(-10)</code>应该返回值<code>14</code>
|
||||
testString: 'assert(convertToF(-10) === 14, "<code>convertToF(-10)</code> should return a value of <code>14</code>");'
|
||||
- text: <code>convertToF(0)</code>应返回值<code>32</code>
|
||||
testString: 'assert(convertToF(0) === 32, "<code>convertToF(0)</code> should return a value of <code>32</code>");'
|
||||
- text: <code>convertToF(20)</code>应返回值<code>68</code>
|
||||
testString: 'assert(convertToF(20) === 68, "<code>convertToF(20)</code> should return a value of <code>68</code>");'
|
||||
- text: <code>convertToF(30)</code>应返回值<code>86</code>
|
||||
testString: 'assert(convertToF(30) === 86, "<code>convertToF(30)</code> should return a value of <code>86</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function convertToF(celsius) {
|
||||
let fahrenheit;
|
||||
return fahrenheit;
|
||||
}
|
||||
|
||||
convertToF(30);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,63 @@
|
||||
---
|
||||
id: a302f7aae1aa3152a5b413bc
|
||||
title: Factorialize a Number
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 对一个数字进行推理
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">返回提供的整数的阶乘。如果整数用字母n表示,则阶乘是所有小于或等于n的正整数的乘积。因子通常用简写符号<code>n!</code>表示<code>n!</code>例如: <code>5! = 1 * 2 * 3 * 4 * 5 = 120</code>只有大于或等于零的整数才会被提供给该函数。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>factorialize(5)</code>应该返回一个数字。
|
||||
testString: 'assert(typeof factorialize(5) === "number", "<code>factorialize(5)</code> should return a number.");'
|
||||
- text: <code>factorialize(5)</code>应该返回120。
|
||||
testString: 'assert(factorialize(5) === 120, "<code>factorialize(5)</code> should return 120.");'
|
||||
- text: <code>factorialize(10)</code>应返回3628800。
|
||||
testString: 'assert(factorialize(10) === 3628800, "<code>factorialize(10)</code> should return 3628800.");'
|
||||
- text: <code>factorialize(20)</code>应该返回2432902008176640000。
|
||||
testString: 'assert(factorialize(20) === 2432902008176640000, "<code>factorialize(20)</code> should return 2432902008176640000.");'
|
||||
- text: <code>factorialize(0)</code>应该返回1。
|
||||
testString: 'assert(factorialize(0) === 1, "<code>factorialize(0)</code> should return 1.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function factorialize(num) {
|
||||
return num;
|
||||
}
|
||||
|
||||
factorialize(5);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,62 @@
|
||||
---
|
||||
id: adf08ec01beb4f99fc7a68f2
|
||||
title: Falsy Bouncer
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: Falsy Bouncer
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">从数组中删除所有有价值的值。 JavaScript中的Falsy值为<code>false</code> , <code>null</code> , <code>0</code> , <code>""</code> , <code>undefined</code>和<code>NaN</code> 。提示:尝试将每个值转换为布尔值。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>bouncer([7, "ate", "", false, 9])</code>应该返回<code>[7, "ate", 9]</code> 。'
|
||||
testString: 'assert.deepEqual(bouncer([7, "ate", "", false, 9]), [7, "ate", 9], "<code>bouncer([7, "ate", "", false, 9])</code> should return <code>[7, "ate", 9]</code>.");'
|
||||
- text: '<code>bouncer(["a", "b", "c"])</code>应返回<code>["a", "b", "c"]</code> 。'
|
||||
testString: 'assert.deepEqual(bouncer(["a", "b", "c"]), ["a", "b", "c"], "<code>bouncer(["a", "b", "c"])</code> should return <code>["a", "b", "c"]</code>.");'
|
||||
- text: '<code>bouncer([false, null, 0, NaN, undefined, ""])</code>应返回<code>[]</code> 。'
|
||||
testString: 'assert.deepEqual(bouncer([false, null, 0, NaN, undefined, ""]), [], "<code>bouncer([false, null, 0, NaN, undefined, ""])</code> should return <code>[]</code>.");'
|
||||
- text: '<code>bouncer([1, null, NaN, 2, undefined])</code>应该返回<code>[1, 2]</code> 。'
|
||||
testString: 'assert.deepEqual(bouncer([1, null, NaN, 2, undefined]), [1, 2], "<code>bouncer([1, null, NaN, 2, undefined])</code> should return <code>[1, 2]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function bouncer(arr) {
|
||||
// Don't show a false ID to this bouncer.
|
||||
return arr;
|
||||
}
|
||||
|
||||
bouncer([7, "ate", "", false, 9]);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,65 @@
|
||||
---
|
||||
id: a26cbbe9ad8655a977e1ceb5
|
||||
title: Find the Longest Word in a String
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 找到字符串中最长的单词
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">返回所提供句子中最长单词的长度。您的回答应该是一个数字。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>findLongestWordLength("The quick brown fox jumped over the lazy dog")</code>应该返回一个数字。
|
||||
testString: 'assert(typeof findLongestWordLength("The quick brown fox jumped over the lazy dog") === "number", "<code>findLongestWordLength("The quick brown fox jumped over the lazy dog")</code> should return a number.");'
|
||||
- text: <code>findLongestWordLength("The quick brown fox jumped over the lazy dog")</code>应该返回6。
|
||||
testString: 'assert(findLongestWordLength("The quick brown fox jumped over the lazy dog") === 6, "<code>findLongestWordLength("The quick brown fox jumped over the lazy dog")</code> should return 6.");'
|
||||
- text: <code>findLongestWordLength("May the force be with you")</code>应该返回5。
|
||||
testString: 'assert(findLongestWordLength("May the force be with you") === 5, "<code>findLongestWordLength("May the force be with you")</code> should return 5.");'
|
||||
- text: <code>findLongestWordLength("Google do a barrel roll")</code>应返回6。
|
||||
testString: 'assert(findLongestWordLength("Google do a barrel roll") === 6, "<code>findLongestWordLength("Google do a barrel roll")</code> should return 6.");'
|
||||
- text: <code>findLongestWordLength("What is the average airspeed velocity of an unladen swallow")</code>应该返回8。
|
||||
testString: 'assert(findLongestWordLength("What is the average airspeed velocity of an unladen swallow") === 8, "<code>findLongestWordLength("What is the average airspeed velocity of an unladen swallow")</code> should return 8.");'
|
||||
- text: <code>findLongestWordLength("What if we try a super-long word such as otorhinolaryngology")</code>应该返回19。
|
||||
testString: 'assert(findLongestWordLength("What if we try a super-long word such as otorhinolaryngology") === 19, "<code>findLongestWordLength("What if we try a super-long word such as otorhinolaryngology")</code> should return 19.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function findLongestWordLength(str) {
|
||||
return str.length;
|
||||
}
|
||||
|
||||
findLongestWordLength("The quick brown fox jumped over the lazy dog");
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,58 @@
|
||||
---
|
||||
id: a6e40f1041b06c996f7b2406
|
||||
title: Finders Keepers
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: Finders Keepers
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">创建一个查看数组(第一个参数)的函数,并返回数组中传递真值测试的第一个元素(第二个参数)。如果没有元素通过测试,则返回undefined。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; })</code>应该返回8。'
|
||||
testString: 'assert.strictEqual(findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; }), 8, "<code>findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; })</code> should return 8.");'
|
||||
- text: '<code>findElement([1, 3, 5, 9], function(num) { return num % 2 === 0; })</code>应返回undefined。'
|
||||
testString: 'assert.strictEqual(findElement([1, 3, 5, 9], function(num) { return num % 2 === 0; }), undefined, "<code>findElement([1, 3, 5, 9], function(num) { return num % 2 === 0; })</code> should return undefined.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function findElement(arr, func) {
|
||||
let num = 0;
|
||||
return num;
|
||||
}
|
||||
|
||||
findElement([1, 2, 3, 4], num => num % 2 === 0);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,71 @@
|
||||
---
|
||||
id: af2170cad53daa0770fabdea
|
||||
title: Mutations
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 突变
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果数组的第一个元素中的字符串包含数组第二个元素中字符串的所有字母,则返回true。例如, <code>["hello", "Hello"]</code>应该返回true,因为第二个字符串中的所有字母都出现在第一个字母中,忽略大小写。参数<code>["hello", "hey"]</code>应返回false,因为字符串“hello”不包含“y”。最后, <code>["Alien", "line"]</code>应该返回true,因为“line”中的所有字母都出现在“Alien”中。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>mutation(["hello", "hey"])</code>应该返回false。'
|
||||
testString: 'assert(mutation(["hello", "hey"]) === false, "<code>mutation(["hello", "hey"])</code> should return false.");'
|
||||
- text: '<code>mutation(["hello", "Hello"])</code>应该返回true。'
|
||||
testString: 'assert(mutation(["hello", "Hello"]) === true, "<code>mutation(["hello", "Hello"])</code> should return true.");'
|
||||
- text: '<code>mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"])</code>应该返回true。'
|
||||
testString: 'assert(mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]) === true, "<code>mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"])</code> should return true.");'
|
||||
- text: '<code>mutation(["Mary", "Army"])</code>应该返回true。'
|
||||
testString: 'assert(mutation(["Mary", "Army"]) === true, "<code>mutation(["Mary", "Army"])</code> should return true.");'
|
||||
- text: '<code>mutation(["Mary", "Aarmy"])</code>应该返回true。'
|
||||
testString: 'assert(mutation(["Mary", "Aarmy"]) === true, "<code>mutation(["Mary", "Aarmy"])</code> should return true.");'
|
||||
- text: '<code>mutation(["Alien", "line"])</code>应该返回true。'
|
||||
testString: 'assert(mutation(["Alien", "line"]) === true, "<code>mutation(["Alien", "line"])</code> should return true.");'
|
||||
- text: '<code>mutation(["floor", "for"])</code>应该返回true。'
|
||||
testString: 'assert(mutation(["floor", "for"]) === true, "<code>mutation(["floor", "for"])</code> should return true.");'
|
||||
- text: '<code>mutation(["hello", "neo"])</code>应该返回false。'
|
||||
testString: 'assert(mutation(["hello", "neo"]) === false, "<code>mutation(["hello", "neo"])</code> should return false.");'
|
||||
- text: '<code>mutation(["voodoo", "no"])</code>应该返回false。'
|
||||
testString: 'assert(mutation(["voodoo", "no"]) === false, "<code>mutation(["voodoo", "no"])</code> should return false.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function mutation(arr) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
mutation(["hello", "hey"]);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,68 @@
|
||||
---
|
||||
id: afcc8d540bea9ea2669306b6
|
||||
title: Repeat a String Repeat a String
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 重复一个字符串重复字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">为<code>num</code> times(第二个参数)重复给定的字符串<code>str</code> (第一个参数)。如果<code>num</code>不是正数,则返回空字符串。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>repeatStringNumTimes("*", 3)</code>应该返回<code>"***"</code> 。'
|
||||
testString: 'assert(repeatStringNumTimes("*", 3) === "***", "<code>repeatStringNumTimes("*", 3)</code> should return <code>"***"</code>.");'
|
||||
- text: '<code>repeatStringNumTimes("abc", 3)</code>应该返回<code>"abcabcabc"</code> 。'
|
||||
testString: 'assert(repeatStringNumTimes("abc", 3) === "abcabcabc", "<code>repeatStringNumTimes("abc", 3)</code> should return <code>"abcabcabc"</code>.");'
|
||||
- text: '<code>repeatStringNumTimes("abc", 4)</code>应返回<code>"abcabcabcabc"</code> 。'
|
||||
testString: 'assert(repeatStringNumTimes("abc", 4) === "abcabcabcabc", "<code>repeatStringNumTimes("abc", 4)</code> should return <code>"abcabcabcabc"</code>.");'
|
||||
- text: '<code>repeatStringNumTimes("abc", 1)</code>应该返回<code>"abc"</code> 。'
|
||||
testString: 'assert(repeatStringNumTimes("abc", 1) === "abc", "<code>repeatStringNumTimes("abc", 1)</code> should return <code>"abc"</code>.");'
|
||||
- text: '<code>repeatStringNumTimes("*", 8)</code>应该返回<code>"********"</code> 。'
|
||||
testString: 'assert(repeatStringNumTimes("*", 8) === "********", "<code>repeatStringNumTimes("*", 8)</code> should return <code>"********"</code>.");'
|
||||
- text: '<code>repeatStringNumTimes("abc", -2)</code>应返回<code>""</code> 。'
|
||||
testString: 'assert(repeatStringNumTimes("abc", -2) === "", "<code>repeatStringNumTimes("abc", -2)</code> should return <code>""</code>.");'
|
||||
- text: 不应使用内置的<code>repeat()</code>方法
|
||||
testString: 'assert(!/\.repeat/g.test(code), "The built-in <code>repeat()</code>-method should not be used");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function repeatStringNumTimes(str, num) {
|
||||
// repeat after me
|
||||
return str;
|
||||
}
|
||||
|
||||
repeatStringNumTimes("abc", 3);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,62 @@
|
||||
---
|
||||
id: a789b3483989747d63b0e427
|
||||
title: Return Largest Numbers in Arrays
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 返回数组中的最大数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">返回一个数组,该数组由每个提供的子数组中的最大数字组成。为简单起见,提供的数组将包含4个子数组。请记住,您可以使用简单的for循环遍历数组,并使用数组语法<code>arr[i]</code>访问每个成员。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])</code>应该返回一个数组。'
|
||||
testString: 'assert(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]).constructor === Array, "<code>largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])</code> should return an array.");'
|
||||
- text: '<code>largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]])</code>应该返回<code>[27, 5, 39, 1001]</code> <code>largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]])</code> <code>[27, 5, 39, 1001]</code> 。'
|
||||
testString: 'assert.deepEqual(largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]), [27, 5, 39, 1001], "<code>largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]])</code> should return <code>[27, 5, 39, 1001]</code>.");'
|
||||
- text: '<code>largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]])</code>应该返回<code>[9, 35, 97, 1000000]</code> <code>largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]])</code> <code>[9, 35, 97, 1000000]</code> 。'
|
||||
testString: 'assert.deepEqual(largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]), [9, 35, 97, 1000000], "<code>largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]])</code> should return <code>[9, 35, 97, 1000000]</code>.");'
|
||||
- text: '<code>largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]])</code>应该返回<code>[25, 48, 21, -3]</code> 。'
|
||||
testString: 'assert.deepEqual(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]), [25, 48, 21, -3], "<code>largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]])</code> should return <code>[25, 48, 21, -3]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function largestOfFour(arr) {
|
||||
// You can do this!
|
||||
return arr;
|
||||
}
|
||||
|
||||
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,61 @@
|
||||
---
|
||||
id: a202eed8fc186c8434cb6d61
|
||||
title: Reverse a String
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 反转字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">反转提供的字符串。您可能需要先将字符串转换为数组,然后才能将其反转。您的结果必须是字符串。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>reverseString("hello")</code>应该返回一个字符串。
|
||||
testString: 'assert(typeof reverseString("hello") === "string", "<code>reverseString("hello")</code> should return a string.");'
|
||||
- text: <code>reverseString("hello")</code>应该变成<code>"olleh"</code> 。
|
||||
testString: 'assert(reverseString("hello") === "olleh", "<code>reverseString("hello")</code> should become <code>"olleh"</code>.");'
|
||||
- text: <code>reverseString("Howdy")</code>应该变成<code>"ydwoH"</code> 。
|
||||
testString: 'assert(reverseString("Howdy") === "ydwoH", "<code>reverseString("Howdy")</code> should become <code>"ydwoH"</code>.");'
|
||||
- text: <code>reverseString("Greetings from Earth")</code>应返回<code>"htraE morf sgniteerG"</code> 。
|
||||
testString: 'assert(reverseString("Greetings from Earth") === "htraE morf sgniteerG", "<code>reverseString("Greetings from Earth")</code> should return <code>"htraE morf sgniteerG"</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function reverseString(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
reverseString("hello");
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 579e2a2c335b9d72dd32e05c
|
||||
title: Slice and Splice
|
||||
isRequired: true
|
||||
isBeta: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 切片和拼接
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您将获得两个数组和一个索引。使用数组方法<code>slice</code>和<code>splice</code>按顺序将第一个数组的每个元素复制到第二个数组中。开始在第二个数组的索引<code>n</code>处插入元素。返回结果数组。函数运行后,输入数组应保持不变。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>frankenSplice([1, 2, 3], [4, 5], 1)</code>应该返回<code>[4, 1, 2, 3, 5]</code> <code>frankenSplice([1, 2, 3], [4, 5], 1)</code> <code>[4, 1, 2, 3, 5]</code> 。'
|
||||
testString: 'assert.deepEqual(frankenSplice([1, 2, 3], [4, 5], 1), [4, 1, 2, 3, 5], "<code>frankenSplice([1, 2, 3], [4, 5], 1)</code> should return <code>[4, 1, 2, 3, 5]</code>.");'
|
||||
- text: '<code>frankenSplice([1, 2], ["a", "b"], 1)</code>应返回<code>["a", 1, 2, "b"]</code> 。'
|
||||
testString: 'assert.deepEqual(frankenSplice(testArr1, testArr2, 1), ["a", 1, 2, "b"], "<code>frankenSplice([1, 2], ["a", "b"], 1)</code> should return <code>["a", 1, 2, "b"]</code>.");'
|
||||
- text: '<code>frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2)</code>应该返回<code>["head", "shoulders", "claw", "tentacle", "knees", "toes"]</code> 。'
|
||||
testString: 'assert.deepEqual(frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2), ["head", "shoulders", "claw", "tentacle", "knees", "toes"], "<code>frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2)</code> should return <code>["head", "shoulders", "claw", "tentacle", "knees", "toes"]</code>.");'
|
||||
- text: 第一个数组中的所有元素都应按原始顺序添加到第二个数组中。
|
||||
testString: 'assert.deepEqual(frankenSplice([1, 2, 3, 4], [], 0), [1, 2, 3, 4], "All elements from the first array should be added to the second array in their original order.");'
|
||||
- text: 函数运行后,第一个数组应保持不变。
|
||||
testString: 'assert(testArr1[0] === 1 && testArr1[1] === 2, "The first array should remain the same after the function runs.");'
|
||||
- text: 函数运行后,第二个数组应保持不变。
|
||||
testString: 'assert(testArr2[0] === "a" && testArr2[1] === "b", "The second array should remain the same after the function runs.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function frankenSplice(arr1, arr2, n) {
|
||||
// It's alive. It's alive!
|
||||
return arr2;
|
||||
}
|
||||
|
||||
frankenSplice([1, 2, 3], [4, 5, 6], 1);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,61 @@
|
||||
---
|
||||
id: ab6137d4e35944e21037b769
|
||||
title: Title Case a Sentence
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 标题案例句子
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">返回提供的字符串,每个单词的首字母大写。确保单词的其余部分为小写。出于本练习的目的,您还应该将诸如“the”和“of”之类的连接词大写。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>titleCase("I'm a little tea pot")</code>应该返回一个字符串。'
|
||||
testString: 'assert(typeof titleCase("I"m a little tea pot") === "string", "<code>titleCase("I'm a little tea pot")</code> should return a string.");'
|
||||
- text: '<code>titleCase("I'm a little tea pot")</code>应该归还<code>I'm A Little Tea Pot</code> 。'
|
||||
testString: 'assert(titleCase("I"m a little tea pot") === "I"m A Little Tea Pot", "<code>titleCase("I'm a little tea pot")</code> should return <code>I'm A Little Tea Pot</code>.");'
|
||||
- text: <code>titleCase("sHoRt AnD sToUt")</code>应返回<code>Short And Stout</code> 。
|
||||
testString: 'assert(titleCase("sHoRt AnD sToUt") === "Short And Stout", "<code>titleCase("sHoRt AnD sToUt")</code> should return <code>Short And Stout</code>.");'
|
||||
- text: <code>titleCase("HERE IS MY HANDLE HERE IS MY SPOUT")</code> <code>Here Is My Handle Here Is My Spout</code> <code>titleCase("HERE IS MY HANDLE HERE IS MY SPOUT")</code>应该回到<code>Here Is My Handle Here Is My Spout</code> 。
|
||||
testString: 'assert(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT") === "Here Is My Handle Here Is My Spout", "<code>titleCase("HERE IS MY HANDLE HERE IS MY SPOUT")</code> should return <code>Here Is My Handle Here Is My Spout</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function titleCase(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
titleCase("I'm a little tea pot");
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,66 @@
|
||||
---
|
||||
id: ac6993d51946422351508a41
|
||||
title: Truncate a String
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 截断字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果字符串(第一个参数)长于给定的最大字符串长度(第二个参数),则截断该字符串。返回带有<code>...</code>结尾的截断字符串。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>truncateString("A-tisket a-tasket A green and yellow basket", 8)</code>应该返回“A-tisket ......”。'
|
||||
testString: 'assert(truncateString("A-tisket a-tasket A green and yellow basket", 8) === "A-tisket...", "<code>truncateString("A-tisket a-tasket A green and yellow basket", 8)</code> should return "A-tisket...".");'
|
||||
- text: '<code>truncateString("Peter Piper picked a peck of pickled peppers", 11)</code>应该回归“Peter Piper ......”。'
|
||||
testString: 'assert(truncateString("Peter Piper picked a peck of pickled peppers", 11) === "Peter Piper...", "<code>truncateString("Peter Piper picked a peck of pickled peppers", 11)</code> should return "Peter Piper...".");'
|
||||
- text: '<code>truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)</code>应该返回“A-tisket a-tasket A green and yellow basket”。'
|
||||
testString: 'assert(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length) === "A-tisket a-tasket A green and yellow basket", "<code>truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)</code> should return "A-tisket a-tasket A green and yellow basket".");'
|
||||
- text: '<code>truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2)</code>应返回“A-tisket a-tasket A green and yellow basket”。'
|
||||
testString: 'assert(truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2) === "A-tisket a-tasket A green and yellow basket", "<code>truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2)</code> should return "A-tisket a-tasket A green and yellow basket".");'
|
||||
- text: '<code>truncateString("A-", 1)</code>应返回“A ...”。'
|
||||
testString: 'assert(truncateString("A-", 1) === "A...", "<code>truncateString("A-", 1)</code> should return "A...".");'
|
||||
- text: '<code>truncateString("Absolutely Longer", 2)</code>应返回“Ab ...”。'
|
||||
testString: 'assert(truncateString("Absolutely Longer", 2) === "Ab...", "<code>truncateString("Absolutely Longer", 2)</code> should return "Ab...".");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function truncateString(str, num) {
|
||||
// Clear out that junk in your trunk
|
||||
return str;
|
||||
}
|
||||
|
||||
truncateString("A-tisket a-tasket A green and yellow basket", 8);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,86 @@
|
||||
---
|
||||
id: a24c1a4622e3c05097f71d67
|
||||
title: Where do I Belong
|
||||
isRequired: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
localeTitle: 我属于哪里?
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">返回一个值(第二个参数)应该在排序后插入数组(第一个参数)的最低索引。返回的值应该是一个数字。例如, <code>getIndexToIns([1,2,3,4], 1.5)</code>应返回<code>1</code>因为它大于<code>1</code> (索引0),但小于<code>2</code> (索引1)。同样, <code>getIndexToIns([20,3,5], 19)</code>应返回<code>2</code>因为一旦数组已经排序,它将看起来像<code>[3,5,20]</code> , <code>19</code>小于<code>20</code> (索引2)并且大于<code>5</code> (指数1)。如果卡住,请记得使用<a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>getIndexToIns([10, 20, 30, 40, 50], 35)</code>应返回<code>3</code> 。'
|
||||
testString: 'assert(getIndexToIns([10, 20, 30, 40, 50], 35) === 3, "<code>getIndexToIns([10, 20, 30, 40, 50], 35)</code> should return <code>3</code>.");'
|
||||
- text: '<code>getIndexToIns([10, 20, 30, 40, 50], 35)</code>应返回一个数字。'
|
||||
testString: 'assert(typeof(getIndexToIns([10, 20, 30, 40, 50], 35)) === "number", "<code>getIndexToIns([10, 20, 30, 40, 50], 35)</code> should return a number.");'
|
||||
- text: '<code>getIndexToIns([10, 20, 30, 40, 50], 30)</code>应该返回<code>2</code> 。'
|
||||
testString: 'assert(getIndexToIns([10, 20, 30, 40, 50], 30) === 2, "<code>getIndexToIns([10, 20, 30, 40, 50], 30)</code> should return <code>2</code>.");'
|
||||
- text: '<code>getIndexToIns([10, 20, 30, 40, 50], 30)</code>应该返回一个数字。'
|
||||
testString: 'assert(typeof(getIndexToIns([10, 20, 30, 40, 50], 30)) === "number", "<code>getIndexToIns([10, 20, 30, 40, 50], 30)</code> should return a number.");'
|
||||
- text: '<code>getIndexToIns([40, 60], 50)</code>应返回<code>1</code> 。'
|
||||
testString: 'assert(getIndexToIns([40, 60], 50) === 1, "<code>getIndexToIns([40, 60], 50)</code> should return <code>1</code>.");'
|
||||
- text: '<code>getIndexToIns([40, 60], 50)</code>应返回一个数字。'
|
||||
testString: 'assert(typeof(getIndexToIns([40, 60], 50)) === "number", "<code>getIndexToIns([40, 60], 50)</code> should return a number.");'
|
||||
- text: '<code>getIndexToIns([3, 10, 5], 3)</code>应该返回<code>0</code> 。'
|
||||
testString: 'assert(getIndexToIns([3, 10, 5], 3) === 0, "<code>getIndexToIns([3, 10, 5], 3)</code> should return <code>0</code>.");'
|
||||
- text: '<code>getIndexToIns([3, 10, 5], 3)</code>应返回一个数字。'
|
||||
testString: 'assert(typeof(getIndexToIns([3, 10, 5], 3)) === "number", "<code>getIndexToIns([3, 10, 5], 3)</code> should return a number.");'
|
||||
- text: '<code>getIndexToIns([5, 3, 20, 3], 5)</code>应返回<code>2</code> 。'
|
||||
testString: 'assert(getIndexToIns([5, 3, 20, 3], 5) === 2, "<code>getIndexToIns([5, 3, 20, 3], 5)</code> should return <code>2</code>.");'
|
||||
- text: '<code>getIndexToIns([5, 3, 20, 3], 5)</code>应返回一个数字。'
|
||||
testString: 'assert(typeof(getIndexToIns([5, 3, 20, 3], 5)) === "number", "<code>getIndexToIns([5, 3, 20, 3], 5)</code> should return a number.");'
|
||||
- text: '<code>getIndexToIns([2, 20, 10], 19)</code>应该返回<code>2</code> 。'
|
||||
testString: 'assert(getIndexToIns([2, 20, 10], 19) === 2, "<code>getIndexToIns([2, 20, 10], 19)</code> should return <code>2</code>.");'
|
||||
- text: '<code>getIndexToIns([2, 20, 10], 19)</code>应返回一个数字。'
|
||||
testString: 'assert(typeof(getIndexToIns([2, 20, 10], 19)) === "number", "<code>getIndexToIns([2, 20, 10], 19)</code> should return a number.");'
|
||||
- text: '<code>getIndexToIns([2, 5, 10], 15)</code>应该返回<code>3</code> 。'
|
||||
testString: 'assert(getIndexToIns([2, 5, 10], 15) === 3, "<code>getIndexToIns([2, 5, 10], 15)</code> should return <code>3</code>.");'
|
||||
- text: '<code>getIndexToIns([2, 5, 10], 15)</code>应返回一个数字。'
|
||||
testString: 'assert(typeof(getIndexToIns([2, 5, 10], 15)) === "number", "<code>getIndexToIns([2, 5, 10], 15)</code> should return a number.");'
|
||||
- text: '<code>getIndexToIns([], 1)</code>应该返回<code>0</code> 。'
|
||||
testString: 'assert(getIndexToIns([], 1) === 0, "<code>getIndexToIns([], 1)</code> should return <code>0</code>.");'
|
||||
- text: '<code>getIndexToIns([], 1)</code>应该返回一个数字。'
|
||||
testString: 'assert(typeof(getIndexToIns([], 1)) === "number", "<code>getIndexToIns([], 1)</code> should return a number.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function getIndexToIns(arr, num) {
|
||||
// Find my place in this sorted array.
|
||||
return num;
|
||||
}
|
||||
|
||||
getIndexToIns([40, 60], 50);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 587d7b7d367417b2b2512b1d
|
||||
title: ' Iterate Through the Keys of an Object with a for...in Statement'
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用for ... in Statement中的对象键迭代
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有时您可能需要遍历对象中的所有键。这需要JavaScript中的特定语法,称为<dfn>for ... in</dfn>语句。对于我们的<code>users</code>对象,这可能看起来像: <blockquote> for(让用户在用户中){ <br>的console.log(用户); <br> }; <br><br> //日志: <br>艾伦<br>杰夫<br>莎拉<br>瑞安</blockquote>在这个语句中,我们定义了一个变量<code>user</code> ,正如您所看到的,在每次迭代期间,当该语句循环遍历该对象时,该变量被重置为每个对象的键,从而导致每个用户的名称被打印到控制台。 <strong>注意:</strong> <br>对象不像数组那样保持对存储键的排序;因此,当引用或访问该密钥时,对象上的键位置或其出现的相对顺序是无关紧要的。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们定义了一个函数<code>countOnline</code> ;在此函数中使用<dfn>for ... in</dfn>语句循环访问<code>users</code>对象中的<code>users</code>并返回其<code>online</code>属性设置为<code>true</code>的用户数。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>users</code>对象包含用户<code>Jeff</code>和<code>Ryan</code> , <code>online</code>设置为<code>true</code> ,用户<code>Alan</code>和<code>Sarah</code> <code>online</code>设置为<code>false</code>
|
||||
testString: 'assert(users.Alan.online === false && users.Jeff.online === true && users.Sarah.online === false && users.Ryan.online === true, "The <code>users</code> object contains users <code>Jeff</code> and <code>Ryan</code> with <code>online</code> set to <code>true</code> and users <code>Alan</code> and <code>Sarah</code> with <code>online</code> set to <code>false</code>");'
|
||||
- text: 函数<code>countOnline</code>返回<code>online</code>属性设置为<code>true</code>的用户数
|
||||
testString: 'assert((function() { users.Harry = {online: true}; users.Sam = {online: true}; users.Carl = {online: true}; return countOnline(users) })() === 5, "The function <code>countOnline</code> returns the number of users with the <code>online</code> property set to <code>true</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let users = {
|
||||
Alan: {
|
||||
age: 27,
|
||||
online: false
|
||||
},
|
||||
Jeff: {
|
||||
age: 32,
|
||||
online: true
|
||||
},
|
||||
Sarah: {
|
||||
age: 48,
|
||||
online: false
|
||||
},
|
||||
Ryan: {
|
||||
age: 19,
|
||||
online: true
|
||||
}
|
||||
};
|
||||
|
||||
function countOnline(obj) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
console.log(countOnline(users));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,59 @@
|
||||
---
|
||||
id: 5a661e0f1068aca922b3ef17
|
||||
title: Access an Array's Contents Using Bracket Notation
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用括号表示法访问数组的内容
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">当然,任何数据结构的基本特征是不仅能够存储数据,而且能够根据命令检索该数据。所以,既然我们已经学会了如何创建数组,那么让我们开始考虑如何访问该数组的信息。当我们定义一个如下所示的简单数组时,其中有3个项目: <blockquote>让ourArray = [“a”,“b”,“c”]; </blockquote>在数组中,每个数组项都有一个<dfn>索引</dfn> 。此索引兼作数组中该项的位置,以及您如何引用它。但是,值得注意的是,JavaScript数组是<dfn>零索引的</dfn> ,这意味着数组的第一个元素实际上处于第<em><strong>零</strong></em>位,而不是第一个。为了从数组中检索元素,我们可以将一个索引括在括号中,并将其附加到数组的末尾,或者更常见的是附加到引用数组对象的变量。这称为<dfn>括号表示法</dfn> 。例如,如果我们想从<code>ourArray</code>检索<code>"a"</code>并将其分配给变量,我们可以使用以下代码执行此操作: <blockquote>让ourVariable = ourArray [0]; <br> // ourVariable等于“a” </blockquote>除了访问与索引相关的值,你还可以<em>设置</em>索引使用相同的符号中的值: <blockquote> ourArray [1] =“不再是b”; <br> // ourArray现在等于[“a”,“不再b”,“c”]; </blockquote>使用括号表示法,我们现在将索引1处的项目从<code>"b"</code>重置为<code>"not b anymore"</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">为了完成此挑战,将<code>myArray</code>的第二个位置(索引<code>1</code> )设置为您想要的任何内容,除了<code>"b"</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>myArray[0]</code>等于<code>"a"</code>'
|
||||
testString: 'assert.strictEqual(myArray[0], "a", "<code>myArray[0]</code> is equal to <code>"a"</code>");'
|
||||
- text: '<code>myArray[1]</code>不再设置为<code>"b"</code>'
|
||||
testString: 'assert.notStrictEqual(myArray[1], "b", "<code>myArray[1]</code> is no longer set to <code>"b"</code>");'
|
||||
- text: '<code>myArray[2]</code>等于<code>"c"</code>'
|
||||
testString: 'assert.strictEqual(myArray[2], "c", "<code>myArray[2]</code> is equal to <code>"c"</code>");'
|
||||
- text: '<code>myArray[3]</code>等于<code>"d"</code>'
|
||||
testString: 'assert.strictEqual(myArray[3], "d", "<code>myArray[3]</code> is equal to <code>"d"</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let myArray = ["a", "b", "c", "d"];
|
||||
// change code below this line
|
||||
|
||||
//change code above this line
|
||||
console.log(myArray);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 587d7b7c367417b2b2512b1a
|
||||
title: Access Property Names with Bracket Notation
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用括号表示法访问属性名称
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在第一个对象挑战中,我们提到使用括号表示法作为使用变量求值来访问属性值的方法。例如,假设我们的<code>foods</code>对象被用于超市收银机的程序中。我们有一些设置<code>selectedFood</code>功能,我们想检查我们的<code>foods</code>对象是否存在该食物。这可能看起来像: <blockquote> let selectedFood = getCurrentFood(scanningItem); <br>让库存=食物[selectedFood]; </blockquote>此代码将评估存储在<code>selectedFood</code>变量中的值,并在<code>foods</code>对象中返回该键的值,如果不存在则返回<code>undefined</code> 。括号表示法非常有用,因为有时候对象属性在运行时之前是未知的,或者我们需要以更动态的方式访问它们。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们定义了一个函数<code>checkInventory</code> ,它接收一个扫描的项目作为参数。返回<code>foods</code>对象中的<code>scannedItem</code>键的当前值。您可以假设只有有效键将作为<code>checkInventory</code>的参数提供。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>checkInventory</code>是一个函数
|
||||
testString: 'assert.strictEqual(typeof checkInventory, "function", "<code>checkInventory</code> is a function");'
|
||||
- text: '<code>foods</code>对象应该只有以下键值对: <code>apples: 25</code> , <code>oranges: 32</code> , <code>plums: 28</code> , <code>bananas: 13</code> , <code>grapes: 35</code> , <code>strawberries: 27</code>'
|
||||
testString: 'assert.deepEqual(foods, {apples: 25, oranges: 32, plums: 28, bananas: 13, grapes: 35, strawberries: 27}, "The <code>foods</code> object should have only the following key-value pairs: <code>apples: 25</code>, <code>oranges: 32</code>, <code>plums: 28</code>, <code>bananas: 13</code>, <code>grapes: 35</code>, <code>strawberries: 27</code>");'
|
||||
- text: <code>checkInventory("apples")</code>应该返回<code>25</code>
|
||||
testString: 'assert.strictEqual(checkInventory("apples"), 25, "<code>checkInventory("apples")</code> should return <code>25</code>");'
|
||||
- text: <code>checkInventory("bananas")</code>应该返回<code>13</code>
|
||||
testString: 'assert.strictEqual(checkInventory("bananas"), 13, "<code>checkInventory("bananas")</code> should return <code>13</code>");'
|
||||
- text: <code>checkInventory("strawberries")</code>应该返回<code>27</code>
|
||||
testString: 'assert.strictEqual(checkInventory("strawberries"), 27, "<code>checkInventory("strawberries")</code> should return <code>27</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let foods = {
|
||||
apples: 25,
|
||||
oranges: 32,
|
||||
plums: 28,
|
||||
bananas: 13,
|
||||
grapes: 35,
|
||||
strawberries: 27
|
||||
};
|
||||
// do not change code above this line
|
||||
|
||||
function checkInventory(scannedItem) {
|
||||
// change code below this line
|
||||
|
||||
}
|
||||
|
||||
// change code below this line to test different cases:
|
||||
console.log(checkInventory("apples"));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,61 @@
|
||||
---
|
||||
id: 587d78b2367417b2b2512b0e
|
||||
title: Add Items to an Array with push() and unshift()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用push()和unshift()将项添加到数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">数组的长度与它可以包含的数据类型一样,并不固定。可以使用任意数量的元素的长度来定义数组,并且可以随时间添加或移除元素;换句话说,数组是<dfn>可变的</dfn> 。在这个挑战中,我们将研究两种方法,我们可以用它们以编程方式修改数组: <code>Array.push()</code>和<code>Array.unshift()</code> 。两种方法都将一个或多个元素作为参数,并将这些元素添加到调用该方法的数组中; <code>push()</code>方法将元素添加到数组的末尾, <code>unshift()</code>将元素添加到开头。考虑以下: <blockquote>让二十三'='XXIII'; <br>让romanNumerals = ['XXI','XXII']; <br><br> romanNumerals.unshift('XIX','XX'); <br> //现在等于['XIX','XX','XXI','XXII'] <br><br> romanNumerals.push(二十三); <br> //现在等于['XIX','XX','XXI','XXII','XXIII']请注意,我们也可以传递变量,这使我们可以更灵活地动态修改数组的数据。 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们定义了一个函数<code>mixedNumbers</code> ,我们将一个数组作为参数传递。修改函数使用<code>push()</code>和<code>unshift()</code>将<code>'I', 2, 'three'</code>到数组的开头,将<code>7, 'VIII', 9</code>到结尾,以便返回的数组包含数字的表示形式按顺序1-9。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>mixedNumbers(["IV", 5, "six"])</code>现在应该返回<code>["I", 2, "three", "IV", 5, "six", 7, "VIII", 9]</code>'
|
||||
testString: 'assert.deepEqual(mixedNumbers(["IV", 5, "six"]), ["I", 2, "three", "IV", 5, "six", 7, "VIII", 9], "<code>mixedNumbers(["IV", 5, "six"])</code> should now return <code>["I", 2, "three", "IV", 5, "six", 7, "VIII", 9]</code>");'
|
||||
- text: <code>mixedNumbers</code>函数应该使用<code>push()</code>方法
|
||||
testString: 'assert.notStrictEqual(mixedNumbers.toString().search(/\.push\(/), -1, "The <code>mixedNumbers</code> function should utilize the <code>push()</code> method");'
|
||||
- text: <code>mixedNumbers</code>函数应该使用<code>unshift()</code>方法
|
||||
testString: 'assert.notStrictEqual(mixedNumbers.toString().search(/\.unshift\(/), -1, "The <code>mixedNumbers</code> function should utilize the <code>unshift()</code> method");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function mixedNumbers(arr) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
return arr;
|
||||
}
|
||||
|
||||
// do not change code below this line
|
||||
console.log(mixedNumbers(['IV', 5, 'six']));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,63 @@
|
||||
---
|
||||
id: 587d78b3367417b2b2512b11
|
||||
title: Add Items Using splice()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用splice()添加项目
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">还记得在上一次挑战中我们提到过<code>splice()</code>最多需要三个参数吗?好吧,我们可以更进一步使用<code>splice()</code> - 除了删除元素之外,我们还可以使用代表一个或多个元素的第三个参数来<em>添加</em>它们。这对于快速切换另一个元素或一组元素非常有用。例如,假设您正在为数组中的一组DOM元素存储颜色方案,并希望根据某些操作动态更改颜色: <blockquote> function colorChange(arr,index,newColor){ <br> arr.splice(index,1,newColor); <br>返回<br> } <br><br>让colorScheme = ['#878787','#a08794','#bb7e8c','#c9b6be','#d1becf']; <br><br> colorScheme = colorChange(colorScheme,2,'#332327'); <br> //我们删除了'#bb7e8c'并在其位置添加了'#332327' <br> // colorScheme现在等于['#878787','#a08794','#332327','#c9b6be','#d1becf'] </blockquote>此函数采用十六进制值数组,删除元素的索引以及用于替换已删除元素的新颜色。返回值是一个包含新修改的颜色方案的数组!虽然这个例子有点过于简单,但我们可以看到利用<code>splice()</code>到其最大潜力的值。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们定义了一个函数<code>htmlColorNames</code> ,它将一组HTML颜色作为参数。使用<code>splice()</code>修改函数以删除数组的前两个元素,并在各自的位置添加<code>'DarkSalmon'</code>和<code>'BlanchedAlmond'</code> <code>'DarkSalmon'</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>htmlColorNames</code>应该返回<code>["DarkSalmon", "BlanchedAlmond", "LavenderBlush", "PaleTurqoise", "FireBrick"]</code>'
|
||||
testString: 'assert.deepEqual(htmlColorNames(["DarkGoldenRod", "WhiteSmoke", "LavenderBlush", "PaleTurqoise", "FireBrick"]), ["DarkSalmon", "BlanchedAlmond", "LavenderBlush", "PaleTurqoise", "FireBrick"], "<code>htmlColorNames</code> should return <code>["DarkSalmon", "BlanchedAlmond", "LavenderBlush", "PaleTurqoise", "FireBrick"]</code>");'
|
||||
- text: <code>htmlColorNames</code>函数应该使用<code>splice()</code>方法
|
||||
testString: 'assert(/.splice/.test(code), "The <code>htmlColorNames</code> function should utilize the <code>splice()</code> method");'
|
||||
- text: 你不应该使用<code>shift()</code>或<code>unshift()</code> 。
|
||||
testString: 'assert(!/shift|unshift/.test(code), "You should not use <code>shift()</code> or <code>unshift()</code>.");'
|
||||
- text: 您不应该使用数组括号表示法。
|
||||
testString: 'assert(!/\[\d\]\s*=/.test(code), "You should not use array bracket notation.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function htmlColorNames(arr) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
return arr;
|
||||
}
|
||||
|
||||
// do not change code below this line
|
||||
console.log(htmlColorNames(['DarkGoldenRod', 'WhiteSmoke', 'LavenderBlush', 'PaleTurqoise', 'FireBrick']));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,67 @@
|
||||
---
|
||||
id: 587d7b7c367417b2b2512b18
|
||||
title: Add Key-Value Pairs to JavaScript Objects
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 将键值对添加到JavaScript对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在最基本的情况下,对象只是<dfn>键值对的</dfn>集合,换句话说,映射到我们称为<dfn>属性</dfn>或<dfn>键的</dfn>唯一标识符的数据片段。我们来看一个非常简单的例子: <blockquote>让FCC_User = { <br>用户名:'awesome_coder', <br>粉丝:572, <br>积分:1741, <br>已完成项目:15 <br> }; </blockquote>上面的代码定义了一个名为<code>FCC_User</code>的对象,它有四个<dfn>属性</dfn> ,每个<dfn>属性</dfn>都映射到一个特定的值。如果我们想知道的数量<code>followers</code> <code>FCC_User</code>了,我们可以通过写访问属性: <blockquote> let userData = FCC_User.followers; <br> // userData等于572 </blockquote>这称为<dfn>点符号</dfn> 。或者,我们也可以使用括号访问该属性,如下所示: <blockquote>让userData = FCC_User ['粉丝'] <br> // userData等于572 </blockquote>请注意,带<dfn>支架的符号</dfn> ,我们封闭<code>followers</code>在引号。这是因为括号实际上允许我们传递一个变量以作为属性名称进行评估(提示:请记住这一点以供日后使用!)。如果我们在没有引号的情况下传递了<code>followers</code> ,那么JavaScript引擎会尝试将其作为变量进行评估,而<code>ReferenceError: followers is not defined</code>将被抛出。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用相同的语法,我们还<em><strong>可以</strong></em>向对象<em><strong>添加新的</strong></em>键值对。我们用三个条目创建了一个<code>foods</code>对象。再添加三个条目:价值为<code>13</code> <code>bananas</code> ,价值为<code>35</code> <code>grapes</code>和价值为<code>27</code> <code>strawberries</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>foods</code>是一个对象
|
||||
testString: 'assert(typeof foods === "object", "<code>foods</code> is an object");'
|
||||
- text: <code>foods</code>对象有一个值为<code>13</code>的关键<code>"bananas"</code>
|
||||
testString: 'assert(foods.bananas === 13, "The <code>foods</code> object has a key <code>"bananas"</code> with a value of <code>13</code>");'
|
||||
- text: <code>foods</code>对象有一个关键的<code>"grapes"</code> ,价值<code>35</code>
|
||||
testString: 'assert(foods.grapes === 35, "The <code>foods</code> object has a key <code>"grapes"</code> with a value of <code>35</code>");'
|
||||
- text: <code>foods</code>对象有一个关键的<code>"strawberries"</code> ,值为<code>27</code>
|
||||
testString: 'assert(foods.strawberries === 27, "The <code>foods</code> object has a key <code>"strawberries"</code> with a value of <code>27</code>");'
|
||||
- text: 应使用点或括号表示法设置键值对
|
||||
testString: 'assert(code.search(/bananas:/) === -1 && code.search(/grapes:/) === -1 && code.search(/strawberries:/) === -1, "The key-value pairs should be set using dot or bracket notation");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let foods = {
|
||||
apples: 25,
|
||||
oranges: 32,
|
||||
plums: 28
|
||||
};
|
||||
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
|
||||
console.log(foods);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 587d7b7b367417b2b2512b14
|
||||
title: Check For The Presence of an Element With indexOf()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用indexOf()检查元素是否存在
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">由于数组可以随时更改或<em>变异</em> ,因此无法保证特定数据在特定数组中的位置,或者该元素是否仍然存在。幸运的是,JavaScript为我们提供了另一种内置方法<code>indexOf()</code> ,它允许我们快速,轻松地检查数组中元素的存在。 <code>indexOf()</code>接受一个元素作为参数,并在调用时返回该元素的位置或索引,如果该元素在数组中不存在,则返回<code>-1</code> 。例如: <blockquote>让水果= ['苹果','梨','橙子','桃子','梨子']; <br><br> fruits.indexOf('dates')//返回-1 <br> fruits.indexOf('oranges')//返回2 <br> fruits.indexOf('pears')//返回1,元素所在的第一个索引</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions"> <code>indexOf()</code>对于快速检查数组中是否存在元素非常有用。我们定义了一个函数<code>quickCheck</code> ,它将一个数组和一个元素作为参数。使用<code>indexOf()</code>修改函数,以便在传递的元素存在于数组时返回<code>true</code>如果不存在则返回<code>false</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>quickCheck(["squash", "onions", "shallots"], "mushrooms")</code>应该返回<code>false</code>'
|
||||
testString: 'assert.strictEqual(quickCheck(["squash", "onions", "shallots"], "mushrooms"), false, "<code>quickCheck(["squash", "onions", "shallots"], "mushrooms")</code> should return <code>false</code>");'
|
||||
- text: '<code>quickCheck(["squash", "onions", "shallots"], "onions")</code>应该返回<code>true</code>'
|
||||
testString: 'assert.strictEqual(quickCheck(["squash", "onions", "shallots"], "onions"), true, "<code>quickCheck(["squash", "onions", "shallots"], "onions")</code> should return <code>true</code>");'
|
||||
- text: '<code>quickCheck([3, 5, 9, 125, 45, 2], 125)</code>应该返回<code>true</code>'
|
||||
testString: 'assert.strictEqual(quickCheck([3, 5, 9, 125, 45, 2], 125), true, "<code>quickCheck([3, 5, 9, 125, 45, 2], 125)</code> should return <code>true</code>");'
|
||||
- text: '<code>quickCheck([true, false, false], undefined)</code>应返回<code>false</code>'
|
||||
testString: 'assert.strictEqual(quickCheck([true, false, false], undefined), false, "<code>quickCheck([true, false, false], undefined)</code> should return <code>false</code>");'
|
||||
- text: <code>quickCheck</code>函数应该使用<code>indexOf()</code>方法
|
||||
testString: 'assert.notStrictEqual(quickCheck.toString().search(/\.indexOf\(/), -1, "The <code>quickCheck</code> function should utilize the <code>indexOf()</code> method");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function quickCheck(arr, elem) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
// change code here to test different cases:
|
||||
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 587d7b7d367417b2b2512b1c
|
||||
title: Check if an Object has a Property
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 检查对象是否具有属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在我们可以添加,修改和删除对象中的键。但是,如果我们只是想知道某个对象是否具有特定属性呢? JavaScript为我们提供了两种不同的方法。一个使用<code>hasOwnProperty()</code>方法,另一个使用<code>in</code>关键字。如果我们有一个具有<code>Alan</code>属性的对象<code>users</code> ,我们可以通过以下任一方式检查其存在: <blockquote> users.hasOwnProperty( '艾伦'); <br> '艾伦'在用户; <br> //都返回true </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们创建了一个对象, <code>users</code> ,其中包含一些用户和一个函数<code>isEveryoneHere</code> ,我们将<code>users</code>对象作为参数传递给它。完成编写此函数,以便仅当<code>users</code>对象包含所有四个名称( <code>Alan</code> , <code>Jeff</code> , <code>Sarah</code>和<code>Ryan</code> )作为键时才返回<code>true</code> ,否则返回<code>false</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>users</code>对象仅包含<code>Alan</code> , <code>Jeff</code> , <code>Sarah</code>和<code>Ryan</code>
|
||||
testString: 'assert("Alan" in users && "Jeff" in users && "Sarah" in users && "Ryan" in users && Object.keys(users).length === 4, "The <code>users</code> object only contains the keys <code>Alan</code>, <code>Jeff</code>, <code>Sarah</code>, and <code>Ryan</code>");'
|
||||
- text: 如果<code>Alan</code> , <code>Jeff</code> , <code>Sarah</code>和<code>Ryan</code>是<code>users</code>对象的属性,则函数<code>isEveryoneHere</code>返回<code>true</code>
|
||||
testString: 'assert(isEveryoneHere(users) === true, "The function <code>isEveryoneHere</code> returns <code>true</code> if <code>Alan</code>, <code>Jeff</code>, <code>Sarah</code>, and <code>Ryan</code> are properties on the <code>users</code> object");'
|
||||
- text: 如果<code>Alan</code> , <code>Jeff</code> , <code>Sarah</code>和<code>Ryan</code>不是<code>users</code>对象的属性,则函数<code>isEveryoneHere</code>返回<code>false</code>
|
||||
testString: 'assert((function() { delete users.Alan; delete users.Jeff; delete users.Sarah; delete users.Ryan; return isEveryoneHere(users) })() === false, "The function <code>isEveryoneHere</code> returns <code>false</code> if <code>Alan</code>, <code>Jeff</code>, <code>Sarah</code>, and <code>Ryan</code> are not properties on the <code>users</code> object");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let users = {
|
||||
Alan: {
|
||||
age: 27,
|
||||
online: true
|
||||
},
|
||||
Jeff: {
|
||||
age: 32,
|
||||
online: true
|
||||
},
|
||||
Sarah: {
|
||||
age: 48,
|
||||
online: true
|
||||
},
|
||||
Ryan: {
|
||||
age: 19,
|
||||
online: true
|
||||
}
|
||||
};
|
||||
|
||||
function isEveryoneHere(obj) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
console.log(isEveryoneHere(users));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,58 @@
|
||||
---
|
||||
id: 587d7b7b367417b2b2512b17
|
||||
title: Combine Arrays with the Spread Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 将数组与Spread运算符组合在一起
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <dfn>扩展</dfn>运算符的另一个巨大优势是能够组合数组,或者在任何索引处将一个数组的所有元素插入到另一个数组中。使用更传统的语法,我们可以连接数组,但这只允许我们在一个数组的末尾和另一个数组的开头组合数组。 Spread语法使以下操作非常简单: <blockquote>让thisArray = ['sage','迷迭香','欧芹','百里香']; <br><br>让那个阵容= ['罗勒','香菜',......这个阿雷,'香菜']; <br> //现在等于['罗勒','香菜','鼠尾草','迷迭香','欧芹','百里香','香菜'] </blockquote>使用扩展语法,我们刚刚实现了一个操作,如果我们使用传统方法,这个操作会更复杂,更冗长。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们定义了一个函数<code>spreadOut</code> ,它返回变量<code>sentence</code> ,使用<dfn>spread</dfn>运算符修改函数,使它返回数组<code>['learning', 'to', 'code', 'is', 'fun']</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>spreadOut</code>应该返回<code>["learning", "to", "code", "is", "fun"]</code>'
|
||||
testString: 'assert.deepEqual(spreadOut(), ["learning", "to", "code", "is", "fun"], "<code>spreadOut</code> should return <code>["learning", "to", "code", "is", "fun"]</code>");'
|
||||
- text: <code>spreadOut</code>函数应该使用扩展语法
|
||||
testString: 'assert.notStrictEqual(spreadOut.toString().search(/[...]/), -1, "The <code>spreadOut</code> function should utilize spread syntax");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function spreadOut() {
|
||||
let fragment = ['to', 'code'];
|
||||
let sentence; // change this line
|
||||
return sentence;
|
||||
}
|
||||
|
||||
// do not change code below this line
|
||||
console.log(spreadOut());
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,69 @@
|
||||
---
|
||||
id: 587d7b7b367417b2b2512b13
|
||||
title: Copy an Array with the Spread Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用Spread Operator复制数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">虽然<code>slice()</code>允许我们选择要复制的数组元素,但在其他几个有用的任务中,ES6的新<dfn>扩展运算符</dfn>允许我们使用简单且高度可读的语法轻松地按顺序复制<em>所有</em>数组的元素。扩展语法看起来像这样: <code>...</code>在实践中,我们可以使用spread运算符来复制数组,如下所示: <blockquote> let thisArray = [true,true,undefined,false,null]; <br>让thatArray = [... thisArray]; <br> // thatArray等于[true,true,undefined,false,null] <br> // thisArray保持不变,与thatArray相同</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们定义了一个函数<code>copyMachine</code> ,它将<code>arr</code> (数组)和<code>num</code> (数字)作为参数。该函数应该返回一个由<code>arr</code>的<code>num</code>副本组成的新数组。我们为您完成了大部分工作,但它还没有正常工作。使用扩展语法修改函数以使其正常工作(提示:我们已经介绍过的另一种方法可能会派上用场!)。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>copyMachine([true, false, true], 2)</code>应返回<code>[[true, false, true], [true, false, true]]</code>'
|
||||
testString: 'assert.deepEqual(copyMachine([true, false, true], 2), [[true, false, true], [true, false, true]], "<code>copyMachine([true, false, true], 2)</code> should return <code>[[true, false, true], [true, false, true]]</code>");'
|
||||
- text: '<code>copyMachine([1, 2, 3], 5)</code>应返回<code>[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]</code>'
|
||||
testString: 'assert.deepEqual(copyMachine([1, 2, 3], 5), [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], "<code>copyMachine([1, 2, 3], 5)</code> should return <code>[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]</code>");'
|
||||
- text: '<code>copyMachine([true, true, null], 1)</code>应该返回<code>[[true, true, null]]</code>'
|
||||
testString: 'assert.deepEqual(copyMachine([true, true, null], 1), [[true, true, null]], "<code>copyMachine([true, true, null], 1)</code> should return <code>[[true, true, null]]</code>");'
|
||||
- text: '<code>copyMachine(["it works"], 3)</code>应该返回<code>[["it works"], ["it works"], ["it works"]]</code>'
|
||||
testString: 'assert.deepEqual(copyMachine(["it works"], 3), [["it works"], ["it works"], ["it works"]], "<code>copyMachine(["it works"], 3)</code> should return <code>[["it works"], ["it works"], ["it works"]]</code>");'
|
||||
- text: <code>copyMachine</code>函数应该使用带有数组<code>arr</code>的<code>spread operator</code>
|
||||
testString: 'assert.notStrictEqual(copyMachine.toString().indexOf(".concat(_toConsumableArray(arr))"), -1, "The <code>copyMachine</code> function should utilize the <code>spread operator</code> with array <code>arr</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function copyMachine(arr, num) {
|
||||
let newArr = [];
|
||||
while (num >= 1) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
num--;
|
||||
}
|
||||
return newArr;
|
||||
}
|
||||
|
||||
// change code here to test different cases:
|
||||
console.log(copyMachine([true, false, true], 2));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,58 @@
|
||||
---
|
||||
id: 587d7b7a367417b2b2512b12
|
||||
title: Copy Array Items Using slice()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用slice()复制数组项
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们将介绍的下一个方法是<code>slice()</code> 。 <code>slice()</code> ,而不是修改数组,将给定数量的元素复制或<em>提取</em>到新数组,而不改变它所调用的数组。 <code>slice()</code>只接受2个参数 - 第一个是开始提取的索引,第二个是停止提取的索引(提取将发生,但不包括此索引处的元素)。考虑一下: <blockquote>让weatherConditions = ['rain','snow','sleet','hail','clear']; <br><br>让todaysWeather = weatherConditions.slice(1,3); <br> //今天天气等于['雪','雨夹雪']; <br> // weatherConditions仍等于['rain','snow','sleet','hail','clear'] <br></blockquote>实际上,我们通过从现有数组中提取元素来创建一个新数组。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们定义了一个以数组作为参数的函数<code>forecast</code> 。使用<code>slice()</code>修改函数以从参数数组中提取信息,并返回包含元素<code>'warm'</code>和<code>'sunny'</code>的新数组。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>forecast</code>应该返回<code>["warm", "sunny"]</code>'
|
||||
testString: 'assert.deepEqual(forecast(["cold", "rainy", "warm", "sunny", "cool", "thunderstorms"]), ["warm", "sunny"], "<code>forecast</code> should return <code>["warm", "sunny"]");'
|
||||
- text: <code>forecast</code>函数应该使用<code>slice()</code>方法
|
||||
testString: 'assert(/\.slice\(/.test(code), "The <code>forecast</code> function should utilize the <code>slice()</code> method");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function forecast(arr) {
|
||||
// change code below this line
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
// do not change code below this line
|
||||
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 587d7b7b367417b2b2512b16
|
||||
title: Create complex multi-dimensional arrays
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 创建复杂的多维数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">真棒!你刚学了很多关于阵列的知识!这是一个相当高级别的概述,有很多关于使用数组的知识,其中大部分将在后面的章节中看到。但在继续观察<dfn>对象之前</dfn> ,让我们再看一下,看看阵列如何变得比我们在之前的挑战中看到的更复杂。将数组视为数据结构时,最强大的功能之一是数组可以包含甚至完全由其他数组组成。我们已经看到在之前的挑战中包含数组的数组,但相当简单。但是,数组可以包含无限深度的数组,这些数组可以包含其他数组,每个数组都有自己的任意深度级别,依此类推。通过这种方式,数组可以非常快速地变成非常复杂的数据结构,称为<dfn>多维</dfn>或嵌套数组。请考虑以下示例: <blockquote>让nestedArray = [// top,或first level - 最外面的数组<br> ['deep'],//数组中的数组,2个深度级别<br> [ <br> ['更深'],['更深'] // 2个数组嵌套3层深<br> ] <br> [ <br> [ <br> ['deepest'],['最深'] // 2个数组嵌套了4个级别<br> ] <br> [ <br> [ <br> ['deepest-est?'] //一个数组嵌套5层深<br> ] <br> ] <br> ] <br> ]。 </blockquote>虽然这个例子看似令人费解,但在处理大量数据时,这种复杂程度并非闻所未闻,甚至不同寻常。但是,我们仍然可以使用括号表示法轻松访问此复杂数组的最深层次: <blockquote>的console.log(nestedArray [2] [1] [0] [0] [0]); <br> //日志:最深? </blockquote>现在我们知道那条数据在哪里,如果需要,我们可以重置它: <blockquote> nestedArray [2] [1] [0] [0] [0] =“更深”; <br><br>的console.log(nestedArray [2] [1] [0] [0] [0]); <br> //现在记录:更深入</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们已经定义了一个变量<code>myNestedArray</code> ,它等于一个数组。修改<code>myNestedArray</code> ,使用数据元素的<dfn>字符串</dfn> , <dfn>数字</dfn>和<dfn>布尔值的</dfn>任意组合,以便它具有五个深度级别(请记住,最外层的数组是级别1)。某处在第三级上,包括字符串<code>'deep'</code> ,在第四级,包括字符串<code>'deeper'</code> ,并且在第五层上,包括字符串<code>'deepest'</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myNestedArray</code>应仅包含数字,布尔值和字符串作为数据元素
|
||||
testString: 'assert.strictEqual((function(arr) { let flattened = (function flatten(arr) { const flat = [].concat(...arr); return flat.some (Array.isArray) ? flatten(flat) : flat; })(arr); for (let i = 0; i < flattened.length; i++) { if ( typeof flattened[i] !== "number" && typeof flattened[i] !== "string" && typeof flattened[i] !== "boolean") { return false } } return true })(myNestedArray), true, "<code>myNestedArray</code> should contain only numbers, booleans, and strings as data elements");'
|
||||
- text: <code>myNestedArray</code>应该有5个级别的深度
|
||||
testString: 'assert.strictEqual((function(arr) {let depth = 0;function arrayDepth(array, i, d) { if (Array.isArray(array[i])) { arrayDepth(array[i], 0, d + 1);} else { depth = (d > depth) ? d : depth;}if (i < array.length) { arrayDepth(array, i + 1, d);} }arrayDepth(arr, 0, 0);return depth;})(myNestedArray), 4, "<code>myNestedArray</code> should have exactly 5 levels of depth");'
|
||||
- text: <code>myNestedArray</code>应该在嵌套3级深度的数组中恰好包含一个字符串<code>"deep"</code>
|
||||
testString: 'assert((function howDeep(array, target, depth = 0) {return array.reduce((combined, current) => {if (Array.isArray(current)) { return combined.concat(howDeep(current, target, depth + 1));} else if (current === target) { return combined.concat(depth);} else { return combined;}}, []);})(myNestedArray, "deep").length === 1 && (function howDeep(array, target, depth = 0) {return array.reduce((combined, current) => {if (Array.isArray(current)) { return combined.concat(howDeep(current, target, depth + 1));} else if (current === target) { return combined.concat(depth);} else { return combined;}}, []);})(myNestedArray, "deep")[0] === 2, "<code>myNestedArray</code> should contain exactly one occurrence of the string <code>"deep"</code> on an array nested 3 levels deep");'
|
||||
- text: <code>myNestedArray</code>应该在嵌套4级深度的数组中恰好包含一个字符串<code>"deeper"</code> deep”
|
||||
testString: 'assert((function howDeep(array, target, depth = 0) {return array.reduce((combined, current) => {if (Array.isArray(current)) { return combined.concat(howDeep(current, target, depth + 1));} else if (current === target) { return combined.concat(depth);} else { return combined;}}, []);})(myNestedArray, "deeper").length === 1 && (function howDeep(array, target, depth = 0) {return array.reduce((combined, current) => {if (Array.isArray(current)) { return combined.concat(howDeep(current, target, depth + 1));} else if (current === target) { return combined.concat(depth);} else { return combined;}}, []);})(myNestedArray, "deeper")[0] === 3, "<code>myNestedArray</code> should contain exactly one occurrence of the string <code>"deeper"</code> on an array nested 4 levels deep");'
|
||||
- text: <code>myNestedArray</code>应该在嵌套5级深度的数组中恰好包含一个字符串<code>"deepest"</code> <code>myNestedArray</code> <code>"deepest"</code>
|
||||
testString: 'assert((function howDeep(array, target, depth = 0) {return array.reduce((combined, current) => {if (Array.isArray(current)) { return combined.concat(howDeep(current, target, depth + 1));} else if (current === target) { return combined.concat(depth);} else { return combined;}}, []);})(myNestedArray, "deepest").length === 1 && (function howDeep(array, target, depth = 0) {return array.reduce((combined, current) => {if (Array.isArray(current)) { return combined.concat(howDeep(current, target, depth + 1));} else if (current === target) { return combined.concat(depth);} else { return combined;}}, []);})(myNestedArray, "deepest")[0] === 4, "<code>myNestedArray</code> should contain exactly one occurrence of the string <code>"deepest"</code> on an array nested 5 levels deep");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let myNestedArray = [
|
||||
// change code below this line
|
||||
['unshift', false, 1, 2, 3, 'complex', 'nested'],
|
||||
['loop', 'shift', 6, 7, 1000, 'method'],
|
||||
['concat', false, true, 'spread', 'array'],
|
||||
['mutate', 1327.98, 'splice', 'slice', 'push'],
|
||||
['iterate', 1.3849, 7, '8.4876', 'arbitrary', 'depth']
|
||||
// change code above this line
|
||||
];
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 587d7b7d367417b2b2512b1e
|
||||
title: Generate an Array of All Object Keys with Object.keys()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用Object.keys()生成所有对象键的数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们还可以使用<code>Object.keys()</code>方法生成一个数组,其中包含存储在对象中的所有键,并传入一个对象作为参数。这将返回一个数组,其中的字符串表示对象中的每个属性。同样,数组中的条目没有特定的顺序。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">完成编写<code>getArrayOfUsers</code>函数,以便它返回一个数组,该数组包含它作为参数接收的对象中的所有属性。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>users</code>对象仅包含<code>Alan</code> , <code>Jeff</code> , <code>Sarah</code>和<code>Ryan</code>
|
||||
testString: 'assert("Alan" in users && "Jeff" in users && "Sarah" in users && "Ryan" in users && Object.keys(users).length === 4, "The <code>users</code> object only contains the keys <code>Alan</code>, <code>Jeff</code>, <code>Sarah</code>, and <code>Ryan</code>");'
|
||||
- text: <code>getArrayOfUsers</code>函数返回一个数组,其中包含<code>users</code>对象中的所有键
|
||||
testString: 'assert((function() { users.Sam = {}; users.Lewis = {}; let R = getArrayOfUsers(users); return (R.indexOf("Alan") !== -1 && R.indexOf("Jeff") !== -1 && R.indexOf("Sarah") !== -1 && R.indexOf("Ryan") !== -1 && R.indexOf("Sam") !== -1 && R.indexOf("Lewis") !== -1); })() === true, "The <code>getArrayOfUsers</code> function returns an array which contains all the keys in the <code>users</code> object");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let users = {
|
||||
Alan: {
|
||||
age: 27,
|
||||
online: false
|
||||
},
|
||||
Jeff: {
|
||||
age: 32,
|
||||
online: true
|
||||
},
|
||||
Sarah: {
|
||||
age: 48,
|
||||
online: false
|
||||
},
|
||||
Ryan: {
|
||||
age: 19,
|
||||
online: true
|
||||
}
|
||||
};
|
||||
|
||||
function getArrayOfUsers(obj) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
console.log(getArrayOfUsers(users));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 587d7b7b367417b2b2512b15
|
||||
title: Iterate Through All an Array's Items Using For Loops
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用for循环遍历所有数组的项目
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有时在使用数组时,能够遍历每个项目以查找我们可能需要的一个或多个元素,或者根据哪些数据项符合某组标准来操作数组非常方便。 JavaScript提供了几种内置方法,每种方法都以稍微不同的方式迭代数组以实现不同的结果(例如<code>every()</code> , <code>forEach()</code> , <code>map()</code>等),但是这种技术最灵活并且为我们提供了最大的控制量是一个简单的<code>for</code>循环。考虑以下: <blockquote> function greaterThanTen(arr){ <br>让newArr = []; <br> for(let i = 0; i <arr.length; i ++){ <br> if(arr [i]> 10){ <br> (ARR [I])newArr.push; <br> } <br> } <br>返回newArr; <br> } <br><br> greaterThanTen([2,12,8,14,80,0,1]); <br> //返回[12,14,80] </blockquote>使用<code>for</code>循环,此函数遍历并访问数组的每个元素,并使其经受我们创建的简单测试。通过这种方式,我们可以轻松地以编程方式确定哪些数据项大于<code>10</code> ,并返回包含这些项的新数组。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们定义了一个函数<code>filteredArray</code> ,它接受<code>arr</code> ,一个嵌套数组和<code>elem</code>作为参数,并返回一个新数组。 <code>elem</code>表示在<code>arr</code>嵌套的一个或多个数组上可能存在或不存在的元素。使用<code>for</code>循环修改函数,以返回已传递数组的过滤版本,以便删除嵌套在包含<code>elem</code> <code>arr</code>中的任何数组。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)</code>应该返回<code>[ [10, 8, 3], [14, 6, 23] ]</code>'
|
||||
testString: 'assert.deepEqual(filteredArray([ [10, 8, 3], [14, 6, 23], [3, 18, 6] ], 18), [[10, 8, 3], [14, 6, 23]], "<code>filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)</code> should return <code>[ [10, 8, 3], [14, 6, 23] ]</code>");'
|
||||
- text: '<code>filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)</code>应返回<code>[ ["flutes", 4] ]</code>'
|
||||
testString: 'assert.deepEqual(filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2), [["flutes", 4]], "<code>filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)</code> should return <code>[ ["flutes", 4] ]</code>");'
|
||||
- text: '<code>filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")</code>应该返回<code>[ ["amy", "beth", "sam"] ]</code>'
|
||||
testString: 'assert.deepEqual(filteredArray([["amy", "beth", "sam"], ["dave", "sean", "peter"]], "peter"), [["amy", "beth", "sam"]], "<code>filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")</code> should return <code>[ ["amy", "beth", "sam"] ]</code>");'
|
||||
- text: '<code>filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)</code>应该返回<code>[ ]</code>'
|
||||
testString: 'assert.deepEqual(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3), [], "<code>filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)</code> should return <code>[ ]</code>");'
|
||||
- text: <code>filteredArray</code>函数应该使用<code>for</code>循环
|
||||
testString: 'assert.notStrictEqual(filteredArray.toString().search(/for/), -1, "The <code>filteredArray</code> function should utilize a <code>for</code> loop");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function filteredArray(arr, elem) {
|
||||
let newArr = [];
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
return newArr;
|
||||
}
|
||||
|
||||
// change code here to test different cases:
|
||||
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,79 @@
|
||||
---
|
||||
id: 587d7b7d367417b2b2512b1f
|
||||
title: Modify an Array Stored in an Object
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 修改存储在对象中的数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在您已经看到了JavaScript对象的所有基本操作。您可以添加,修改和删除键值对,检查键是否存在,并迭代对象中的所有键。随着您继续学习JavaScript,您将看到更多功能的对象应用程序。此外,课程后面的可选高级数据结构课程还涵盖ES6 <dfn>Map</dfn>和<dfn>Set</dfn>对象,这两个对象与普通对象类似,但提供了一些附加功能。既然您已经学习了数组和对象的基础知识,那么您已经准备好开始使用JavaScript解决更复杂的问题了! </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">看看我们在代码编辑器中提供的对象。 <code>user</code>对象包含三个键。 <code>data</code>键包含五个键,其中一个键包含一组<code>friends</code> 。从这里,您可以看到灵活的对象如何作为数据结构。我们已经开始编写一个函数<code>addFriend</code> 。完成编写它以便它获取<code>user</code>对象并将<code>friend</code>参数的名称添加到存储在<code>user.data.friends</code>中的数组并返回该数组。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>user</code>对象具有<code>name</code> , <code>age</code>和<code>data</code>键
|
||||
testString: 'assert("name" in user && "age" in user && "data" in user, "The <code>user</code> object has <code>name</code>, <code>age</code>, and <code>data</code> keys");'
|
||||
- text: <code>addFriend</code>函数接受<code>user</code>对象和<code>friend</code>字符串作为参数,并将朋友添加到<code>user</code>对象中的<code>friends</code>数组
|
||||
testString: 'assert((function() { let L1 = user.data.friends.length; addFriend(user, "Sean"); let L2 = user.data.friends.length; return (L2 === L1 + 1); })(), "The <code>addFriend</code> function accepts a <code>user</code> object and a <code>friend</code> string as arguments and adds the friend to the array of <code>friends</code> in the <code>user</code> object");'
|
||||
- text: '<code>addFriend(user, "Pete")</code>应该返回<code>["Sam", "Kira", "Tomo", "Pete"]</code>'
|
||||
testString: 'assert.deepEqual((function() { delete user.data.friends; user.data.friends = ["Sam", "Kira", "Tomo"]; return addFriend(user, "Pete") })(), ["Sam", "Kira", "Tomo", "Pete"], "<code>addFriend(user, "Pete")</code> should return <code>["Sam", "Kira", "Tomo", "Pete"]</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let user = {
|
||||
name: 'Kenneth',
|
||||
age: 28,
|
||||
data: {
|
||||
username: 'kennethCodesAllDay',
|
||||
joinDate: 'March 26, 2016',
|
||||
organization: 'freeCodeCamp',
|
||||
friends: [
|
||||
'Sam',
|
||||
'Kira',
|
||||
'Tomo'
|
||||
],
|
||||
location: {
|
||||
city: 'San Francisco',
|
||||
state: 'CA',
|
||||
country: 'USA'
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function addFriend(userObj, friend) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
console.log(addFriend(user, 'Pete'));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 587d7b7c367417b2b2512b19
|
||||
title: Modify an Object Nested Within an Object
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 修改嵌套在对象中的对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在让我们来看一个稍微复杂的对象。对象属性可以嵌套到任意深度,它们的值可以是JavaScript支持的任何类型的数据,包括数组甚至其他对象。考虑以下: <blockquote>让nestedObject = { <br> id:28802695164, <br>日期:'2016年12月31日', <br>数据:{ <br>总用户:99, <br>在线:80, <br> onlineStatus:{ <br>活跃:67, <br>离开:13 <br> } <br> } <br> }; </blockquote> <code>nestedObject</code>有三个唯一的键: <code>id</code> ,其值为数字, <code>date</code>为字符串的<code>data</code> , <code>data</code> ,其值为另一个嵌套在其中的对象。虽然结构很快就会变得复杂,但我们仍然可以使用相同的符号来访问我们需要的信息。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在这里,我们定义了一个对象<code>userActivity</code> ,其中包含嵌套在其中的另一个对象。您可以像修改上一个挑战中的属性一样修改此嵌套对象的属性。将<code>online</code>密钥的值设置为<code>45</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>userActivity</code>具有<code>id</code> , <code>date</code>和<code>data</code>属性
|
||||
testString: 'assert("id" in userActivity && "date" in userActivity && "data" in userActivity, "<code>userActivity</code> has <code>id</code>, <code>date</code> and <code>data</code> properties");'
|
||||
- text: <code>userActivity</code>具有设置为具有密钥<code>totalUsers</code>和<code>online</code>的对象的<code>data</code>密钥
|
||||
testString: 'assert("totalUsers" in userActivity.data && "online" in userActivity.data, "<code>userActivity</code> has a <code>data</code> key set to an object with keys <code>totalUsers</code> and <code>online</code>");'
|
||||
- text: 嵌套在<code>userActivity</code> <code>data</code>键中的<code>online</code>属性应设置为<code>45</code>
|
||||
testString: 'assert(userActivity.data.online === 45, "The <code>online</code> property nested in the <code>data</code> key of <code>userActivity</code> should be set to <code>45</code>");'
|
||||
- text: <code>online</code>属性使用点或括号表示法设置
|
||||
testString: 'assert.strictEqual(code.search(/online: 45/), -1, "The <code>online</code> property is set using dot or bracket notation");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let userActivity = {
|
||||
id: 23894201352,
|
||||
date: 'January 1, 2017',
|
||||
data: {
|
||||
totalUsers: 51,
|
||||
online: 42
|
||||
}
|
||||
};
|
||||
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
|
||||
console.log(userActivity);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,60 @@
|
||||
---
|
||||
id: 587d78b2367417b2b2512b0f
|
||||
title: Remove Items from an Array with pop() and shift()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用pop()和shift()从数组中删除项
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>push()</code>和<code>unshift()</code>都有相应的几乎相反的方法: <code>pop()</code>和<code>shift()</code> 。正如您现在可能已经猜到的那样, <code>pop()</code>不是添加,而是从数组的末尾<em>删除</em>元素,而<code>shift()</code>从头开始删除元素。 <code>pop()</code>和<code>shift()</code>及其兄弟<code>push()</code>和<code>unshift()</code>之间的关键区别在于,两个方法都不接受参数,并且每个方法只允许一次由单个元素修改数组。让我们来看看: <blockquote>让问候= ['什么事情?','你好','看到你!']; <br><br> greetings.pop(); <br> //现在等于['whats up?','hello'] <br><br> greetings.shift(); <br> //现在等于['你好'] </blockquote>我们还可以使用以下任一方法返回已删除元素的值: <blockquote> let popped = greetings.pop(); <br> //返回'你好' <br> //问候现在等于[] </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们定义了一个函数<code>popShift</code> ,它将一个数组作为参数并返回一个新数组。使用<code>pop()</code>和<code>shift()</code>修改函数,删除参数数组的第一个和最后一个元素,并将删除的元素分配给它们对应的变量,以便返回的数组包含它们的值。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>popShift(["challenge", "is", "not", "complete"])</code>应返回<code>["challenge", "complete"]</code>'
|
||||
testString: 'assert.deepEqual(popShift(["challenge", "is", "not", "complete"]), ["challenge", "complete"], "<code>popShift(["challenge", "is", "not", "complete"])</code> should return <code>["challenge", "complete"]</code>");'
|
||||
- text: <code>popShift</code>函数应该使用<code>pop()</code>方法
|
||||
testString: 'assert.notStrictEqual(popShift.toString().search(/\.pop\(/), -1, "The <code>popShift</code> function should utilize the <code>pop()</code> method");'
|
||||
- text: <code>popShift</code>函数应该使用<code>shift()</code>方法
|
||||
testString: 'assert.notStrictEqual(popShift.toString().search(/\.shift\(/), -1, "The <code>popShift</code> function should utilize the <code>shift()</code> method");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function popShift(arr) {
|
||||
let popped; // change this line
|
||||
let shifted; // change this line
|
||||
return [shifted, popped];
|
||||
}
|
||||
|
||||
// do not change code below this line
|
||||
console.log(popShift(['challenge', 'is', 'not', 'complete']));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,59 @@
|
||||
---
|
||||
id: 587d78b2367417b2b2512b10
|
||||
title: Remove Items Using splice()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用splice()删除项目
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">好的,所以我们已经学会了如何使用<code>shift()</code>和<code>pop()</code>从数组的开头和结尾删除元素,但是如果我们想要从中间某处删除元素呢?或者一次删除多个元素?好吧,这就是<code>splice()</code>用武之地<code>splice()</code>允许我们这样做:从数组中的任何位置<strong>删除任意数量的连续元素</strong> 。 <code>splice()</code>可能需要长达3个参数,但现在,我们将重点放在刚第一2.第2个参数<code>splice()</code>是其代表索引的整数,或位置中,阵列的该<code>splice()</code>是为呼吁。请记住,数组是<em>零索引的</em> ,所以为了表示数组的第一个元素,我们将使用<code>0</code> 。 <code>splice()</code>的第一个参数表示从中开始删除元素的数组的索引,而第二个参数表示要删除的元素的数量。例如: <blockquote>让array = ['今天','是','不','所以','伟大']; <br><br> array.splice(2,2); <br> //删除以第3个元素开头的2个元素<br> //数组现在等于['今天','是','很棒'] </blockquote> <code>splice()</code>不仅修改了它被调用的数组,而且还返回一个包含被删除元素值的新数组: <blockquote>让array = ['我','我','感觉','真的','快乐']; <br><br> let newArray = array.splice(3,2); <br> // newArray等于['真'','快乐'] </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们定义了一个函数<code>sumOfTen</code> ,它将一个数组作为参数,并返回该数组元素的总和。使用<code>splice()</code>修改函数,使其返回值<code>10</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>sumOfTen</code>应该返回10
|
||||
testString: 'assert.strictEqual(sumOfTen([2, 5, 1, 5, 2, 1]), 10, "<code>sumOfTen</code> should return 10");'
|
||||
- text: <code>sumOfTen</code>函数应该使用<code>splice()</code>方法
|
||||
testString: 'assert.notStrictEqual(sumOfTen.toString().search(/\.splice\(/), -1, "The <code>sumOfTen</code> function should utilize the <code>splice()</code> method");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function sumOfTen(arr) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
return arr.reduce((a, b) => a + b);
|
||||
}
|
||||
|
||||
// do not change code below this line
|
||||
console.log(sumOfTen([2, 5, 1, 5, 2, 1]));
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,57 @@
|
||||
---
|
||||
id: 587d7b7e367417b2b2512b20
|
||||
title: Use an Array to Store a Collection of Data
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用数组存储数据集合
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">以下是阵列数据结构最简单实现的示例。这被称为<dfn>一维数组</dfn> ,意味着它只有一个级别,或者它没有嵌套在其中的任何其他数组。请注意,它包含<dfn>布尔值</dfn> , <dfn>字符串</dfn>和<dfn>数字</dfn> ,以及其他有效的JavaScript数据类型: <blockquote> let simpleArray = ['one',2,'three',true,false,undefined,null]; <br>的console.log(simpleArray.length); <br> //记录7 </blockquote>所有数组都有一个length属性,如上所示,可以使用语法<code>Array.length</code>轻松访问<code>Array.length</code> 。下面可以看到更复杂的数组实现。这称为<dfn>多维数组</dfn> ,或包含其他数组的数组。请注意,此数组还包含JavaScript <dfn>对象</dfn> ,我们将在下一节中详细介绍,但是现在,您需要知道的是,数组也能够存储复杂对象。 <blockquote>让complexArray = [ <br> [ <br> { <br>一:1, <br>二:2 <br> }, <br> { <br>三:3, <br>四:4 <br> } <br> ] <br> [ <br> { <br> a:“a”, <br> b:“b” <br> }, <br> { <br> c:“c”, <br> d:“d” <br> } <br> ] <br> ]。 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">我们定义了一个名为<code>yourArray</code>的变量。通过为<code>yourArray</code>变量指定长度至少为5个元素的数组来完成该语句。您的数组应至少包含一个<dfn>字符串</dfn> ,一个<dfn>数字</dfn>和一个<dfn>布尔值</dfn> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: yourArray是一个数组
|
||||
testString: 'assert.strictEqual(Array.isArray(yourArray), true, "yourArray is an array");'
|
||||
- text: <code>yourArray</code>至少有5个元素
|
||||
testString: 'assert.isAtLeast(yourArray.length, 5, "<code>yourArray</code> is at least 5 elements long");'
|
||||
- text: <code>yourArray</code>至少包含一个<code>boolean</code>
|
||||
testString: 'assert(yourArray.filter( el => typeof el === "boolean").length >= 1, "<code>yourArray</code> contains at least one <code>boolean</code>");'
|
||||
- text: <code>yourArray</code>至少包含一个<code>number</code>
|
||||
testString: 'assert(yourArray.filter( el => typeof el === "number").length >= 1, "<code>yourArray</code> contains at least one <code>number</code>");'
|
||||
- text: <code>yourArray</code>至少包含一个<code>string</code>
|
||||
testString: 'assert(yourArray.filter( el => typeof el === "string").length >= 1, "<code>yourArray</code> contains at least one <code>string</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let yourArray; // change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 587d7b7c367417b2b2512b1b
|
||||
title: Use the delete Keyword to Remove Object Properties
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用删除关键字删除对象属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在你知道了什么是对象及其基本特征和优点。简而言之,它们是键值存储,它提供了一种灵活,直观的数据结构方式, <strong><em>并且</em></strong>它们提供了非常快速的查找时间。在其余的挑战中,我们将描述您可以对对象执行的几个常见操作,以便您可以轻松地在程序中应用这些有用的数据结构。在早期的挑战中,我们都添加并修改了对象的键值对。在这里,我们将看到如何从对象中<em>删除</em>键值对。让我们最后一次重新审视我们的<code>foods</code>对象示例。如果我们想删除<code>apples</code>键,我们可以使用<code>delete</code>关键字删除它,如下所示: <blockquote>删除foods.apples; </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用delete关键字从<code>foods</code>对象中删除<code>oranges</code> , <code>plums</code>和<code>strawberries</code>键。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>foods</code>对象只有三个键: <code>apples</code> , <code>grapes</code>和<code>bananas</code>
|
||||
testString: 'assert(!foods.hasOwnProperty("oranges") && !foods.hasOwnProperty("plums") && !foods.hasOwnProperty("strawberries") && Object.keys(foods).length === 3, "The <code>foods</code> object only has three keys: <code>apples</code>, <code>grapes</code>, and <code>bananas</code>");'
|
||||
- text: 使用<code>delete</code> <code>oranges</code> , <code>plums</code>和<code>strawberries</code>键
|
||||
testString: 'assert(code.search(/oranges:/) !== -1 && code.search(/plums:/) !== -1 && code.search(/strawberries:/) !== -1, "The <code>oranges</code>, <code>plums</code>, and <code>strawberries</code> keys are removed using <code>delete</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let foods = {
|
||||
apples: 25,
|
||||
oranges: 32,
|
||||
plums: 28,
|
||||
bananas: 13,
|
||||
grapes: 35,
|
||||
strawberries: 27
|
||||
};
|
||||
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
|
||||
console.log(foods);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,67 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392ca
|
||||
title: Access Array Data with Indexes
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/access-array-data-with-indexes'
|
||||
videoUrl: ''
|
||||
localeTitle: 使用索引访问数组数据
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们可以使用<code>indexes</code>访问数组内部的数据。数组索引使用字符串使用的相同括号表示法编写,但不是指定字符,而是指定数组中的条目。与字符串一样,数组使用<dfn>从零开始的</dfn>索引,因此数组中的第一个元素是元素<code>0</code> 。 <strong>例</strong> <blockquote> var array = [50,60,70]; <br>阵列[0]; //等于50 <br> var data = array [1]; //等于60 </blockquote> <strong>注意</strong> <br>数组名称和方括号之间不应有任何空格,如<code>array [0]</code> 。尽管JavaScript能够正确处理,但这可能会让其他程序员在阅读代码时感到困惑。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个名为<code>myData</code>的变量,并使用括号表示法将其设置为等于<code>myArray</code>的第一个值。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 变量<code>myData</code>应该等于<code>myArray</code>的第一个值。
|
||||
testString: 'assert((function(){if(typeof myArray !== "undefined" && typeof myData !== "undefined" && myArray[0] === myData){return true;}else{return false;}})(), "The variable <code>myData</code> should equal the first value of <code>myArray</code>.");'
|
||||
- text: 应使用括号表示法访问变量<code>myArray</code>的数据。
|
||||
testString: 'assert((function(){if(code.match(/\s*=\s*myArray\[0\]/g)){return true;}else{return false;}})(), "The data in variable <code>myArray</code> should be accessed using bracket notation.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [50,60,70];
|
||||
var ourData = ourArray[0]; // equals 50
|
||||
|
||||
// Setup
|
||||
var myArray = [50,60,70];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 56592a60ddddeae28f7aa8e1
|
||||
title: Access Multi-Dimensional Arrays With Indexes
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/access-array-data-with-indexes'
|
||||
videoUrl: ''
|
||||
localeTitle: 访问带索引的多维数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">考虑<dfn>多维</dfn>数组的一种方法是作为<em>数组的数组</em> 。使用括号访问数组时,第一组括号引用最外层(第一级)数组中的条目,另外一对括号引用内部的下一级条目。 <strong>例</strong> <blockquote> var arr = [ <br> [1,2,3], <br> [4,5,6] <br> [7,8,9] <br> [[10,11,12],13,14] <br> ]。 <br> ARR [3]; //等于[[10,11,12],13,14] <br> ARR [3] [0]; //等于[10,11,12] <br> ARR [3] [0] [1]; //等于11 </blockquote> <strong>注意</strong> <br>数组名称和方括号之间不应该有任何空格,如<code>array [0][0]</code> ,甚至不允许使用此<code>array [0] [0]</code> 。尽管JavaScript能够正确处理,但这可能会让其他程序员在阅读代码时感到困惑。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用括号表示法从<code>myArray</code>选择一个元素,使<code>myData</code>等于<code>8</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myData</code>应该等于<code>8</code> 。
|
||||
testString: 'assert(myData === 8, "<code>myData</code> should be equal to <code>8</code>.");'
|
||||
- text: 您应该使用括号表示法从<code>myArray</code>读取正确的值。
|
||||
testString: 'assert(/myArray\[2\]\[1\]/g.test(code) && !/myData\s*=\s*(?:.*[-+*/%]|\d)/g.test(code), "You should be using bracket notation to read the correct value from <code>myArray</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];
|
||||
|
||||
// Only change code below this line.
|
||||
var myData = myArray[0][0];
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244cd
|
||||
title: Accessing Nested Arrays
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/access-array-data-with-indexes'
|
||||
videoUrl: ''
|
||||
localeTitle: 访问嵌套数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">正如我们在前面的示例中所看到的,对象可以包含嵌套对象和嵌套数组。与访问嵌套对象类似,可以链接数组括号表示法来访问嵌套数组。以下是如何访问嵌套数组的示例: <blockquote> var ourPets = [ <br> { <br> animalType:“猫”, <br>名称:[ <br> “Meowzer” <br> “蓬松”, <br> “洁猫” <br> ] <br> }, <br> { <br>动物类型:“狗”, <br>名称:[ <br> “点”, <br> “库巴” <br> “羊羊” <br> ] <br> } <br> ]。 <br> ourPets [0] .names [1]; //“蓬松” <br> ourPets [1] .names [0]; //“Spot” </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用对象点和数组括号表示法从变量<code>myPlants</code>检索第二个树。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>secondTree</code>应该等于“松树”
|
||||
testString: 'assert(secondTree === "pine", "<code>secondTree</code> should equal "pine"");'
|
||||
- text: 使用点和括号表示法访问<code>myPlants</code>
|
||||
testString: 'assert(/=\s*myPlants\[1\].list\[1\]/.test(code), "Use dot and bracket notation to access <code>myPlants</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myPlants = [
|
||||
{
|
||||
type: "flowers",
|
||||
list: [
|
||||
"rose",
|
||||
"tulip",
|
||||
"dandelion"
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "trees",
|
||||
list: [
|
||||
"fir",
|
||||
"pine",
|
||||
"birch"
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var secondTree = ""; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244cc
|
||||
title: Accessing Nested Objects
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/accessing-nested-objects-in-json'
|
||||
videoUrl: ''
|
||||
localeTitle: 访问嵌套对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">可以通过将点或括号表示法链接在一起来访问对象的子属性。这是一个嵌套对象: <blockquote> var ourStorage = { <br> “桌子”:{ <br> “抽屉”:“订书机” <br> }, <br> “内阁”:{ <br> “顶级抽屉”:{ <br> “folder1”:“一个文件”, <br> “folder2”:“秘密” <br> }, <br> “底部抽屉”:“苏打水” <br> } <br> }; <br> ourStorage.cabinet [“top drawer”]。folder2; //“秘密” <br> ourStorage.desk.drawer; //“订书机” </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">访问<code>myStorage</code>对象并将<code>glove box</code>属性的内容分配给<code>gloveBoxContents</code>变量。对于名称中包含空格的属性,请使用括号表示法。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>gloveBoxContents</code>应该等于“地图”
|
||||
testString: 'assert(gloveBoxContents === "maps", "<code>gloveBoxContents</code> should equal "maps"");'
|
||||
- text: 使用点和括号表示法访问<code>myStorage</code>
|
||||
testString: 'assert(/=\s*myStorage\.car\.inside\[\s*("|")glove box\1\s*\]/g.test(code), "Use dot and bracket notation to access <code>myStorage</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myStorage = {
|
||||
"car": {
|
||||
"inside": {
|
||||
"glove box": "maps",
|
||||
"passenger seat": "crumbs"
|
||||
},
|
||||
"outside": {
|
||||
"trunk": "jack"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var gloveBoxContents = undefined; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c8
|
||||
title: Accessing Object Properties with Bracket Notation
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/accessing-objects-properties-with-bracket-notation'
|
||||
videoUrl: ''
|
||||
localeTitle: 使用括号表示法访问对象属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">访问对象属性的第二种方法是括号表示法( <code>[]</code> )。如果您尝试访问的对象的属性在其名称中有空格,则需要使用括号表示法。但是,您仍然可以在没有空格的对象属性上使用括号表示法。以下是使用括号表示法读取对象属性的示例: <blockquote> var myObj = { <br> “太空名称”:“柯克”, <br> “更多空间”:“Spock”, <br> “NoSpace”:“USS Enterprise” <br> }; <br> myObj [“空间名称”]; //柯克<br> myObj ['更多空间']; // Spock <br> MyObj中[ “无空间”]; // USS Enterprise </blockquote>请注意,其中包含空格的属性名称必须使用引号(单引号或双引号)。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用括号表示法<code>testObj</code>属性<code>"an entree"</code> <code>testObj</code> <code>"an entree"</code>和<code>"the drink"</code>的<code>testObj</code> ,并分别将它们分配给<code>entreeValue</code>和<code>drinkValue</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>entreeValue</code>应该是一个字符串
|
||||
testString: 'assert(typeof entreeValue === "string" , "<code>entreeValue</code> should be a string");'
|
||||
- text: <code>entreeValue</code>的值应该是<code>"hamburger"</code>
|
||||
testString: 'assert(entreeValue === "hamburger" , "The value of <code>entreeValue</code> should be <code>"hamburger"</code>");'
|
||||
- text: <code>drinkValue</code>应该是一个字符串
|
||||
testString: 'assert(typeof drinkValue === "string" , "<code>drinkValue</code> should be a string");'
|
||||
- text: <code>drinkValue</code>的值应该是<code>"water"</code>
|
||||
testString: 'assert(drinkValue === "water" , "The value of <code>drinkValue</code> should be <code>"water"</code>");'
|
||||
- text: 您应该使用括号表示法两次
|
||||
testString: 'assert(code.match(/testObj\s*?\[("|")[^""]+\1\]/g).length > 1, "You should use bracket notation twice");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var testObj = {
|
||||
"an entree": "hamburger",
|
||||
"my side": "veggies",
|
||||
"the drink": "water"
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var entreeValue = testObj; // Change this line
|
||||
var drinkValue = testObj; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c7
|
||||
title: Accessing Object Properties with Dot Notation
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用点表示法访问对象属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有两种方法可以访问对象的属性:点表示法( <code>.</code> )和括号表示法( <code>[]</code> ),类似于数组。当您知道要提前访问的属性的名称时,使用点符号。以下是使用点表示法( <code>.</code> )读取对象属性的示例: <blockquote> var myObj = { <br> prop1:“val1”, <br> prop2:“val2” <br> }; <br> var prop1val = myObj.prop1; // val1 <br> var prop2val = myObj.prop2; // val2 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用点表示法读入<code>testObj</code>的属性值。将变量<code>hatValue</code>设置为等于对象的属性<code>hat</code> ,并将变量<code>shirtValue</code>设置为等于对象的属性<code>shirt</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>hatValue</code>应该是一个字符串
|
||||
testString: 'assert(typeof hatValue === "string" , "<code>hatValue</code> should be a string");'
|
||||
- text: <code>hatValue</code>的值应该是<code>"ballcap"</code>
|
||||
testString: 'assert(hatValue === "ballcap" , "The value of <code>hatValue</code> should be <code>"ballcap"</code>");'
|
||||
- text: <code>shirtValue</code>应该是一个字符串
|
||||
testString: 'assert(typeof shirtValue === "string" , "<code>shirtValue</code> should be a string");'
|
||||
- text: <code>shirtValue</code>的值应该是<code>"jersey"</code>
|
||||
testString: 'assert(shirtValue === "jersey" , "The value of <code>shirtValue</code> should be <code>"jersey"</code>");'
|
||||
- text: 你应该使用点符号两次
|
||||
testString: 'assert(code.match(/testObj\.\w+/g).length > 1, "You should use dot notation twice");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var testObj = {
|
||||
"hat": "ballcap",
|
||||
"shirt": "jersey",
|
||||
"shoes": "cleats"
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var hatValue = testObj; // Change this line
|
||||
var shirtValue = testObj; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c9
|
||||
title: Accessing Object Properties with Variables
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/accessing-objects-properties-with-variables'
|
||||
videoUrl: ''
|
||||
localeTitle: 使用变量访问对象属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">对象的括号表示法的另一个用途是访问存储为变量值的属性。这对于迭代对象的属性或访问查找表非常有用。以下是使用变量访问属性的示例: <blockquote> var dogs = { <br> Fido:“Mutt”,Hunter:“Doberman”,Snoopie:“Beagle” <br> }; <br> var myDog =“猎人”; <br> var myBreed = dogs [myDog]; <br>的console.log(myBreed); //“杜宾犬” </blockquote>另一种可以使用此概念的方法是在程序执行期间动态收集属性的名称,如下所示: <blockquote> var someObj = { <br> propName:“约翰” <br> }; <br> function propPrefix(str){ <br> var s =“prop”; <br> return s + str; <br> } <br> var someProp = propPrefix(“Name”); // someProp现在保存值'propName' <br>的console.log(someObj中[someProp]); // “约翰” </blockquote>请注意,在使用变量名来访问属性时,我们<em>不会</em>使用引号,因为我们使用的是变量的<em>值</em> ,而不是<em>名称</em> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>playerNumber</code>变量使用括号表示法在<code>testObj</code>查找玩家<code>16</code> 。然后将该名称分配给<code>player</code>变量。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>playerNumber</code>应该是一个数字
|
||||
testString: 'assert(typeof playerNumber === "number", "<code>playerNumber</code> should be a number");'
|
||||
- text: 变量<code>player</code>应该是一个字符串
|
||||
testString: 'assert(typeof player === "string", "The variable <code>player</code> should be a string");'
|
||||
- text: <code>player</code>的价值应该是“蒙大拿”
|
||||
testString: 'assert(player === "Montana", "The value of <code>player</code> should be "Montana"");'
|
||||
- text: 您应该使用括号表示法来访问<code>testObj</code>
|
||||
testString: 'assert(/testObj\s*?\[.*?\]/.test(code),"You should use bracket notation to access <code>testObj</code>");'
|
||||
- text: 您不应该直接将值<code>Montana</code>分配给变量<code>player</code> 。
|
||||
testString: 'assert(!code.match(/player\s*=\s*"|\"\s*Montana\s*"|\"\s*;/gi),"You should not assign the value <code>Montana</code> to the variable <code>player</code> directly.");'
|
||||
- text: 您应该在括号表示法中使用变量<code>playerNumber</code>
|
||||
testString: 'assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code),"You should be using the variable <code>playerNumber</code> in your bracket notation");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var testObj = {
|
||||
12: "Namath",
|
||||
16: "Montana",
|
||||
19: "Unitas"
|
||||
};
|
||||
|
||||
// Only change code below this line;
|
||||
|
||||
var playerNumber; // Change this Line
|
||||
var player = testObj; // Change this Line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d2
|
||||
title: Add New Properties to a JavaScript Object
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 将新属性添加到JavaScript对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以像修改现有JavaScript对象一样向现有JavaScript对象添加新属性。以下是我们如何为<code>ourDog</code>添加<code>"bark"</code>属性: <code>ourDog.bark = "bow-wow";</code>或者我们的<code>ourDog["bark"] = "bow-wow";</code>现在当我们评估我们的<code>ourDog.bark</code> ,我们会得到他的吠声,“低头哇”。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">向<code>myDog</code>添加<code>"bark"</code>属性并将其设置为狗声,例如“woof”。您可以使用点或括号表示法。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 将属性<code>"bark"</code>添加到<code>myDog</code> 。
|
||||
testString: 'assert(myDog.bark !== undefined, "Add the property <code>"bark"</code> to <code>myDog</code>.");'
|
||||
- text: 不要在设置部分添加<code>"bark"</code>
|
||||
testString: 'assert(!/bark[^\n]:/.test(code), "Do not add <code>"bark"</code> to the setup section");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"]
|
||||
};
|
||||
|
||||
ourDog.bark = "bow-wow";
|
||||
|
||||
// Setup
|
||||
var myDog = {
|
||||
"name": "Happy Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"]
|
||||
};
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,59 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb3bdef
|
||||
title: Add Two Numbers with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript添加两个数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>Number</code>是JavaScript中的数据类型,表示数字数据。现在让我们尝试使用JavaScript添加两个数字。当放置在两个数字之间时,JavaScript使用<code>+</code>符号作为加法运算。 <strong>例</strong> <blockquote> myVar = 5 + 10; //分配15 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改<code>0</code>使总和等于<code>20</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>sum</code>应该等于<code>20</code>
|
||||
testString: 'assert(sum === 20, "<code>sum</code> should equal <code>20</code>");'
|
||||
- text: 使用<code>+</code>运算符
|
||||
testString: 'assert(/\+/.test(code), "Use the <code>+</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var sum = 10 + 0;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244de
|
||||
title: Adding a Default Option in Switch Statements
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/adding-a-default-option-in-switch-statements'
|
||||
videoUrl: ''
|
||||
localeTitle: 在交换机语句中添加默认选项
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在<code>switch</code>语句中,您可能无法将所有可能的值指定为<code>case</code>语句。相反,您可以添加<code>default</code>语句,如果找不到匹配的<code>case</code>语句,将执行该语句。可以把它想象成<code>if/else</code>链中的最后一个<code>else</code>语句。 <code>default</code>语句应该是最后一种情况。 <blockquote> switch(num){ <br>案例值1: <br>语句1; <br>打破; <br>案例值2: <br>语句2; <br>打破; <br> ... <br>默认: <br> defaultStatement; <br>打破; <br> } </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">写一个switch语句来设置以下条件的<code>answer</code> : <br> <code>"a"</code> - “苹果” <br> <code>"b"</code> - “鸟” <br> <code>"c"</code> - “猫” <br> <code>default</code> - “东西” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>switchOfStuff("a")</code>的值应为“apple”
|
||||
testString: 'assert(switchOfStuff("a") === "apple", "<code>switchOfStuff("a")</code> should have a value of "apple"");'
|
||||
- text: <code>switchOfStuff("b")</code>的值应为“bird”
|
||||
testString: 'assert(switchOfStuff("b") === "bird", "<code>switchOfStuff("b")</code> should have a value of "bird"");'
|
||||
- text: <code>switchOfStuff("c")</code>的值应为“cat”
|
||||
testString: 'assert(switchOfStuff("c") === "cat", "<code>switchOfStuff("c")</code> should have a value of "cat"");'
|
||||
- text: <code>switchOfStuff("d")</code>的值应为“stuff”
|
||||
testString: 'assert(switchOfStuff("d") === "stuff", "<code>switchOfStuff("d")</code> should have a value of "stuff"");'
|
||||
- text: <code>switchOfStuff(4)</code>的值应为“stuff”
|
||||
testString: 'assert(switchOfStuff(4) === "stuff", "<code>switchOfStuff(4)</code> should have a value of "stuff"");'
|
||||
- text: 您不应该使用任何<code>if</code>或<code>else</code>语句
|
||||
testString: 'assert(!/else/g.test(code) || !/if/g.test(code), "You should not use any <code>if</code> or <code>else</code> statements");'
|
||||
- text: 您应该使用<code>default</code>语句
|
||||
testString: 'assert(switchOfStuff("string-to-trigger-default-case") === "stuff", "You should use a <code>default</code> statement");'
|
||||
- text: 你应该至少有3个<code>break</code>语句
|
||||
testString: 'assert(code.match(/break/g).length > 2, "You should have at least 3 <code>break</code> statements");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function switchOfStuff(val) {
|
||||
var answer = "";
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
return answer;
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
switchOfStuff(1);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ed
|
||||
title: Appending Variables to Strings
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/appending-variables-to-strings'
|
||||
videoUrl: ''
|
||||
localeTitle: 将变量附加到字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">正如我们可以在字符串<dfn>文字中</dfn>构建多行的字符串一样,我们也可以使用加号等于( <code>+=</code> )运算符将变量附加到字符串。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">设置<code>someAdjective</code>并使用<code>+=</code>运算符将其附加到<code>myStr</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>someAdjective</code>应设置为至少3个字符长的字符串
|
||||
testString: 'assert(typeof someAdjective !== "undefined" && someAdjective.length > 2, "<code>someAdjective</code> should be set to a string at least 3 characters long");'
|
||||
- text: 使用<code>+=</code>运算符将<code>someAdjective</code>附加到<code>myStr</code>
|
||||
testString: 'assert(code.match(/myStr\s*\+=\s*someAdjective\s*/).length > 0, "Append <code>someAdjective</code> to <code>myStr</code> using the <code>+=</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var anAdjective = "awesome!";
|
||||
var ourStr = "freeCodeCamp is ";
|
||||
ourStr += anAdjective;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var someAdjective;
|
||||
var myStr = "Learning to code is ";
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c3
|
||||
title: Assignment with a Returned Value
|
||||
challengeType: 1
|
||||
guideUrl: 'https://chinese.freecodecamp.org/guide/certificates/assignment-with-a-returned-value'
|
||||
videoUrl: ''
|
||||
localeTitle: 具有返回值的分配
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果您从我们对<a href="javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator" target="_blank">使用赋值运算符存储值</a>的讨论中回忆起来<a href="javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator" target="_blank">,则在分配</a>值之前,将解决等号右侧的所有内容。这意味着我们可以获取函数的返回值并将其赋值给变量。假设我们预先定义了一个函数<code>sum</code> ,它将两个数字相加,然后: <code>ourSum = sum(5, 12);</code>将调用<code>sum</code>函数,它返回值<code>17</code>并将其分配给<code>ourSum</code>变量。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用参数<code>7</code>调用<code>processArg</code>函数,并将其返回值分配给已<code>processed</code>的变量。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>processed</code>的值应为<code>2</code>
|
||||
testString: 'assert(processed === 2, "<code>processed</code> should have a value of <code>2</code>");'
|
||||
- text: 您应该将<code>processArg</code>分配给已<code>processed</code>
|
||||
testString: 'assert(/processed\s*=\s*processArg\(\s*7\s*\)\s*;/.test(code), "You should assign <code>processArg</code> to <code>processed</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var changed = 0;
|
||||
|
||||
function change(num) {
|
||||
return (num + 5) / 3;
|
||||
}
|
||||
|
||||
changed = change(10);
|
||||
|
||||
// Setup
|
||||
var processed = 0;
|
||||
|
||||
function processArg(num) {
|
||||
return (num + 3) / 5;
|
||||
}
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d0
|
||||
title: Build JavaScript Objects
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 构建JavaScript对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您之前可能听说过<code>object</code>这个术语。对象类似于<code>arrays</code> ,除了不使用索引访问和修改数据,您可以通过所谓的<code>properties</code>访问对象中的数据。对象对于以结构化方式存储数据很有用,并且可以表示真实世界对象,如猫。这是一个示例cat对象: <blockquote> var cat = { <br> “名字”:“胡须”, <br> “腿”:4, <br> “尾巴”:1, <br> “敌人”:[“水”,“狗”] <br> }; </blockquote>在此示例中,所有属性都存储为字符串,例如 - <code>"name"</code> , <code>"legs"</code>和<code>"tails"</code> 。但是,您也可以使用数字作为属性。您甚至可以省略单字符串属性的引号,如下所示: <blockquote> var anotherObject = { <br>制作:“福特”, <br> 5:“五”, <br> “模特”:“焦点” <br> }; </blockquote>但是,如果您的对象具有任何非字符串属性,JavaScript将自动将它们作为字符串进行类型转换。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个代表名为<code>myDog</code>的狗的对象,其中包含属性<code>"name"</code> (字符串), <code>"legs"</code> , <code>"tails"</code>和<code>"friends"</code> 。您可以将这些对象属性设置为您想要的任何值,因为<code>"name"</code>是一个字符串, <code>"legs"</code>和<code>"tails"</code>是数字, <code>"friends"</code>是一个数组。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myDog</code>应该包含属性<code>name</code> ,它应该是一个<code>string</code> 。
|
||||
testString: 'assert((function(z){if(z.hasOwnProperty("name") && z.name !== undefined && typeof z.name === "string"){return true;}else{return false;}})(myDog), "<code>myDog</code> should contain the property <code>name</code> and it should be a <code>string</code>.");'
|
||||
- text: <code>myDog</code>应该包含属性<code>legs</code> ,它应该是一个<code>number</code> 。
|
||||
testString: 'assert((function(z){if(z.hasOwnProperty("legs") && z.legs !== undefined && typeof z.legs === "number"){return true;}else{return false;}})(myDog), "<code>myDog</code> should contain the property <code>legs</code> and it should be a <code>number</code>.");'
|
||||
- text: <code>myDog</code>应该包含属性<code>tails</code> ,它应该是一个<code>number</code> 。
|
||||
testString: 'assert((function(z){if(z.hasOwnProperty("tails") && z.tails !== undefined && typeof z.tails === "number"){return true;}else{return false;}})(myDog), "<code>myDog</code> should contain the property <code>tails</code> and it should be a <code>number</code>.");'
|
||||
- text: <code>myDog</code>应该包含属性<code>friends</code> ,它应该是一个<code>array</code> 。
|
||||
testString: 'assert((function(z){if(z.hasOwnProperty("friends") && z.friends !== undefined && Array.isArray(z.friends)){return true;}else{return false;}})(myDog), "<code>myDog</code> should contain the property <code>friends</code> and it should be an <code>array</code>.");'
|
||||
- text: <code>myDog</code>应该只包含所有给定的属性。
|
||||
testString: 'assert((function(z){return Object.keys(z).length === 4;})(myDog), "<code>myDog</code> should only contain all the given properties.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"]
|
||||
};
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
var myDog = {
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244dc
|
||||
title: Chaining If Else Statements
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 链接如果其他声明
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>if/else</code>语句可以链接在一起以用于复杂的逻辑。这是多个链式<code>if</code> / <code>else if</code>语句的<dfn>伪代码</dfn> : <blockquote> if( <em>condition1</em> ){ <br> <em>语句1</em> <br> } else if( <em>condition2</em> ){ <br> <em>语句2</em> <br> } else if( <em>condition3</em> ){ <br> <em>声明3</em> <br> 。 。 。 <br> } else { <br> <em>statementN</em> <br> } </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">写入链接<code>if</code> / <code>else if</code>语句以满足以下条件: <code>num < 5</code> - return“Tiny” <br> <code>num < 10</code> - 返回“Small” <br> <code>num < 15</code> - 返回“中” <br> <code>num < 20</code> - 返回“Large” <br> <code>num >= 20</code> - 返回“巨大” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该至少有四个<code>else</code>语句
|
||||
testString: 'assert(code.match(/else/g).length > 3, "You should have at least four <code>else</code> statements");'
|
||||
- text: 你应该至少有四个<code>if</code>语句
|
||||
testString: 'assert(code.match(/if/g).length > 3, "You should have at least four <code>if</code> statements");'
|
||||
- text: 你应该至少有一个<code>return</code>语句
|
||||
testString: 'assert(code.match(/return/g).length >= 1, "You should have at least one <code>return</code> statement");'
|
||||
- text: <code>testSize(0)</code>应该返回“Tiny”
|
||||
testString: 'assert(testSize(0) === "Tiny", "<code>testSize(0)</code> should return "Tiny"");'
|
||||
- text: <code>testSize(4)</code>应该返回“Tiny”
|
||||
testString: 'assert(testSize(4) === "Tiny", "<code>testSize(4)</code> should return "Tiny"");'
|
||||
- text: <code>testSize(5)</code>应返回“Small”
|
||||
testString: 'assert(testSize(5) === "Small", "<code>testSize(5)</code> should return "Small"");'
|
||||
- text: <code>testSize(8)</code>应该返回“Small”
|
||||
testString: 'assert(testSize(8) === "Small", "<code>testSize(8)</code> should return "Small"");'
|
||||
- text: <code>testSize(10)</code>应该返回“Medium”
|
||||
testString: 'assert(testSize(10) === "Medium", "<code>testSize(10)</code> should return "Medium"");'
|
||||
- text: <code>testSize(14)</code>应返回“Medium”
|
||||
testString: 'assert(testSize(14) === "Medium", "<code>testSize(14)</code> should return "Medium"");'
|
||||
- text: <code>testSize(15)</code>应该返回“Large”
|
||||
testString: 'assert(testSize(15) === "Large", "<code>testSize(15)</code> should return "Large"");'
|
||||
- text: <code>testSize(17)</code>应该返回“Large”
|
||||
testString: 'assert(testSize(17) === "Large", "<code>testSize(17)</code> should return "Large"");'
|
||||
- text: <code>testSize(20)</code>应该返回“巨大”
|
||||
testString: 'assert(testSize(20) === "Huge", "<code>testSize(20)</code> should return "Huge"");'
|
||||
- text: <code>testSize(25)</code>应该返回“巨大”
|
||||
testString: 'assert(testSize(25) === "Huge", "<code>testSize(25)</code> should return "Huge"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testSize(num) {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
return "Change Me";
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testSize(7);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,50 @@
|
||||
---
|
||||
id: bd7123c9c441eddfaeb4bdef
|
||||
title: Comment Your JavaScript Code
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 评论您的JavaScript代码
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">注释是JavaScript有意忽略的代码行。注释是一种很好的方式,可以将注释留给自己和其他人,这些人稍后需要弄清楚代码的作用。在JavaScript中编写注释有两种方法:使用<code>//</code>将告诉JavaScript忽略当前行上的其余文本: <blockquote> //这是一个内嵌评论。 </blockquote>您可以使用<code>/*</code>开头并以<code>*/</code>结尾的多行注释: <blockquote> /* 这是一个<br>多行评论* / </blockquote> <strong>最佳实践</strong> <br>在编写代码时,应定期添加注释以阐明代码部分的功能。良好的评论可以帮助传达您的代码的意图 - 包括他人<em>和</em>未来的自我。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">尝试创建每种评论类型之一。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 创建一个包含至少五个字母的<code>//</code>样式注释。
|
||||
testString: 'assert(code.match(/(\/\/)...../g), "Create a <code>//</code> style comment that contains at least five letters.");'
|
||||
- text: 创建包含至少五个字母的<code>/* */</code>样式注释。
|
||||
testString: 'assert(code.match(/(\/\*)([^\/]{5,})(?=\*\/)/gm), "Create a <code>/* */</code> style comment that contains at least five letters.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d0
|
||||
title: Comparison with the Equality Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与平等算子的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有很多<dfn>比较运算符</dfn>在JavaScript中。所有这些运算符都返回布尔值<code>true</code>或<code>false</code>值。最基本的运算符是等于运算符<code>==</code> 。等于运算符比较两个值,如果它们是等价的则返回<code>true</code>否则返回<code>false</code> 。请注意,相等性与赋值( <code>=</code> )不同,后者将运算符右侧的值分配给左侧的变量。 <blockquote> function equalityTest(myVal){ <br> if(myVal == 10){ <br>返回“平等”; <br> } <br>返回“不等于”; <br> } </blockquote>如果<code>myVal</code>等于<code>10</code> ,则等于运算符返回<code>true</code> ,因此大括号中的代码将执行,函数将返回<code>"Equal"</code> 。否则,该函数将返回<code>"Not Equal"</code> 。为了使JavaScript能够比较两种不同的<code>data types</code> (例如, <code>numbers</code>和<code>strings</code> ),它必须将一种类型转换为另一种类型。这被称为“类型强制”。但是,一旦它完成,它可以比较如下术语: <blockquote> 1 == 1 //是的<br> 1 == 2 //假<br> 1 =='1'//是的<br> “3”== 3 //是的</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>equality operator</code>添加到指示的行,以便当<code>val</code>等于<code>12</code>时,函数将返回“Equal” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testEqual(10)</code>应该返回“Not Equal”
|
||||
testString: 'assert(testEqual(10) === "Not Equal", "<code>testEqual(10)</code> should return "Not Equal"");'
|
||||
- text: <code>testEqual(12)</code>应返回“Equal”
|
||||
testString: 'assert(testEqual(12) === "Equal", "<code>testEqual(12)</code> should return "Equal"");'
|
||||
- text: <code>testEqual("12")</code>应返回“Equal”
|
||||
testString: 'assert(testEqual("12") === "Equal", "<code>testEqual("12")</code> should return "Equal"");'
|
||||
- text: 您应该使用<code>==</code>运算符
|
||||
testString: 'assert(code.match(/==/g) && !code.match(/===/g), "You should use the <code>==</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testEqual(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d4
|
||||
title: Comparison with the Greater Than Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与大于运营商的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">大于运算符( <code>></code> )比较两个数字的值。如果左边的数字大于右边的数字,则返回<code>true</code> 。否则,它返回<code>false</code> 。与等于运算符一样,大于运算符将在比较时转换数据类型的值。 <strong>例子</strong> <blockquote> 5> 3 //是的<br> 7>'3'//是的<br> 2> 3 //假<br> '1'> 9 //假</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>greater than</code>运算符添加到指示的行,以便返回语句有意义。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testGreaterThan(0)</code>应返回“10或Under”
|
||||
testString: 'assert(testGreaterThan(0) === "10 or Under", "<code>testGreaterThan(0)</code> should return "10 or Under"");'
|
||||
- text: <code>testGreaterThan(10)</code>应返回“10或Under”
|
||||
testString: 'assert(testGreaterThan(10) === "10 or Under", "<code>testGreaterThan(10)</code> should return "10 or Under"");'
|
||||
- text: <code>testGreaterThan(11)</code>应该返回“Over 10”
|
||||
testString: 'assert(testGreaterThan(11) === "Over 10", "<code>testGreaterThan(11)</code> should return "Over 10"");'
|
||||
- text: <code>testGreaterThan(99)</code>应该返回“Over 10”
|
||||
testString: 'assert(testGreaterThan(99) === "Over 10", "<code>testGreaterThan(99)</code> should return "Over 10"");'
|
||||
- text: <code>testGreaterThan(100)</code>应该返回“Over 10”
|
||||
testString: 'assert(testGreaterThan(100) === "Over 10", "<code>testGreaterThan(100)</code> should return "Over 10"");'
|
||||
- text: <code>testGreaterThan(101)</code>应返回“超过100”
|
||||
testString: 'assert(testGreaterThan(101) === "Over 100", "<code>testGreaterThan(101)</code> should return "Over 100"");'
|
||||
- text: <code>testGreaterThan(150)</code>应该返回“超过100”
|
||||
testString: 'assert(testGreaterThan(150) === "Over 100", "<code>testGreaterThan(150)</code> should return "Over 100"");'
|
||||
- text: 您应该至少使用<code>></code>运算符两次
|
||||
testString: 'assert(code.match(/val\s*>\s*("|")*\d+("|")*/g).length > 1, "You should use the <code>></code> operator at least twice");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testGreaterThan(val) {
|
||||
if (val) { // Change this line
|
||||
return "Over 100";
|
||||
}
|
||||
|
||||
if (val) { // Change this line
|
||||
return "Over 10";
|
||||
}
|
||||
|
||||
return "10 or Under";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testGreaterThan(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d5
|
||||
title: Comparison with the Greater Than Or Equal To Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与大于或等于运算符的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>greater than or equal to</code>运算符( <code>>=</code> )比较两个数字的值。如果左边的数字大于或等于右边的数字,则返回<code>true</code> 。否则,它返回<code>false</code> 。与等于运算符一样, <code>greater than or equal to</code>运算符将在比较时转换数据类型。 <strong>例子</strong> <blockquote> 6> = 6 //是的<br> 7> ='3'//是的<br> 2> = 3 //假<br> '7'> = 9 //假</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>greater than or equal to</code>运算符添加到指示的行,以便返回语句有意义。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testGreaterOrEqual(0)</code>应返回“小于10”
|
||||
testString: 'assert(testGreaterOrEqual(0) === "Less than 10", "<code>testGreaterOrEqual(0)</code> should return "Less than 10"");'
|
||||
- text: <code>testGreaterOrEqual(9)</code>应返回“小于10”
|
||||
testString: 'assert(testGreaterOrEqual(9) === "Less than 10", "<code>testGreaterOrEqual(9)</code> should return "Less than 10"");'
|
||||
- text: <code>testGreaterOrEqual(10)</code>应返回“10或Over”
|
||||
testString: 'assert(testGreaterOrEqual(10) === "10 or Over", "<code>testGreaterOrEqual(10)</code> should return "10 or Over"");'
|
||||
- text: <code>testGreaterOrEqual(11)</code>应返回“10或Over”
|
||||
testString: 'assert(testGreaterOrEqual(11) === "10 or Over", "<code>testGreaterOrEqual(11)</code> should return "10 or Over"");'
|
||||
- text: <code>testGreaterOrEqual(19)</code>应返回“10或Over”
|
||||
testString: 'assert(testGreaterOrEqual(19) === "10 or Over", "<code>testGreaterOrEqual(19)</code> should return "10 or Over"");'
|
||||
- text: <code>testGreaterOrEqual(100)</code>应该返回“20或Over”
|
||||
testString: 'assert(testGreaterOrEqual(100) === "20 or Over", "<code>testGreaterOrEqual(100)</code> should return "20 or Over"");'
|
||||
- text: <code>testGreaterOrEqual(21)</code>应返回“20或Over”
|
||||
testString: 'assert(testGreaterOrEqual(21) === "20 or Over", "<code>testGreaterOrEqual(21)</code> should return "20 or Over"");'
|
||||
- text: 您应该使用<code>>=</code>运算符至少两次
|
||||
testString: 'assert(code.match(/val\s*>=\s*("|")*\d+("|")*/g).length > 1, "You should use the <code>>=</code> operator at least twice");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testGreaterOrEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "20 or Over";
|
||||
}
|
||||
|
||||
if (val) { // Change this line
|
||||
return "10 or Over";
|
||||
}
|
||||
|
||||
return "Less than 10";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testGreaterOrEqual(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d2
|
||||
title: Comparison with the Inequality Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与不等式算子的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">不等运算符( <code>!=</code> )与等于运算符相反。它意味着“不等于”并返回<code>false</code> ,其中相等性将返回<code>true</code> , <em>反之亦然</em> 。与等式运算符一样,不等式运算符将在比较时转换数据类型的值。 <strong>例子</strong> <blockquote> 1!= 2 //是的<br> 1!=“1”//假<br> 1!='1'//假<br> 1!= true // false <br> 0!= false // false </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在<code>if</code>语句中添加不等式运算符<code>!=</code> ,以便当<code>val</code>不等于<code>99</code>时函数将返回“Not Equal” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testNotEqual(99)</code>应返回“Equal”
|
||||
testString: 'assert(testNotEqual(99) === "Equal", "<code>testNotEqual(99)</code> should return "Equal"");'
|
||||
- text: <code>testNotEqual("99")</code>应该返回“Equal”
|
||||
testString: 'assert(testNotEqual("99") === "Equal", "<code>testNotEqual("99")</code> should return "Equal"");'
|
||||
- text: <code>testNotEqual(12)</code>应该返回“Not Equal”
|
||||
testString: 'assert(testNotEqual(12) === "Not Equal", "<code>testNotEqual(12)</code> should return "Not Equal"");'
|
||||
- text: <code>testNotEqual("12")</code>应该返回“Not Equal”
|
||||
testString: 'assert(testNotEqual("12") === "Not Equal", "<code>testNotEqual("12")</code> should return "Not Equal"");'
|
||||
- text: <code>testNotEqual("bob")</code>应返回“Not Equal”
|
||||
testString: 'assert(testNotEqual("bob") === "Not Equal", "<code>testNotEqual("bob")</code> should return "Not Equal"");'
|
||||
- text: 你应该使用<code>!=</code>运算符
|
||||
testString: 'assert(code.match(/(?!!==)!=/), "You should use the <code>!=</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testNotEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Not Equal";
|
||||
}
|
||||
return "Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testNotEqual(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d6
|
||||
title: Comparison with the Less Than Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与小于算子的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <dfn>小于</dfn>运算符( <code><</code> )比较两个数字的值。如果左边的数字小于右边的数字,则返回<code>true</code> 。否则,它返回<code>false</code> 。与等于运算符一样, <dfn>少于</dfn>运算符在比较时转换数据类型。 <strong>例子</strong> <blockquote> 2 <5 //是的<br> '3'<7 //是的<br> 5 <5 //假<br> 3 <2 //假<br> '8'<4 //假</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>less than</code>运算符添加到指示的行,以便返回语句有意义。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testLessThan(0)</code>应该返回“25岁以下”
|
||||
testString: 'assert(testLessThan(0) === "Under 25", "<code>testLessThan(0)</code> should return "Under 25"");'
|
||||
- text: <code>testLessThan(24)</code>应该返回“25岁以下”
|
||||
testString: 'assert(testLessThan(24) === "Under 25", "<code>testLessThan(24)</code> should return "Under 25"");'
|
||||
- text: <code>testLessThan(25)</code>应该返回“55岁以下”
|
||||
testString: 'assert(testLessThan(25) === "Under 55", "<code>testLessThan(25)</code> should return "Under 55"");'
|
||||
- text: <code>testLessThan(54)</code>应该返回“55岁以下”
|
||||
testString: 'assert(testLessThan(54) === "Under 55", "<code>testLessThan(54)</code> should return "Under 55"");'
|
||||
- text: <code>testLessThan(55)</code>应返回“55或以上”
|
||||
testString: 'assert(testLessThan(55) === "55 or Over", "<code>testLessThan(55)</code> should return "55 or Over"");'
|
||||
- text: <code>testLessThan(99)</code>应返回“55或以上”
|
||||
testString: 'assert(testLessThan(99) === "55 or Over", "<code>testLessThan(99)</code> should return "55 or Over"");'
|
||||
- text: 您应该至少使用<code><</code>运算符两次
|
||||
testString: 'assert(code.match(/val\s*<\s*("|")*\d+("|")*/g).length > 1, "You should use the <code><</code> operator at least twice");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testLessThan(val) {
|
||||
if (val) { // Change this line
|
||||
return "Under 25";
|
||||
}
|
||||
|
||||
if (val) { // Change this line
|
||||
return "Under 55";
|
||||
}
|
||||
|
||||
return "55 or Over";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testLessThan(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d7
|
||||
title: Comparison with the Less Than Or Equal To Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与小于或等于运算符的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>less than or equal to</code>运算符( <code><=</code> )比较两个数字的值。如果左边的数字小于或等于右边的数字,则返回<code>true</code> 。如果左侧的数字大于右侧的数字,则返回<code>false</code> 。与等于运算符一样, <code>less than or equal to</code>转换数据类型。 <strong>例子</strong> <blockquote> 4 <= 5 //是的<br> '7'<= 7 //是的<br> 5 <= 5 //是的<br> 3 <= 2 //假<br> '8'<= 4 //假</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>less than or equal to</code>运算符添加到指示的行,以便返回语句有意义。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testLessOrEqual(0)</code>应该返回“小于或等于12”
|
||||
testString: 'assert(testLessOrEqual(0) === "Smaller Than or Equal to 12", "<code>testLessOrEqual(0)</code> should return "Smaller Than or Equal to 12"");'
|
||||
- text: <code>testLessOrEqual(11)</code>应返回“小于或等于12”
|
||||
testString: 'assert(testLessOrEqual(11) === "Smaller Than or Equal to 12", "<code>testLessOrEqual(11)</code> should return "Smaller Than or Equal to 12"");'
|
||||
- text: <code>testLessOrEqual(12)</code>应返回“小于或等于12”
|
||||
testString: 'assert(testLessOrEqual(12) === "Smaller Than or Equal to 12", "<code>testLessOrEqual(12)</code> should return "Smaller Than or Equal to 12"");'
|
||||
- text: <code>testLessOrEqual(23)</code>应返回“小于或等于24”
|
||||
testString: 'assert(testLessOrEqual(23) === "Smaller Than or Equal to 24", "<code>testLessOrEqual(23)</code> should return "Smaller Than or Equal to 24"");'
|
||||
- text: <code>testLessOrEqual(24)</code>应返回“小于或等于24”
|
||||
testString: 'assert(testLessOrEqual(24) === "Smaller Than or Equal to 24", "<code>testLessOrEqual(24)</code> should return "Smaller Than or Equal to 24"");'
|
||||
- text: <code>testLessOrEqual(25)</code>应该返回“超过24”
|
||||
testString: 'assert(testLessOrEqual(25) === "More Than 24", "<code>testLessOrEqual(25)</code> should return "More Than 24"");'
|
||||
- text: <code>testLessOrEqual(55)</code>应该返回“超过24”
|
||||
testString: 'assert(testLessOrEqual(55) === "More Than 24", "<code>testLessOrEqual(55)</code> should return "More Than 24"");'
|
||||
- text: 你应该至少使用<code><=</code>运算符两次
|
||||
testString: 'assert(code.match(/val\s*<=\s*("|")*\d+("|")*/g).length > 1, "You should use the <code><=</code> operator at least twice");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testLessOrEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Smaller Than or Equal to 12";
|
||||
}
|
||||
|
||||
if (val) { // Change this line
|
||||
return "Smaller Than or Equal to 24";
|
||||
}
|
||||
|
||||
return "More Than 24";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testLessOrEqual(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d1
|
||||
title: Comparison with the Strict Equality Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与严格平等算子的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">严格相等( <code>===</code> )是相等运算符( <code>==</code> )的对应物。但是,与尝试将两个值转换为常见类型的等式运算符不同,严格相等运算符不执行类型转换。如果要比较的值具有不同的类型,则认为它们不相等,并且严格相等运算符将返回false。 <strong>例子</strong> <blockquote> 3 === 3 //是的<br> 3 ==='3'//假</blockquote>在第二个示例中, <code>3</code>是<code>Number</code>类型, <code>'3'</code>是<code>String</code>类型。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在<code>if</code>语句中使用strict equality运算符,因此当<code>val</code>严格等于<code>7</code>时,函数将返回“Equal” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testStrict(10)</code>应返回“Not Equal”
|
||||
testString: 'assert(testStrict(10) === "Not Equal", "<code>testStrict(10)</code> should return "Not Equal"");'
|
||||
- text: <code>testStrict(7)</code>应返回“Equal”
|
||||
testString: 'assert(testStrict(7) === "Equal", "<code>testStrict(7)</code> should return "Equal"");'
|
||||
- text: <code>testStrict("7")</code>应返回“Not Equal”
|
||||
testString: 'assert(testStrict("7") === "Not Equal", "<code>testStrict("7")</code> should return "Not Equal"");'
|
||||
- text: 您应该使用<code>===</code>运算符
|
||||
testString: 'assert(code.match(/(val\s*===\s*\d+)|(\d+\s*===\s*val)/g).length > 0, "You should use the <code>===</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testStrict(val) {
|
||||
if (val) { // Change this line
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testStrict(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,71 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d3
|
||||
title: Comparison with the Strict Inequality Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与严格不等式算子的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">严格不等式运算符( <code>!==</code> )与严格相等运算符的逻辑相反。它意味着“严格不等于”并返回<code>false</code> ,其中严格相等将返回<code>true</code> , <em>反之亦然</em> 。严格的不等式不会转换数据类型。 <strong>例子</strong> <blockquote> 3!== 3 //假<br> 3!=='3'//是的<br> 4!== 3 //是的</blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>strict inequality operator</code>添加到<code>if</code>语句,以便当<code>val</code>不严格等于<code>17</code>时,函数将返回“Not Equal” </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testStrictNotEqual(17)</code>应返回“Equal”
|
||||
testString: 'assert(testStrictNotEqual(17) === "Equal", "<code>testStrictNotEqual(17)</code> should return "Equal"");'
|
||||
- text: <code>testStrictNotEqual("17")</code>应返回“Not Equal”
|
||||
testString: 'assert(testStrictNotEqual("17") === "Not Equal", "<code>testStrictNotEqual("17")</code> should return "Not Equal"");'
|
||||
- text: <code>testStrictNotEqual(12)</code>应该返回“Not Equal”
|
||||
testString: 'assert(testStrictNotEqual(12) === "Not Equal", "<code>testStrictNotEqual(12)</code> should return "Not Equal"");'
|
||||
- text: <code>testStrictNotEqual("bob")</code>应返回“Not Equal”
|
||||
testString: 'assert(testStrictNotEqual("bob") === "Not Equal", "<code>testStrictNotEqual("bob")</code> should return "Not Equal"");'
|
||||
- text: 你应该使用<code>!==</code>运算符
|
||||
testString: 'assert(code.match(/(val\s*!==\s*\d+)|(\d+\s*!==\s*val)/g).length > 0, "You should use the <code>!==</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testStrictNotEqual(val) {
|
||||
// Only Change Code Below this Line
|
||||
|
||||
if (val) {
|
||||
|
||||
// Only Change Code Above this Line
|
||||
|
||||
return "Not Equal";
|
||||
}
|
||||
return "Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testStrictNotEqual(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d8
|
||||
title: Comparisons with the Logical And Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与逻辑和运算符的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有时你需要一次测试多个东西。当且仅当其左侧和右侧的<dfn>操作数</dfn>为<code>true</code>时, <dfn>逻辑和</dfn>运算符( <code>&&</code> )才返回true。如果将if语句嵌套在另一个语句中,则可以实现相同的效果: <blockquote> if(num> 5){ <br> if(num <10){ <br>返回“是”; <br> } <br> } <br>返回“否”; </blockquote>如果<code>num</code>大于<code>5</code>且小于<code>10</code>则仅返回“Yes”。相同的逻辑可以写成: <blockquote> if(num> 5 && num <10){ <br>返回“是”; <br> } <br>返回“否”; </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将两个if语句合并为一个语句,如果<code>val</code>小于或等于<code>50</code>且大于或等于<code>25</code> ,则返回<code>"Yes"</code> 。否则,将返回<code>"No"</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用一次<code>&&</code>运算符
|
||||
testString: 'assert(code.match(/&&/g).length === 1, "You should use the <code>&&</code> operator once");'
|
||||
- text: 你应该只有一个<code>if</code>语句
|
||||
testString: 'assert(code.match(/if/g).length === 1, "You should only have one <code>if</code> statement");'
|
||||
- text: <code>testLogicalAnd(0)</code>应返回“否”
|
||||
testString: 'assert(testLogicalAnd(0) === "No", "<code>testLogicalAnd(0)</code> should return "No"");'
|
||||
- text: <code>testLogicalAnd(24)</code>应返回“否”
|
||||
testString: 'assert(testLogicalAnd(24) === "No", "<code>testLogicalAnd(24)</code> should return "No"");'
|
||||
- text: <code>testLogicalAnd(25)</code>应返回“是”
|
||||
testString: 'assert(testLogicalAnd(25) === "Yes", "<code>testLogicalAnd(25)</code> should return "Yes"");'
|
||||
- text: <code>testLogicalAnd(30)</code>应该返回“是”
|
||||
testString: 'assert(testLogicalAnd(30) === "Yes", "<code>testLogicalAnd(30)</code> should return "Yes"");'
|
||||
- text: <code>testLogicalAnd(50)</code>应该返回“是”
|
||||
testString: 'assert(testLogicalAnd(50) === "Yes", "<code>testLogicalAnd(50)</code> should return "Yes"");'
|
||||
- text: <code>testLogicalAnd(51)</code>应返回“否”
|
||||
testString: 'assert(testLogicalAnd(51) === "No", "<code>testLogicalAnd(51)</code> should return "No"");'
|
||||
- text: <code>testLogicalAnd(75)</code>应返回“否”
|
||||
testString: 'assert(testLogicalAnd(75) === "No", "<code>testLogicalAnd(75)</code> should return "No"");'
|
||||
- text: <code>testLogicalAnd(80)</code>应返回“否”
|
||||
testString: 'assert(testLogicalAnd(80) === "No", "<code>testLogicalAnd(80)</code> should return "No"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testLogicalAnd(val) {
|
||||
// Only change code below this line
|
||||
|
||||
if (val) {
|
||||
if (val) {
|
||||
return "Yes";
|
||||
}
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
return "No";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testLogicalAnd(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,83 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d9
|
||||
title: Comparisons with the Logical Or Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 与逻辑或运算符的比较
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <dfn>逻辑OR</dfn>运算符( <code>||</code> )返回<code>true</code> ,如果任一<dfn>操作数</dfn>为<code>true</code> 。否则,它返回<code>false</code> 。 <dfn>逻辑或</dfn>运算符由两个管道符号( <code>|</code> )组成。这通常可以在Backspace和Enter键之间找到。以下模式应该从以前的方法点看起来很熟悉: <blockquote> if(num> 10){ <br>返回“否”; <br> } <br> if(num <5){ <br>返回“否”; <br> } <br>返回“是”; </blockquote>仅当<code>num</code>介于<code>5</code>和<code>10</code>之间(包括5和10)时,才会返回“Yes”。相同的逻辑可以写成: <blockquote> if(num> 10 || num <5){ <br>返回“否”; <br> } <br>返回“是”; </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将两个<code>if</code>语句组合成一个语句,如果<code>val</code>不在<code>10</code>和<code>20</code>之间(包括<code>10</code>和<code>20</code> ,则返回<code>"Outside"</code> 。否则,返回<code>"Inside"</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>||</code>操作员一次
|
||||
testString: 'assert(code.match(/\|\|/g).length === 1, "You should use the <code>||</code> operator once");'
|
||||
- text: 你应该只有一个<code>if</code>语句
|
||||
testString: 'assert(code.match(/if/g).length === 1, "You should only have one <code>if</code> statement");'
|
||||
- text: <code>testLogicalOr(0)</code>应返回“Outside”
|
||||
testString: 'assert(testLogicalOr(0) === "Outside", "<code>testLogicalOr(0)</code> should return "Outside"");'
|
||||
- text: <code>testLogicalOr(9)</code>应返回“Outside”
|
||||
testString: 'assert(testLogicalOr(9) === "Outside", "<code>testLogicalOr(9)</code> should return "Outside"");'
|
||||
- text: <code>testLogicalOr(10)</code>应返回“Inside”
|
||||
testString: 'assert(testLogicalOr(10) === "Inside", "<code>testLogicalOr(10)</code> should return "Inside"");'
|
||||
- text: <code>testLogicalOr(15)</code>应返回“Inside”
|
||||
testString: 'assert(testLogicalOr(15) === "Inside", "<code>testLogicalOr(15)</code> should return "Inside"");'
|
||||
- text: <code>testLogicalOr(19)</code>应该返回“Inside”
|
||||
testString: 'assert(testLogicalOr(19) === "Inside", "<code>testLogicalOr(19)</code> should return "Inside"");'
|
||||
- text: <code>testLogicalOr(20)</code>应该返回“Inside”
|
||||
testString: 'assert(testLogicalOr(20) === "Inside", "<code>testLogicalOr(20)</code> should return "Inside"");'
|
||||
- text: <code>testLogicalOr(21)</code>应该返回“Outside”
|
||||
testString: 'assert(testLogicalOr(21) === "Outside", "<code>testLogicalOr(21)</code> should return "Outside"");'
|
||||
- text: <code>testLogicalOr(25)</code>应返回“Outside”
|
||||
testString: 'assert(testLogicalOr(25) === "Outside", "<code>testLogicalOr(25)</code> should return "Outside"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testLogicalOr(val) {
|
||||
// Only change code below this line
|
||||
|
||||
if (val) {
|
||||
return "Outside";
|
||||
}
|
||||
|
||||
if (val) {
|
||||
return "Outside";
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
return "Inside";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testLogicalOr(15);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244af
|
||||
title: Compound Assignment With Augmented Addition
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 具有增强加法的复合赋值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在编程中,通常使用赋值来修改变量的内容。请记住,首先评估等号右侧的所有内容,因此我们可以说: <code>myVar = myVar + 5;</code>添加<code>5</code>到<code>myVar</code> 。由于这是一种常见的模式,因此存在一步完成数学运算和赋值的运算符。一个这样的运算符是<code>+=</code>运算符。 <blockquote> var myVar = 1; <br> myVar + = 5; <br>的console.log(myVar的); //返回6 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">转换<code>a</code> , <code>b</code>和<code>c</code>的赋值以使用<code>+=</code>运算符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应该等于<code>15</code>
|
||||
testString: 'assert(a === 15, "<code>a</code> should equal <code>15</code>");'
|
||||
- text: <code>b</code>应该等于<code>26</code>
|
||||
testString: 'assert(b === 26, "<code>b</code> should equal <code>26</code>");'
|
||||
- text: <code>c</code>应该等于<code>19</code>
|
||||
testString: 'assert(c === 19, "<code>c</code> should equal <code>19</code>");'
|
||||
- text: 您应该为每个变量使用<code>+=</code>运算符
|
||||
testString: 'assert(code.match(/\+=/g).length === 3, "You should use the <code>+=</code> operator for each variable");'
|
||||
- text: 不要修改行上方的代码
|
||||
testString: 'assert(/var a = 3;/.test(code) && /var b = 17;/.test(code) && /var c = 12;/.test(code), "Do not modify the code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var a = 3;
|
||||
var b = 17;
|
||||
var c = 12;
|
||||
|
||||
// Only modify code below this line
|
||||
|
||||
a = a + 12;
|
||||
b = 9 + b;
|
||||
c = c + 7;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b2
|
||||
title: Compound Assignment With Augmented Division
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 具有增广划分的复合赋值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>/=</code>运算符将变量除以另一个数字。 <code>myVar = myVar / 5;</code>将<code>myVar</code>除以<code>5</code> 。这可以改写为: <code>myVar /= 5;</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">转换<code>a</code> , <code>b</code>和<code>c</code>的赋值以使用<code>/=</code>运算符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应该等于<code>4</code>
|
||||
testString: 'assert(a === 4, "<code>a</code> should equal <code>4</code>");'
|
||||
- text: <code>b</code>应该等于<code>27</code>
|
||||
testString: 'assert(b === 27, "<code>b</code> should equal <code>27</code>");'
|
||||
- text: <code>c</code>应该等于<code>3</code>
|
||||
testString: 'assert(c === 3, "<code>c</code> should equal <code>3</code>");'
|
||||
- text: 您应该为每个变量使用<code>/=</code>运算符
|
||||
testString: 'assert(code.match(/\/=/g).length === 3, "You should use the <code>/=</code> operator for each variable");'
|
||||
- text: 不要修改行上方的代码
|
||||
testString: 'assert(/var a = 48;/.test(code) && /var b = 108;/.test(code) && /var c = 33;/.test(code), "Do not modify the code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var a = 48;
|
||||
var b = 108;
|
||||
var c = 33;
|
||||
|
||||
// Only modify code below this line
|
||||
|
||||
a = a / 12;
|
||||
b = b / 4;
|
||||
c = c / 11;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b1
|
||||
title: Compound Assignment With Augmented Multiplication
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 具有增广乘法的复合赋值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>*=</code>运算符将变量乘以数字。 <code>myVar = myVar * 5;</code>将<code>myVar</code>乘以<code>5</code> 。这可以改写为: <code>myVar *= 5;</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">转换<code>a</code> , <code>b</code>和<code>c</code>的赋值以使用<code>*=</code>运算符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应该等于<code>25</code>
|
||||
testString: 'assert(a === 25, "<code>a</code> should equal <code>25</code>");'
|
||||
- text: <code>b</code>应该等于<code>36</code>
|
||||
testString: 'assert(b === 36, "<code>b</code> should equal <code>36</code>");'
|
||||
- text: <code>c</code>应该等于<code>46</code>
|
||||
testString: 'assert(c === 46, "<code>c</code> should equal <code>46</code>");'
|
||||
- text: 您应该为每个变量使用<code>*=</code>运算符
|
||||
testString: 'assert(code.match(/\*=/g).length === 3, "You should use the <code>*=</code> operator for each variable");'
|
||||
- text: 不要修改行上方的代码
|
||||
testString: 'assert(/var a = 5;/.test(code) && /var b = 12;/.test(code) && /var c = 4\.6;/.test(code), "Do not modify the code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var a = 5;
|
||||
var b = 12;
|
||||
var c = 4.6;
|
||||
|
||||
// Only modify code below this line
|
||||
|
||||
a = a * 5;
|
||||
b = 3 * b;
|
||||
c = c * 10;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b0
|
||||
title: Compound Assignment With Augmented Subtraction
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 具有增广减法的复合赋值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">与<code>+=</code>运算符一样, <code>-=</code>从变量中减去一个数字。 <code>myVar = myVar - 5;</code>将从<code>myVar</code>减去<code>5</code> 。这可以改写为: <code>myVar -= 5;</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">转换<code>a</code> , <code>b</code>和<code>c</code>的赋值以使用<code>-=</code>运算符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应该等于<code>5</code>
|
||||
testString: 'assert(a === 5, "<code>a</code> should equal <code>5</code>");'
|
||||
- text: <code>b</code>应该等于<code>-6</code>
|
||||
testString: 'assert(b === -6, "<code>b</code> should equal <code>-6</code>");'
|
||||
- text: <code>c</code>应该等于<code>2</code>
|
||||
testString: 'assert(c === 2, "<code>c</code> should equal <code>2</code>");'
|
||||
- text: 您应该为每个变量使用<code>-=</code>运算符
|
||||
testString: 'assert(code.match(/-=/g).length === 3, "You should use the <code>-=</code> operator for each variable");'
|
||||
- text: 不要修改行上方的代码
|
||||
testString: 'assert(/var a = 11;/.test(code) && /var b = 9;/.test(code) && /var c = 3;/.test(code), "Do not modify the code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var a = 11;
|
||||
var b = 9;
|
||||
var c = 3;
|
||||
|
||||
// Only modify code below this line
|
||||
|
||||
a = a - 6;
|
||||
b = b - 15;
|
||||
c = c - 1;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b7
|
||||
title: Concatenating Strings with Plus Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 用Plus运算符连接字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在JavaScript中,当<code>+</code>运算符与<code>String</code>值一起使用时,它被称为<dfn>连接</dfn>运算符。您可以通过<dfn>将</dfn>它们<dfn>连接</dfn>在一起来构建其他字符串中的新字符串。 <strong>例</strong> <blockquote> “我叫艾伦,'+'我连接起来。” </blockquote> <strong>注意</strong> <br>留意空间。连接不会在连接字符串之间添加空格,因此您需要自己添加它们。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">从字符串构建<code>myStr</code> <code>"This is the start. "</code>和<code>"This is the end."</code>使用<code>+</code>运算符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myStr</code>应该有一个值<code>This is the start. This is the end.</code>
|
||||
testString: 'assert(myStr === "This is the start. This is the end.", "<code>myStr</code> should have a value of <code>This is the start. This is the end.</code>");'
|
||||
- text: 使用<code>+</code>运算符构建<code>myStr</code>
|
||||
testString: 'assert(code.match(/([""]).*([""])\s*\+\s*([""]).*([""])/g).length > 1, "Use the <code>+</code> operator to build <code>myStr</code>");'
|
||||
- text: 应使用<code>var</code>关键字创建<code>myStr</code> 。
|
||||
testString: 'assert(/var\s+myStr/.test(code), "<code>myStr</code> should be created using the <code>var</code> keyword.");'
|
||||
- text: 确保将结果分配给<code>myStr</code>变量。
|
||||
testString: 'assert(/myStr\s*=/.test(code), "Make sure to assign the result to the <code>myStr</code> variable.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourStr = "I come first. " + "I come second.";
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var myStr;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b8
|
||||
title: Concatenating Strings with the Plus Equals Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用Plus Equals运算符连接字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们还可以使用<code>+=</code>运算符将字符串<dfn>连接</dfn>到现有字符串变量的末尾。这对于在多行上打破长字符串非常有帮助。 <strong>注意</strong> <br>留意空间。连接不会在连接字符串之间添加空格,因此您需要自己添加它们。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">通过连接这两个字符串来构建<code>myStr</code>几行: <code>"This is the first sentence. "</code>和<code>"This is the second sentence."</code>使用<code>+=</code>运算符。使用<code>+=</code>运算符,类似于它在编辑器中的显示方式。首先将第一个字符串分配给<code>myStr</code> ,然后添加第二个字符串。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myStr</code>应该有一个值<code>This is the first sentence. This is the second sentence.</code>
|
||||
testString: 'assert(myStr === "This is the first sentence. This is the second sentence.", "<code>myStr</code> should have a value of <code>This is the first sentence. This is the second sentence.</code>");'
|
||||
- text: 使用<code>+=</code>运算符构建<code>myStr</code>
|
||||
testString: 'assert(code.match(/\w\s*\+=\s*[""]/g).length > 1 && code.match(/\w\s*\=\s*[""]/g).length > 1, "Use the <code>+=</code> operator to build <code>myStr</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourStr = "I come first. ";
|
||||
ourStr += "I come second.";
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var myStr;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b9
|
||||
title: Constructing Strings with Variables
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 用变量构造字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有时您需要构建一个字符串, <a href="https://en.wikipedia.org/wiki/Mad_Libs" target="_blank">Mad Libs</a>样式。通过使用连接运算符( <code>+</code> ),您可以将一个或多个变量插入到正在构建的字符串中。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>myName</code>设置为等于您的名字的字符串,并在字符串<code>"My name is "</code>和<code>" and I am well!"</code>之间用<code>myName</code>构建<code>myStr</code> <code>" and I am well!"</code> </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myName</code>应设置为至少3个字符长的字符串
|
||||
testString: 'assert(typeof myName !== "undefined" && myName.length > 2, "<code>myName</code> should be set to a string at least 3 characters long");'
|
||||
- text: 使用两个<code>+</code>运算符在其中构建<code>myStr</code> with <code>myName</code>
|
||||
testString: 'assert(code.match(/[""]\s*\+\s*myName\s*\+\s*[""]/g).length > 0, "Use two <code>+</code> operators to build <code>myStr</code> with <code>myName</code> inside it");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourName = "freeCodeCamp";
|
||||
var ourStr = "Hello, our name is " + ourName + ", how are you?";
|
||||
|
||||
// Only change code below this line
|
||||
var myName;
|
||||
var myStr;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,71 @@
|
||||
---
|
||||
id: 56105e7b514f539506016a5e
|
||||
title: Count Backwards With a For Loop
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 用For循环向后计数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">只要我们可以定义正确的条件,for循环也可以向后计数。为了向后计数两次,我们需要更改<code>initialization</code> , <code>condition</code>和<code>final-expression</code> 。我们将从<code>i = 10</code>开始并在<code>i > 0</code>循环。我们将递减<code>i</code> 2每个回路与<code>i -= 2</code> 。 <blockquote> var ourArray = []; <br> for(var i = 10; i> 0; i- = 2){ <br> ourArray.push(ⅰ); <br> } </blockquote> <code>ourArray</code>现在包含<code>[10,8,6,4,2]</code> 。让我们改变<code>initialization</code>和<code>final-expression</code>这样我们就可以向后计数两位奇数。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>for</code>循环将奇数从9到1推送到<code>myArray</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该为此使用<code>for</code>循环。
|
||||
testString: 'assert(code.match(/for\s*\(/g).length > 1, "You should be using a <code>for</code> loop for this.");'
|
||||
- text: 你应该使用数组方法<code>push</code> 。
|
||||
testString: 'assert(code.match(/myArray.push/), "You should be using the array method <code>push</code>.");'
|
||||
- text: '<code>myArray</code>应该等于<code>[9,7,5,3,1]</code> 。'
|
||||
testString: 'assert.deepEqual(myArray, [9,7,5,3,1], "<code>myArray</code> should equal <code>[9,7,5,3,1]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [];
|
||||
|
||||
for (var i = 10; i > 0; i -= 2) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 565bbe00e9cc8ac0725390f4
|
||||
title: Counting Cards
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 计数卡
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在赌场游戏Blackjack中,玩家可以通过跟踪牌组中剩余的高牌和低牌的相对数量来获得优势。这称为<a href="https://en.wikipedia.org/wiki/Card_counting" target="_blank">卡计数</a> 。在牌组中剩下更多高牌有利于玩家。根据下表为每张卡分配一个值。当计数为正数时,玩家应该下注。当计数为零或负数时,玩家应该下注低。 <table class="table table-striped"><thead><tr><th>计数变化</th><th>牌</th></tr></thead><tbody><tr><td> +1 </td><td> 2,3,4,5,6 </td></tr><tr><td> 0 </td><td> 7,8,9 </td></tr><tr><td> -1 </td><td> 10,'J','Q','K','A' </td></tr></tbody></table>你会写一个卡计数功能。它将接收一个<code>card</code>参数,可以是数字或字符串,并根据卡的值递增或递减全局<code>count</code>变量(参见表格)。然后,该函数将返回一个包含当前计数的字符串,如果计数为正则返回字符串<code>Bet</code> ,如果计数为零或为负,则返回<code>Hold</code> 。当前计数和玩家的决定( <code>Bet</code>或<code>Hold</code> )应该由一个空格分隔。 <strong>示例输出</strong> <br> <code>-3 Hold</code> <br> <code>5 Bet</code> <strong>提示</strong> <br>当值为7,8或9时,请勿将<code>count</code>重置为0。 <br>不要返回数组。 <br>不要在输出中包含引号(单引号或双引号)。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 牌序列<code>5 Bet</code>应该返回<code>5 Bet</code>
|
||||
testString: 'assert((function(){ count = 0; cc(2);cc(3);cc(4);cc(5);var out = cc(6); if(out === "5 Bet") {return true;} return false; })(), "Cards Sequence 2, 3, 4, 5, 6 should return <code>5 Bet</code>");'
|
||||
- text: '卡片序列7,8,9应返回<code>0 Hold</code>'
|
||||
testString: 'assert((function(){ count = 0; cc(7);cc(8);var out = cc(9); if(out === "0 Hold") {return true;} return false; })(), "Cards Sequence 7, 8, 9 should return <code>0 Hold</code>");'
|
||||
- text: 卡序列10,J,Q,K,A应返回<code>-5 Hold</code>
|
||||
testString: 'assert((function(){ count = 0; cc(10);cc("J");cc("Q");cc("K");var out = cc("A"); if(out === "-5 Hold") {return true;} return false; })(), "Cards Sequence 10, J, Q, K, A should return <code>-5 Hold</code>");'
|
||||
- text: '卡序列3,7,Q,8,A应返回<code>-1 Hold</code>'
|
||||
testString: 'assert((function(){ count = 0; cc(3);cc(7);cc("Q");cc(8);var out = cc("A"); if(out === "-1 Hold") {return true;} return false; })(), "Cards Sequence 3, 7, Q, 8, A should return <code>-1 Hold</code>");'
|
||||
- text: 牌序列2,J, <code>1 Bet</code>应该返回<code>1 Bet</code>
|
||||
testString: 'assert((function(){ count = 0; cc(2);cc("J");cc(9);cc(2);var out = cc(7); if(out === "1 Bet") {return true;} return false; })(), "Cards Sequence 2, J, 9, 2, 7 should return <code>1 Bet</code>");'
|
||||
- text: 牌序列<code>1 Bet</code>应该返回<code>1 Bet</code>
|
||||
testString: 'assert((function(){ count = 0; cc(2);cc(2);var out = cc(10); if(out === "1 Bet") {return true;} return false; })(), "Cards Sequence 2, 2, 10 should return <code>1 Bet</code>");'
|
||||
- text: '卡序列3,2,A,10,K应返回<code>-1 Hold</code>'
|
||||
testString: 'assert((function(){ count = 0; cc(3);cc(2);cc("A");cc(10);var out = cc("K"); if(out === "-1 Hold") {return true;} return false; })(), "Cards Sequence 3, 2, A, 10, K should return <code>-1 Hold</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var count = 0;
|
||||
|
||||
function cc(card) {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
return "Change Me";
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
// Add/remove calls to test your function.
|
||||
// Note: Only the last will display
|
||||
cc(2); cc(3); cc(7); cc('K'); cc('A');
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,61 @@
|
||||
---
|
||||
id: cf1391c1c11feddfaeb4bdef
|
||||
title: Create Decimal Numbers with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript创建十进制数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们也可以在变量中存储十进制数。十进制数有时称为<dfn>浮点数</dfn>或<dfn>浮点数</dfn> 。 <strong>注意</strong> <br>并非所有实数都可以准确地以<dfn>浮点</dfn>表示。这可能导致舍入错误。 <a href="https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems" target="_blank">细节在这里</a> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个变量<code>myDecimal</code>并给它一个带小数部分的十进制值(例如<code>5.7</code> )。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myDecimal</code>应该是一个数字。
|
||||
testString: 'assert(typeof myDecimal === "number", "<code>myDecimal</code> should be a number.");'
|
||||
- text: <code>myDecimal</code>应该有一个小数点
|
||||
testString: 'assert(myDecimal % 1 != 0, "<code>myDecimal</code> should have a decimal point"); '
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var ourDecimal = 5.7;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,60 @@
|
||||
---
|
||||
id: bd7123c9c443eddfaeb5bdef
|
||||
title: Declare JavaScript Variables
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 声明JavaScript变量
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在计算机科学中, <dfn>数据</dfn>是对计算机有意义的任何东西。 JavaScript提供了七种不同的<dfn>数据类型</dfn> ,它们是<code>undefined</code> , <code>null</code> , <code>boolean</code> , <code>string</code> , <code>symbol</code> , <code>number</code>和<code>object</code> 。例如,计算机区分数字(例如数字<code>12</code> )和<code>strings</code> (例如<code>"12"</code> , <code>"dog"</code>或<code>"123 cats"</code> ,它们是字符集合。计算机可以对数字执行数学运算,但不能对字符串执行数学运算。 <dfn>变量</dfn>允许计算机以动态方式存储和操作数据。他们通过使用“标签”指向数据而不是使用数据本身来做到这一点。七种数据类型中的任何一种都可以存储在变量中。 <code>Variables</code>类似于您在数学中使用的x和y变量,这意味着它们是表示我们想要引用的数据的简单名称。计算机<code>variables</code>与数学<code>variables</code>不同之处在于它们可以在不同时间存储不同的值。我们告诉JavaScript通过将关键字<code>var</code>放在它前面来创建或<dfn>声明</dfn>变量,如下所示: <blockquote> var ourName; </blockquote>创建一个名为<code>ourName</code>的<code>variable</code> 。在JavaScript中,我们以分号结束语句。 <code>Variable</code>名可以由数字,字母和<code>$</code>或<code>_</code> ,但不能包含空格或以数字开头。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>var</code>关键字创建名为<code>myName</code>的变量。 <strong>暗示</strong> <br>如果你遇到<code>ourName</code>查看我们的<code>ourName</code>示例。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您应该使用<code>var</code>关键字声明<code>myName</code> ,以分号结尾
|
||||
testString: 'assert(/var\s+myName\s*;/.test(code), "You should declare <code>myName</code> with the <code>var</code> keyword, ending with a semicolon");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourName;
|
||||
|
||||
// Declare myName below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,63 @@
|
||||
---
|
||||
id: bd7123c9c444eddfaeb5bdef
|
||||
title: Declare String Variables
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 声明字符串变量
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">以前我们使用过代码<code>var myName = "your name";</code> <code>"your name"</code>被称为<dfn>字符串</dfn> <dfn>文字</dfn> 。它是一个字符串,因为它是用单引号或双引号括起来的一系列零个或多个字符。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建两个新的<code>string</code>变量: <code>myFirstName</code>和<code>myLastName</code>并分别为它们分配<code>myFirstName</code>和<code>myLastName</code>的值。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myFirstName</code>应该是一个至少包含一个字符的字符串。
|
||||
testString: 'assert((function(){if(typeof myFirstName !== "undefined" && typeof myFirstName === "string" && myFirstName.length > 0){return true;}else{return false;}})(), "<code>myFirstName</code> should be a string with at least one character in it.");'
|
||||
- text: <code>myLastName</code>应该是一个至少包含一个字符的字符串。
|
||||
testString: 'assert((function(){if(typeof myLastName !== "undefined" && typeof myLastName === "string" && myLastName.length > 0){return true;}else{return false;}})(), "<code>myLastName</code> should be a string with at least one character in it.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var firstName = "Alan";
|
||||
var lastName = "Turing";
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ad
|
||||
title: Decrement a Number with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript减少数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以使用<code>--</code>运算符轻松地将变量<dfn>减1</dfn>或减1。 <code>i--;</code>相当于<code>i = i - 1;</code> <strong>注意</strong> <br>整条线变成了<code>i--;</code> ,消除了对等号的需要。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改代码以在<code>myVar</code>上使用<code>--</code>运算符。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myVar</code>应该等于<code>10</code>
|
||||
testString: 'assert(myVar === 10, "<code>myVar</code> should equal <code>10</code>");'
|
||||
- text: <code>myVar = myVar - 1;</code>应该改变
|
||||
testString: 'assert(/var\s*myVar\s*=\s*11;\s*\/*.*\s*([-]{2}\s*myVar|myVar\s*[-]{2});/.test(code), "<code>myVar = myVar - 1;</code> should be changed");'
|
||||
- text: 在<code>myVar</code>上使用<code>--</code>运算符
|
||||
testString: 'assert(/[-]{2}\s*myVar|myVar\s*[-]{2}/.test(code), "Use the <code>--</code> operator on <code>myVar</code>");'
|
||||
- text: 不要更改行上方的代码
|
||||
testString: 'assert(/var myVar = 11;/.test(code), "Do not change code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var myVar = 11;
|
||||
|
||||
// Only change code below this line
|
||||
myVar = myVar - 1;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,79 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d3
|
||||
title: Delete Properties from a JavaScript Object
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 从JavaScript对象中删除属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们还可以删除对象中的属性,如下所示: <code>delete ourDog.bark;</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">从<code>myDog</code>删除<code>"tails"</code>属性。您可以使用点或括号表示法。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 从<code>myDog</code>删除属性<code>"tails"</code> 。
|
||||
testString: 'assert(typeof myDog === "object" && myDog.tails === undefined, "Delete the property <code>"tails"</code> from <code>myDog</code>.");'
|
||||
- text: 不要修改<code>myDog</code>设置
|
||||
testString: 'assert(code.match(/"tails": 1/g).length > 1, "Do not modify the <code>myDog</code> setup");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"],
|
||||
"bark": "bow-wow"
|
||||
};
|
||||
|
||||
delete ourDog.bark;
|
||||
|
||||
// Setup
|
||||
var myDog = {
|
||||
"name": "Happy Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"],
|
||||
"bark": "woof"
|
||||
};
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,61 @@
|
||||
---
|
||||
id: bd7993c9ca9feddfaeb7bdef
|
||||
title: Divide One Decimal by Another with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript将另一个十进制除以另一个
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">现在让我们将一位小数除以另一位小数。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改<code>0.0</code>使<code>quotient</code>等于<code>2.2</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 变<code>quotient</code>应该等于<code>2.2</code>
|
||||
testString: 'assert(quotient === 2.2, "The variable <code>quotient</code> should equal <code>2.2</code>");'
|
||||
- text: 您应该使用<code>/</code>运算符将4.4除以2
|
||||
testString: 'assert(/4\.40*\s*\/\s*2\.*0*/.test(code), "You should use the <code>/</code> operator to divide 4.4 by 2");'
|
||||
- text: 商数变量只应分配一次
|
||||
testString: 'assert(code.match(/quotient/g).length === 1, "The quotient variable should only be assigned once");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var quotient = 0.0 / 2.0; // Fix this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,59 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb6bdef
|
||||
title: Divide One Number by Another with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 用JavaScript划分一个号码
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们也可以将一个数字除以另一个数字。 JavaScript使用<code>/</code>符号进行除法。 <p> <strong>例</strong> </p><blockquote> myVar = 16/2; //分配8 </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改<code>0</code> ,使<code>quotient</code>等于<code>2</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 使变量<code>quotient</code>等于2。
|
||||
testString: 'assert(quotient === 2, "Make the variable <code>quotient</code> equal to 2.");'
|
||||
- text: 使用<code>/</code>运算符
|
||||
testString: 'assert(/\d+\s*\/\s*\d+/.test(code), "Use the <code>/</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var quotient = 66 / 0;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,67 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b6
|
||||
title: Escape Sequences in Strings
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 字符串中的转义序列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">引号不是可以在字符串中<dfn>转义</dfn>的唯一字符。使用转义字符有两个原因:首先是允许您使用您可能无法输入的字符,例如退格。其次是允许你在一个字符串中表示多个引号,而不会误解你的意思。我们在之前的挑战中学到了这一点。 <table class="table table-striped"><thead><tr><th>码</th><th>产量</th></tr></thead><tbody><tr><td> <code>\'</code> </td> <td>单引号</td></tr><tr><td> <code>\"</code> </td> <td>双引号</td></tr><tr><td> <code>\\</code> </td> <td>反斜线</td></tr><tr><td> <code>\n</code> </td> <td>新队</td></tr><tr><td> <code>\r</code> </td> <td>回车</td></tr><tr><td> <code>\t</code> </td> <td>标签</td></tr><tr><td> <code>\b</code> </td> <td>退格</td></tr><tr><td> <code>\f</code> </td> <td>形式饲料</td></tr></tbody></table> <em>请注意,必须对反斜杠本身进行转义才能显示为反斜杠。</em> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用转义序列将以下三行文本分配到单个变量<code>myStr</code> 。 <blockquote>第一行<br> \第二行<br> ThirdLine </blockquote>您将需要使用转义序列正确插入特殊字符。您还需要按照上面的间距来跟踪,在转义序列或单词之间没有空格。这是写出转义序列的文本。 <q>FirstLine <code>newline</code> <code>tab</code> <code>backslash</code> SecondLine <code>newline</code> ThirdLine</q> </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myStr</code>不应包含任何空格
|
||||
testString: 'assert(!/ /.test(myStr), "<code>myStr</code> should not contain any spaces");'
|
||||
- text: <code>myStr</code>应包含的字符串<code>FirstLine</code> , <code>SecondLine</code>和<code>ThirdLine</code> (记住区分大小写)
|
||||
testString: 'assert(/FirstLine/.test(myStr) && /SecondLine/.test(myStr) && /ThirdLine/.test(myStr), "<code>myStr</code> should contain the strings <code>FirstLine</code>, <code>SecondLine</code> and <code>ThirdLine</code> (remember case sensitivity)");'
|
||||
- text: <code>FirstLine</code>后面应跟换行符<code>\n</code>
|
||||
testString: 'assert(/FirstLine\n/.test(myStr), "<code>FirstLine</code> should be followed by the newline character <code>\n</code>");'
|
||||
- text: <code>myStr</code>应该包含一个制表字符<code>\t</code> ,它跟在换行符后面
|
||||
testString: 'assert(/\n\t/.test(myStr), "<code>myStr</code> should contain a tab character <code>\t</code> which follows a newline character");'
|
||||
- text: <code>SecondLine</code>应该以反斜杠字符<code>\\</code>开头
|
||||
testString: 'assert(/\SecondLine/.test(myStr), "<code>SecondLine</code> should be preceded by the backslash character <code>\\</code>");'
|
||||
- text: <code>SecondLine</code>和<code>ThirdLine</code>之间应该有换行符
|
||||
testString: 'assert(/SecondLine\nThirdLine/.test(myStr), "There should be a newline character between <code>SecondLine</code> and <code>ThirdLine</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var myStr; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,59 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b5
|
||||
title: Escaping Literal Quotes in Strings
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 逃避字符串中的字面引用
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在定义字符串时,必须以单引号或双引号开头和结尾。当你需要一个文字报价会发生什么: <code>"</code>还是<code>'</code> ?你的字符串里面在JavaScript中,你可以放置一个<dfn>反斜杠</dfn> (从考虑到它作为字符串报价的最终<dfn>逃脱</dfn>报价<code>\</code>在引号前)。 <code>var sampleStr = "Alan said, \"Peter is learning JavaScript\".";</code>这告诉JavaScript,以下引用不是字符串的结尾,而是应该出现在字符串中。所以如果要将它打印到控制台,你会得到: <code>Alan said, "Peter is learning JavaScript".</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<dfn>反斜杠</dfn>将字符串分配给<code>myStr</code>变量,这样如果要将其打印到控制台,您会看到: <code>I am a "double quoted" string inside "double quotes".</code> </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您应该使用两个双引号( <code>"</code> )和四个转义双引号( <code>\"</code> )。
|
||||
testString: 'assert(code.match(/\\"/g).length === 4 && code.match(/[^\\]"/g).length === 2, "You should use two double quotes (<code>"</code>) and four escaped double quotes (<code>\"</code>).");'
|
||||
- text: 变量myStr应该包含字符串: <code>I am a "double quoted" string inside "double quotes".</code>
|
||||
testString: 'assert(myStr === "I am a \"double quoted\" string inside \"double quotes\".", "Variable myStr should contain the string: <code>I am a "double quoted" string inside "double quotes".</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var myStr = ""; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,71 @@
|
||||
---
|
||||
id: bd7123c9c448eddfaeb5bdef
|
||||
title: Find the Length of a String
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 找到字符串的长度
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以通过在字符串变量或字符串文字后面写<code>.length</code>来查找<code>String</code>值的长度。 <code>"Alan Peter".length; // 10</code>例如,如果我们创建了一个变量<code>var firstName = "Charles"</code> ,我们可以通过使用<code>firstName.length</code>属性找出字符串<code>"Charles"</code>长度。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>.length</code>属性计算<code>lastName</code>变量中的字符数,并将其分配给<code>lastNameLength</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>lastNameLength</code>应该等于8。
|
||||
testString: 'assert((function(){if(typeof lastNameLength !== "undefined" && typeof lastNameLength === "number" && lastNameLength === 8){return true;}else{return false;}})(), "<code>lastNameLength</code> should be equal to eight.");'
|
||||
- text: 您应该使用<code>.length</code>来获取<code>lastName</code>的长度,如下所示: <code>lastName.length</code> 。
|
||||
testString: 'assert((function(){if(code.match(/\.length/gi) && code.match(/\.length/gi).length >= 2 && code.match(/var lastNameLength \= 0;/gi) && code.match(/var lastNameLength \= 0;/gi).length >= 1){return true;}else{return false;}})(), "You should be getting the length of <code>lastName</code> by using <code>.length</code> like this: <code>lastName.length</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var firstNameLength = 0;
|
||||
var firstName = "Ada";
|
||||
|
||||
firstNameLength = firstName.length;
|
||||
|
||||
// Setup
|
||||
var lastNameLength = 0;
|
||||
var lastName = "Lovelace";
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
lastNameLength = lastName;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,63 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ae
|
||||
title: Finding a Remainder in JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在JavaScript中查找剩余内容
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <dfn>余数</dfn>运算符<code>%</code>给出了两个数的除法的余数。 <strong>例</strong> <blockquote> 5%2 = 1因为<br> Math.floor(5/2)= 2(商数) <br> 2 * 2 = 4 <br> 5 - 4 = 1(剩余) </blockquote> <strong>用法</strong> <br>在数学中,通过检查数字除以<code>2</code>的余数,可以检查数字是偶数还是奇数。 <blockquote> 17%2 = 1(17为奇数) <br> 48%2 = 0(48为偶数) </blockquote> <strong>注意</strong> <br> <dfn>余数</dfn>运算符有时被错误地称为“模数”运算符。它与模数非常相似,但在负数下不能正常工作。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<dfn>余数</dfn> ( <code>%</code> )运算符将<code>remainder</code>设置为等于<code>11</code>的余数除以<code>3</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 应该初始化变量<code>remainder</code>
|
||||
testString: 'assert(/var\s+?remainder/.test(code), "The variable <code>remainder</code> should be initialized");'
|
||||
- text: <code>remainder</code>的值应为<code>2</code>
|
||||
testString: 'assert(remainder === 2, "The value of <code>remainder</code> should be <code>2</code>");'
|
||||
- text: 您应该使用<code>%</code>运算符
|
||||
testString: 'assert(/\s+?remainder\s*?=\s*?.*%.*;/.test(code), "You should use the <code>%</code> operator");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Only change code below this line
|
||||
|
||||
var remainder;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,68 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb9bdef
|
||||
title: Generate Random Fractions with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript生成随机分数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">随机数对于创建随机行为很有用。 JavaScript有一个<code>Math.random()</code>函数,它生成一个介于<code>0</code> (含)和不高达<code>1</code> (独占)之间的随机十进制数。因此<code>Math.random()</code>可以返回<code>0</code>但永远不会返回<code>1</code> <strong>Note</strong> <br> <a href="storing-values-with-the-assignment-operator" target="_blank">与使用Equal运算符存储值</a>一样,所有函数调用将在<code>return</code>执行之前解析,因此我们可以<code>return</code> <code>Math.random()</code>函数的值。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改<code>randomFraction</code>以返回随机数而不是返回<code>0</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>randomFraction</code>应该返回一个随机数。
|
||||
testString: 'assert(typeof randomFraction() === "number", "<code>randomFraction</code> should return a random number.");'
|
||||
- text: <code>randomFraction</code>返回的<code>randomFraction</code>应该是小数。
|
||||
testString: 'assert((randomFraction()+""). match(/\./g), "The number returned by <code>randomFraction</code> should be a decimal.");'
|
||||
- text: 您应该使用<code>Math.random</code>来生成随机十进制数。
|
||||
testString: 'assert(code.match(/Math\.random/g).length >= 0, "You should be using <code>Math.random</code> to generate the random decimal number.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function randomFraction() {
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
return 0;
|
||||
|
||||
// Only change code above this line.
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,70 @@
|
||||
---
|
||||
id: cf1111c1c12feddfaeb1bdef
|
||||
title: Generate Random Whole Numbers with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript生成随机整数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们可以生成随机十进制数很好,但如果我们用它来生成随机整数,它会更有用。 <ol><li>使用<code>Math.random()</code>生成随机小数。 </li><li>将随机小数乘以<code>20</code> 。 </li><li>使用另一个函数<code>Math.floor()</code>将数字向下舍入到最接近的整数。 </li></ol>请记住, <code>Math.random()</code>永远不会返回<code>1</code> ,因为我们正在向下舍入,实际上不可能得到<code>20</code> 。这项技术将给我们一个<code>0</code>到<code>19</code>之间的整数。将所有内容放在一起,这就是我们的代码: <code>Math.floor(Math.random() * 20);</code>我们调用<code>Math.random()</code> ,将结果乘以20,然后将值传递给<code>Math.floor()</code>函数,将值向下舍入到最接近的整数。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用此技术生成并返回<code>0</code>到<code>9</code>之间的随机整数。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>randomWholeNum</code>的结果应该是整数。
|
||||
testString: 'assert(typeof randomWholeNum() === "number" && (function(){var r = randomWholeNum();return Math.floor(r) === r;})(), "The result of <code>randomWholeNum</code> should be a whole number.");'
|
||||
- text: 您应该使用<code>Math.random</code>来生成随机数。
|
||||
testString: 'assert(code.match(/Math.random/g).length > 1, "You should be using <code>Math.random</code> to generate a random number.");'
|
||||
- text: 您应该将<code>Math.random</code>的结果乘以10,使其成为介于0和9之间的数字。
|
||||
testString: 'assert(code.match(/\s*?Math.random\s*?\(\s*?\)\s*?\*\s*?10[\D]\s*?/g) || code.match(/\s*?10\s*?\*\s*?Math.random\s*?\(\s*?\)\s*?/g), "You should have multiplied the result of <code>Math.random</code> by 10 to make it a number that is between zero and nine.");'
|
||||
- text: 您应该使用<code>Math.floor</code>删除数字的小数部分。
|
||||
testString: 'assert(code.match(/Math.floor/g).length > 1, "You should use <code>Math.floor</code> to remove the decimal part of the number.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var randomNumberBetween0and19 = Math.floor(Math.random() * 20);
|
||||
|
||||
function randomWholeNum() {
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
return Math.random();
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,80 @@
|
||||
---
|
||||
id: cf1111c1c12feddfaeb2bdef
|
||||
title: Generate Random Whole Numbers within a Range
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 生成范围内的随机整数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">我们可以生成一个落在两个特定数字范围内的随机数,而不是像我们之前那样在零和给定数字之间生成一个随机数。为此,我们将定义最小数量<code>min</code>和最大数量<code>max</code> 。这是我们将使用的公式。花一点时间阅读它并尝试理解这段代码在做什么: <code>Math.floor(Math.random() * (max - min + 1)) + min</code> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个名为<code>randomRange</code>的函数,它接受一个范围<code>myMin</code>和<code>myMax</code>并返回一个大于或等于<code>myMin</code>的随机数,并且小于或等于<code>myMax</code> (包括<code>myMax</code> )。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>randomRange</code>可以生成的最低随机数应该等于你的最小数量<code>myMin</code> 。
|
||||
testString: 'assert(calcMin === 5, "The lowest random number that can be generated by <code>randomRange</code> should be equal to your minimum number, <code>myMin</code>.");'
|
||||
- text: <code>randomRange</code>可以生成的最高随机数应该等于最大数量<code>myMax</code> 。
|
||||
testString: 'assert(calcMax === 15, "The highest random number that can be generated by <code>randomRange</code> should be equal to your maximum number, <code>myMax</code>.");'
|
||||
- text: <code>randomRange</code>生成的随机数应该是整数,而不是小数。
|
||||
testString: 'assert(randomRange(0,1) % 1 === 0 , "The random number generated by <code>randomRange</code> should be an integer, not a decimal.");'
|
||||
- text: <code>randomRange</code>应该同时使用<code>myMax</code>和<code>myMin</code> ,并在你的范围内返回一个随机数。
|
||||
testString: 'assert((function(){if(code.match(/myMax/g).length > 1 && code.match(/myMin/g).length > 2 && code.match(/Math.floor/g) && code.match(/Math.random/g)){return true;}else{return false;}})(), "<code>randomRange</code> should use both <code>myMax</code> and <code>myMin</code>, and return a random number in your range.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
function ourRandomRange(ourMin, ourMax) {
|
||||
|
||||
return Math.floor(Math.random() * (ourMax - ourMin + 1)) + ourMin;
|
||||
}
|
||||
|
||||
ourRandomRange(1, 9);
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
function randomRange(myMin, myMax) {
|
||||
|
||||
return 0; // Change this line
|
||||
|
||||
}
|
||||
|
||||
// Change these values to test your function
|
||||
var myRandom = randomRange(5, 15);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,109 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244be
|
||||
title: Global Scope and Functions
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 全球范围和职能
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在JavaScript中, <dfn>范围</dfn>是指变量的可见性。在功能块之外定义的变量具有<dfn>全局</dfn>范围。这意味着,它们可以在JavaScript代码中随处可见。在没有<code>var</code>关键字的情况下使用的变量将在<code>global</code>范围内自动创建。这可能会在代码中的其他位置或再次运行函数时产生意外后果。您应该始终使用<code>var</code>声明变量。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>var</code> ,在任何函数之外声明一个<code>global</code>变量<code>myGlobal</code> 。使用值<code>10</code>初始化它。在函数<code>fun1</code>内部,在<strong><em>不</em></strong>使用<code>var</code>关键字的<strong><em>情况下</em></strong>为<code>oopsGlobal</code>分配<code>5</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 应该定义<code>myGlobal</code>
|
||||
testString: 'assert(typeof myGlobal != "undefined", "<code>myGlobal</code> should be defined");'
|
||||
- text: <code>myGlobal</code>的值应为<code>10</code>
|
||||
testString: 'assert(myGlobal === 10, "<code>myGlobal</code> should have a value of <code>10</code>");'
|
||||
- text: 应使用<code>var</code>关键字声明<code>myGlobal</code>
|
||||
testString: 'assert(/var\s+myGlobal/.test(code), "<code>myGlobal</code> should be declared using the <code>var</code> keyword");'
|
||||
- text: <code>oopsGlobal</code>应该是一个全局变量,其值为<code>5</code>
|
||||
testString: 'assert(typeof oopsGlobal != "undefined" && oopsGlobal === 5, "<code>oopsGlobal</code> should be a global variable and have a value of <code>5</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Declare your variable here
|
||||
|
||||
|
||||
function fun1() {
|
||||
// Assign 5 to oopsGlobal Here
|
||||
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
function fun2() {
|
||||
var output = "";
|
||||
if (typeof myGlobal != "undefined") {
|
||||
output += "myGlobal: " + myGlobal;
|
||||
}
|
||||
if (typeof oopsGlobal != "undefined") {
|
||||
output += " oopsGlobal: " + oopsGlobal;
|
||||
}
|
||||
console.log(output);
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
|
||||
```js
|
||||
var logOutput = "";
|
||||
var originalConsole = console
|
||||
function capture() {
|
||||
var nativeLog = console.log;
|
||||
console.log = function (message) {
|
||||
logOutput = message;
|
||||
if(nativeLog.apply) {
|
||||
nativeLog.apply(originalConsole, arguments);
|
||||
} else {
|
||||
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
|
||||
nativeLog(nativeMsg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function uncapture() {
|
||||
console.log = originalConsole.log;
|
||||
}
|
||||
var oopsGlobal;
|
||||
capture();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c0
|
||||
title: Global vs. Local Scope in Functions
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 功能中的全局与局部范围
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">可以使<dfn>本地</dfn>变量和<dfn>全局</dfn>变量具有相同的名称。执行此操作时, <code>local</code>变量优先于<code>global</code>变量。在这个例子中: <blockquote> var someVar =“帽子”; <br> function myFun(){ <br> var someVar =“Head”; <br>返回someVar; <br> } </blockquote>函数<code>myFun</code>将返回<code>"Head"</code>因为存在变量的<code>local</code>版本。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将一个局部变量添加到<code>myOutfit</code>函数,以使用<code>"sweater"</code>覆盖<code>outerWear</code>的值。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 不要更改全局<code>outerWear</code>的值
|
||||
testString: 'assert(outerWear === "T-Shirt", "Do not change the value of the global <code>outerWear</code>");'
|
||||
- text: <code>myOutfit</code>应该返回<code>"sweater"</code>
|
||||
testString: 'assert(myOutfit() === "sweater", "<code>myOutfit</code> should return <code>"sweater"</code>");'
|
||||
- text: 不要更改return语句
|
||||
testString: 'assert(/return outerWear/.test(code), "Do not change the return statement");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var outerWear = "T-Shirt";
|
||||
|
||||
function myOutfit() {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
return outerWear;
|
||||
}
|
||||
|
||||
myOutfit();
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 5664820f61c48e80c9fa476c
|
||||
title: Golf Code
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 高尔夫码
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在<a href="https://en.wikipedia.org/wiki/Golf" target="_blank">高尔夫</a>比赛中,每个洞都具有<code>par</code>意义,即高尔夫球手为了将球沉入洞中以完成比赛所期望的平均<code>strokes</code>次数。根据你的<code>strokes</code>高出或低于<code>par</code>的距离,有一个不同的昵称。您的函数将通过<code>par</code>和<code>strokes</code>参数。根据此表返回正确的字符串,该表按优先级顺序列出笔划;顶部(最高)到底部(最低): <table class="table table-striped"><thead><tr><th>笔画</th><th>返回</th></tr></thead><tbody><tr><td> 1 </td><td> “一杆进洞!” </td></tr><tr><td> <= par - 2 </td><td> “鹰” </td></tr><tr><td> par - 1 </td><td> “小鸟” </td></tr><tr><td>平价</td><td> “相提并论” </td></tr><tr><td> par + 1 </td><td> “柏忌” </td></tr><tr><td> par + 2 </td><td> “双柏忌” </td></tr><tr><td> > = par + 3 </td><td> “回家!” </td></tr></tbody></table> <code>par</code>和<code>strokes</code>将始终为数字和正数。为方便起见,我们添加了所有名称的数组。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>golfScore(4, 1)</code>应该返回“Hole-in-one!”'
|
||||
testString: 'assert(golfScore(4, 1) === "Hole-in-one!", "<code>golfScore(4, 1)</code> should return "Hole-in-one!"");'
|
||||
- text: '<code>golfScore(4, 2)</code>应该返回“Eagle”'
|
||||
testString: 'assert(golfScore(4, 2) === "Eagle", "<code>golfScore(4, 2)</code> should return "Eagle"");'
|
||||
- text: '<code>golfScore(5, 2)</code>应该返回“Eagle”'
|
||||
testString: 'assert(golfScore(5, 2) === "Eagle", "<code>golfScore(5, 2)</code> should return "Eagle"");'
|
||||
- text: '<code>golfScore(4, 3)</code>应该返回“Birdie”'
|
||||
testString: 'assert(golfScore(4, 3) === "Birdie", "<code>golfScore(4, 3)</code> should return "Birdie"");'
|
||||
- text: '<code>golfScore(4, 4)</code>应该返回“Par”'
|
||||
testString: 'assert(golfScore(4, 4) === "Par", "<code>golfScore(4, 4)</code> should return "Par"");'
|
||||
- text: '<code>golfScore(1, 1)</code>应该返回“Hole-in-one!”'
|
||||
testString: 'assert(golfScore(1, 1) === "Hole-in-one!", "<code>golfScore(1, 1)</code> should return "Hole-in-one!"");'
|
||||
- text: '<code>golfScore(5, 5)</code>应该返回“Par”'
|
||||
testString: 'assert(golfScore(5, 5) === "Par", "<code>golfScore(5, 5)</code> should return "Par"");'
|
||||
- text: '<code>golfScore(4, 5)</code>应该返回“Bogey”'
|
||||
testString: 'assert(golfScore(4, 5) === "Bogey", "<code>golfScore(4, 5)</code> should return "Bogey"");'
|
||||
- text: '<code>golfScore(4, 6)</code>应该返回“Double Bogey”'
|
||||
testString: 'assert(golfScore(4, 6) === "Double Bogey", "<code>golfScore(4, 6)</code> should return "Double Bogey"");'
|
||||
- text: '<code>golfScore(4, 7)</code>应该返回“Go Home!”'
|
||||
testString: 'assert(golfScore(4, 7) === "Go Home!", "<code>golfScore(4, 7)</code> should return "Go Home!"");'
|
||||
- text: '<code>golfScore(5, 9)</code>应该返回“Go Home!”'
|
||||
testString: 'assert(golfScore(5, 9) === "Go Home!", "<code>golfScore(5, 9)</code> should return "Go Home!"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
|
||||
function golfScore(par, strokes) {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
return "Change Me";
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
// Change these values to test
|
||||
golfScore(5, 4);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ac
|
||||
title: Increment a Number with JavaScript
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript增加数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以轻松地<dfn>增加</dfn>或添加一个变量与<code>++</code>运算符。 <code>i++;</code>相当于<code>i = i + 1;</code> <strong>注意</strong> <br>整行成为<code>i++;</code> ,消除了对等号的需要。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改代码以在<code>myVar</code>上使用<code>++</code>运算符。 <strong>暗示</strong> <br>了解有关<a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment_()" target="_blank">算术运算符的</a>更多信息<a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment_()" target="_blank">- 增量(++)</a> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myVar</code>应该等于<code>88</code>
|
||||
testString: 'assert(myVar === 88, "<code>myVar</code> should equal <code>88</code>");'
|
||||
- text: <code>myVar = myVar + 1;</code>应该改变
|
||||
testString: 'assert(/var\s*myVar\s*=\s*87;\s*\/*.*\s*([+]{2}\s*myVar|myVar\s*[+]{2});/.test(code), "<code>myVar = myVar + 1;</code> should be changed");'
|
||||
- text: 使用<code>++</code>运算符
|
||||
testString: 'assert(/[+]{2}\s*myVar|myVar\s*[+]{2}/.test(code), "Use the <code>++</code> operator");'
|
||||
- text: 不要更改行上方的代码
|
||||
testString: 'assert(/var myVar = 87;/.test(code), "Do not change code above the line");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
var myVar = 87;
|
||||
|
||||
// Only change code below this line
|
||||
myVar = myVar + 1;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,60 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244a9
|
||||
title: Initializing Variables with the Assignment Operator
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用赋值运算符初始化变量
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">通常将变量<dfn>初始化</dfn>为与声明的同一行中的初始值。 <code>var myVar = 0;</code>创建一个名为<code>myVar</code>的新变量,并为其指定初始值<code>0</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>var</code>定义变量<code>a</code>并将其初始化为值<code>9</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 将<code>a</code>初始化为值<code>9</code>
|
||||
testString: 'assert(/var\s+a\s*=\s*9\s*/.test(code), "Initialize <code>a</code> to a value of <code>9</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourVar = 19;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244db
|
||||
title: Introducing Else If Statements
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 如果声明引入Else
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果您有多个需要解决的条件,可以将<code>if</code>语句与<code>else if</code>语句链接在一起。 <blockquote> if(num> 15){ <br>返回“大于15”; <br> } else if(num <5){ <br>返回“小于5”; <br> } else { <br>返回“5到15之间”; <br> } </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">转换逻辑以使用<code>else if</code>语句。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该至少有两个<code>else</code>语句
|
||||
testString: 'assert(code.match(/else/g).length > 1, "You should have at least two <code>else</code> statements");'
|
||||
- text: 你应该至少有两个<code>if</code>语句
|
||||
testString: 'assert(code.match(/if/g).length > 1, "You should have at least two <code>if</code> statements");'
|
||||
- text: 您应该为每个条件关闭并打开花括号
|
||||
testString: 'assert(code.match(/if\s*\((.+)\)\s*\{[\s\S]+\}\s*else if\s*\((.+)\)\s*\{[\s\S]+\}\s*else\s*\{[\s\S]+\s*\}/), "You should have closing and opening curly braces for each condition in your if else statement");'
|
||||
- text: <code>testElseIf(0)</code>应返回“小于5”
|
||||
testString: 'assert(testElseIf(0) === "Smaller than 5", "<code>testElseIf(0)</code> should return "Smaller than 5"");'
|
||||
- text: <code>testElseIf(5)</code>应该返回“5到10之间”
|
||||
testString: 'assert(testElseIf(5) === "Between 5 and 10", "<code>testElseIf(5)</code> should return "Between 5 and 10"");'
|
||||
- text: <code>testElseIf(7)</code>应返回“5到10之间”
|
||||
testString: 'assert(testElseIf(7) === "Between 5 and 10", "<code>testElseIf(7)</code> should return "Between 5 and 10"");'
|
||||
- text: <code>testElseIf(10)</code>应返回“5到10之间”
|
||||
testString: 'assert(testElseIf(10) === "Between 5 and 10", "<code>testElseIf(10)</code> should return "Between 5 and 10"");'
|
||||
- text: <code>testElseIf(12)</code>应返回“大于10”
|
||||
testString: 'assert(testElseIf(12) === "Greater than 10", "<code>testElseIf(12)</code> should return "Greater than 10"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testElseIf(val) {
|
||||
if (val > 10) {
|
||||
return "Greater than 10";
|
||||
}
|
||||
|
||||
if (val < 5) {
|
||||
return "Smaller than 5";
|
||||
}
|
||||
|
||||
return "Between 5 and 10";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testElseIf(7);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244da
|
||||
title: Introducing Else Statements
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 介绍其他声明
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">当<code>if</code>语句的条件为真时,将执行其后面的代码块。当那个条件是假的时候怎么办?通常什么都不会发生。使用<code>else</code>语句,可以执行备用代码块。 <blockquote> if(num> 10){ <br>返回“大于10”; <br> } else { <br>返回“10或更少”; <br> } </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>if</code>语句组合到单个<code>if/else</code>语句中。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您应该只在编辑器中有一个<code>if</code>语句
|
||||
testString: 'assert(code.match(/if/g).length === 1, "You should only have one <code>if</code> statement in the editor");'
|
||||
- text: 你应该使用<code>else</code>语句
|
||||
testString: 'assert(/else/g.test(code), "You should use an <code>else</code> statement");'
|
||||
- text: <code>testElse(4)</code>应返回“5或更小”
|
||||
testString: 'assert(testElse(4) === "5 or Smaller", "<code>testElse(4)</code> should return "5 or Smaller"");'
|
||||
- text: <code>testElse(5)</code>应返回“5或更小”
|
||||
testString: 'assert(testElse(5) === "5 or Smaller", "<code>testElse(5)</code> should return "5 or Smaller"");'
|
||||
- text: <code>testElse(6)</code>应该返回“大于5”
|
||||
testString: 'assert(testElse(6) === "Bigger than 5", "<code>testElse(6)</code> should return "Bigger than 5"");'
|
||||
- text: <code>testElse(10)</code>应该返回“大于5”
|
||||
testString: 'assert(testElse(10) === "Bigger than 5", "<code>testElse(10)</code> should return "Bigger than 5"");'
|
||||
- text: 请勿更改行上方或下方的代码。
|
||||
testString: 'assert(/var result = "";/.test(code) && /return result;/.test(code), "Do not change the code above or below the lines.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function testElse(val) {
|
||||
var result = "";
|
||||
// Only change code below this line
|
||||
|
||||
if (val > 5) {
|
||||
result = "Bigger than 5";
|
||||
}
|
||||
|
||||
if (val <= 5) {
|
||||
result = "5 or Smaller";
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
return result;
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testElse(4);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,69 @@
|
||||
---
|
||||
id: 56104e9e514f539506016a5c
|
||||
title: Iterate Odd Numbers With a For Loop
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用For循环迭代奇数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> For循环不必一次迭代一个循环。通过改变我们的<code>final-expression</code> ,我们可以计算偶数。我们将从<code>i = 0</code>开始并在<code>i < 10</code>循环。我们会增加<code>i</code>的2每个回路与<code>i += 2</code> 。 <blockquote> var ourArray = []; <br> for(var i = 0; i <10; i + = 2){ <br> ourArray.push(ⅰ); <br> } </blockquote> <code>ourArray</code>现在包含<code>[0,2,4,6,8]</code> 。让我们改变<code>initialization</code>这样我们就可以用奇数来计算。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>for</code>循环将奇数从1到9推送到<code>myArray</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该为此使用<code>for</code>循环。
|
||||
testString: 'assert(code.match(/for\s*\(/g).length > 1, "You should be using a <code>for</code> loop for this.");'
|
||||
- text: '<code>myArray</code>应该等于<code>[1,3,5,7,9]</code> 。'
|
||||
testString: 'assert.deepEqual(myArray, [1,3,5,7,9], "<code>myArray</code> should equal <code>[1,3,5,7,9]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [];
|
||||
|
||||
for (var i = 0; i < 10; i += 2) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 5675e877dbd60be8ad28edc6
|
||||
title: Iterate Through an Array with a For Loop
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用For循环遍历数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> JavaScript中的一个常见任务是遍历数组的内容。一种方法是使用<code>for</code>循环。此代码将数组<code>arr</code>每个元素输出到控制台: <blockquote> var arr = [10,9,8,7,6]; <br> for(var i = 0; i <arr.length; i ++){ <br> (ARR [I])的console.log; <br> } </blockquote>请记住,数组具有从零开始的编号,这意味着数组的最后一个索引是长度 - 1.我们对此循环的<dfn>条件</dfn>是<code>i < arr.length</code> ,当<code>i</code>长度为1时停止。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">声明并将变量<code>total</code>初始化为<code>0</code> 。使用<code>for</code>循环将<code>myArr</code>数组的每个元素的值添加到<code>total</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 应声明<code>total</code>并初始化为0
|
||||
testString: 'assert(code.match(/var.*?total\s*=\s*0.*?;/), "<code>total</code> should be declared and initialized to 0");'
|
||||
- text: <code>total</code>应该等于20
|
||||
testString: 'assert(total === 20, "<code>total</code> should equal 20");'
|
||||
- text: 您应该使用<code>for</code>循环来遍历<code>myArr</code>
|
||||
testString: 'assert(code.match(/for\s*\(/g).length > 1 && code.match(/myArr\s*\[/), "You should use a <code>for</code> loop to iterate through <code>myArr</code>");'
|
||||
- text: 不要直接将<code>total</code>设置为20
|
||||
testString: 'assert(!code.match(/total[\s\+\-]*=\s*(\d(?!\s*[;,])|[1-9])/g), "Do not set <code>total</code> to 20 directly");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArr = [ 9, 10, 11, 12];
|
||||
var ourTotal = 0;
|
||||
|
||||
for (var i = 0; i < ourArr.length; i++) {
|
||||
ourTotal += ourArr[i];
|
||||
}
|
||||
|
||||
// Setup
|
||||
var myArr = [ 2, 3, 4, 5, 6];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,70 @@
|
||||
---
|
||||
id: 5a2efd662fb457916e1fe604
|
||||
title: Iterate with JavaScript Do...While Loops
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript迭代...循环
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以使用循环多次运行相同的代码。你将学习的下一个类型的循环称为“ <code>do...while</code> ”循环,因为它首先将“ <code>do</code> ”循环内部代码的一次传递,无论如何,然后它运行“ <code>while</code> ”指定条件为真一旦这种情况不再真实就停止。我们来看一个例子。 <blockquote> var ourArray = []; <br> var i = 0; <br>做{ <br> ourArray.push(ⅰ); <br>我++; <br> } while(i <5); </blockquote>这与任何其他类型的循环一样正常,结果数组看起来像<code>[0, 1, 2, 3, 4]</code> 。然而,什么使得<code>do...while</code>与其他循环不同,但是当条件在第一次检查时失败时它的行为如何。让我们看看这个在行动。这是一个常规的while循环,只要<code>i < 5</code> ,它就会在循环中运行代码。 <blockquote> var ourArray = []; <br> var i = 5; <br>而(i <5){ <br> ourArray.push(ⅰ); <br>我++; <br> } </blockquote>请注意,我们将<code>i</code>的值初始化为5.当我们执行下一行时,我们注意到<code>i</code>不小于5.所以我们不执行循环内的代码。结果是<code>ourArray</code>最终没有添加任何内容,因此当上面示例中的所有代码完成运行时,它仍然看起来像这个<code>[]</code> 。现在,看一下<code>do...while</code>循环。 <blockquote> var ourArray = []; <br> var i = 5; <br>做{ <br> ourArray.push(ⅰ); <br>我++; <br> } while(i <5); </blockquote>在这种情况下,我们将<code>i</code>的值初始化为5,就像我们使用while循环一样。当我们到达下一行时,没有检查<code>i</code>的值,所以我们转到花括号内的代码并执行它。我们将在数组中添加一个元素并在进行条件检查之前递增<code>i</code> 。然后,当我们检查<code>i < 5</code>看到<code>i</code>现在是6,这不符合条件检查。所以我们退出循环并完成。在上面的例子的末尾, <code>ourArray</code>的值是<code>[5]</code> 。本质上, <code>do...while</code>循环确保循环内的代码至少运行一次。让我们尝试通过将值推送到数组来实现<code>do...while</code>循环。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将代码中的<code>while</code>循环更改为<code>do...while</code>循环,以便循环将数字10推送到<code>myArray</code> ,当代码完成运行时, <code>i</code>将等于<code>11</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>do...while</code>循环。
|
||||
testString: 'assert(code.match(/do/g), "You should be using a <code>do...while</code> loop for this.");'
|
||||
- text: '<code>myArray</code>应该等于<code>[10]</code> 。'
|
||||
testString: 'assert.deepEqual(myArray, [10], "<code>myArray</code> should equal <code>[10]</code>.");'
|
||||
- text: <code>i</code>应该等于<code>11</code>
|
||||
testString: 'assert.deepEqual(i, 11, "<code>i</code> should equal <code>11</code>");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [];
|
||||
var i = 10;
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
while (i < 5) {
|
||||
myArray.push(i);
|
||||
i++;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,69 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb5bdef
|
||||
title: Iterate with JavaScript For Loops
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用JavaScript迭代循环
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以使用循环多次运行相同的代码。最常见的JavaScript循环类型称为“ <code>for loop</code> ”,因为它“运行”特定次数。 For循环用三个可选表达式声明,用分号分隔: <code>for ([initialization]; [condition]; [final-expression])</code> <code>initialization</code>语句仅在循环开始之前执行一次。它通常用于定义和设置循环变量。 <code>condition</code>语句在每次循环迭代开始时进行计算,并且只要计算结果为<code>true</code>就会继续。当迭代开始时<code>condition</code>为<code>false</code>时,循环将停止执行。这意味着如果<code>condition</code>以<code>false</code>开头,则循环将永远不会执行。 <code>final-expression</code>在每次循环迭代结束时执行,在下一次<code>condition</code>检查之前执行,通常用于递增或递减循环计数器。在下面的示例中,我们使用<code>i = 0</code>初始化并迭代,而条件<code>i < 5</code>为真。我们将在每个循环迭代中将<code>i</code>递增<code>1</code> ,并使用<code>i++</code>作为<code>final-expression</code> 。 <blockquote> var ourArray = []; <br> for(var i = 0; i <5; i ++){ <br> ourArray.push(ⅰ); <br> } </blockquote> <code>ourArray</code>现在包含<code>[0,1,2,3,4]</code> 。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>for</code>循环将值1到5推送到<code>myArray</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该为此使用<code>for</code>循环。
|
||||
testString: 'assert(code.match(/for\s*\(/g).length > 1, "You should be using a <code>for</code> loop for this.");'
|
||||
- text: '<code>myArray</code>应该等于<code>[1,2,3,4,5]</code> 。'
|
||||
testString: 'assert.deepEqual(myArray, [1,2,3,4,5], "<code>myArray</code> should equal <code>[1,2,3,4,5]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [];
|
||||
|
||||
for (var i = 0; i < 5; i++) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,62 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb1bdef
|
||||
title: Iterate with JavaScript While Loops
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 在循环时使用JavaScript进行迭代
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以使用循环多次运行相同的代码。我们将学习的第一种类型的循环称为“ <code>while</code> ”循环,因为它在“while”运行时指定的条件为true,并且一旦该条件不再为真就停止。 <blockquote> var ourArray = []; <br> var i = 0; <br>而(i <5){ <br> ourArray.push(ⅰ); <br>我++; <br> } </blockquote>让我们尝试通过将值推送到数组来实现while循环。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>while</code>循环将数字0到4推送到<code>myArray</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>while</code>循环。
|
||||
testString: 'assert(code.match(/while/g), "You should be using a <code>while</code> loop for this.");'
|
||||
- text: '<code>myArray</code>应该等于<code>[0,1,2,3,4]</code> 。'
|
||||
testString: 'assert.deepEqual(myArray, [0,1,2,3,4], "<code>myArray</code> should equal <code>[0,1,2,3,4]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,96 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244bf
|
||||
title: Local Scope and Functions
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 本地范围和功能
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在函数内声明的变量,以及函数参数都具有<dfn>局部</dfn>范围。这意味着,它们仅在该功能中可见。这是一个函数<code>myTest</code>带有一个名为<code>loc</code>的局部变量。 <blockquote> function myTest(){ <br> var loc =“foo”; <br>的console.log(LOC); <br> } <br> MYTEST(); //记录“foo” <br>的console.log(LOC); // loc未定义</blockquote> <code>loc</code>未在函数外定义。 </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在<code>myLocalScope</code>声明一个局部变量<code>myVar</code> 。运行测试,然后按照编辑器中注释的说明进行操作。 <strong>暗示</strong> <br>如果您遇到问题,刷新页面可能会有所帮助。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 没有全局<code>myVar</code>变量
|
||||
testString: 'assert(typeof myVar === "undefined", "No global <code>myVar</code> variable");'
|
||||
- text: 添加本地<code>myVar</code>变量
|
||||
testString: 'assert(/var\s+myVar/.test(code), "Add a local <code>myVar</code> variable");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function myLocalScope() {
|
||||
'use strict'; // you shouldn't need to edit this line
|
||||
|
||||
console.log(myVar);
|
||||
}
|
||||
myLocalScope();
|
||||
|
||||
// Run and check the console
|
||||
// myVar is not defined outside of myLocalScope
|
||||
console.log(myVar);
|
||||
|
||||
// Now remove the console log line to pass the test
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
|
||||
```js
|
||||
var logOutput = "";
|
||||
var originalConsole = console
|
||||
function capture() {
|
||||
var nativeLog = console.log;
|
||||
console.log = function (message) {
|
||||
logOutput = message;
|
||||
if(nativeLog.apply) {
|
||||
nativeLog.apply(originalConsole, arguments);
|
||||
} else {
|
||||
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
|
||||
nativeLog(nativeMsg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function uncapture() {
|
||||
console.log = originalConsole.log;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 5690307fddb111c6084545d7
|
||||
title: Logical Order in If Else Statements
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 如果其他陈述中的逻辑顺序
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">订单在<code>if</code> , <code>else if</code>语句中很重要。该函数从上到下执行,因此您需要注意首先出现的语句。以这两个函数为例。这是第一个: <blockquote> function foo(x){ <br> if(x <1){ <br>返回“少于一个”; <br> } else if(x <2){ <br>返回“少于两个”; <br> } else { <br>返回“大于或等于2”; <br> } <br> } </blockquote>第二个只是切换语句的顺序: <blockquote>功能栏(x){ <br> if(x <2){ <br>返回“少于两个”; <br> } else if(x <1){ <br>返回“少于一个”; <br> } else { <br>返回“大于或等于2”; <br> } <br> } </blockquote>虽然如果我们将数字传递给两者,这两个函数看起来几乎相同但我们得到不同的输出。 <blockquote> foo(0)//“不到一个” <br> bar(0)//“少于两个” </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改函数中的逻辑顺序,以便在所有情况下都返回正确的语句。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>orderMyLogic(4)</code>应返回“小于5”
|
||||
testString: 'assert(orderMyLogic(4) === "Less than 5", "<code>orderMyLogic(4)</code> should return "Less than 5"");'
|
||||
- text: <code>orderMyLogic(6)</code>应该返回“少于10”
|
||||
testString: 'assert(orderMyLogic(6) === "Less than 10", "<code>orderMyLogic(6)</code> should return "Less than 10"");'
|
||||
- text: <code>orderMyLogic(11)</code>应该返回“大于或等于10”
|
||||
testString: 'assert(orderMyLogic(11) === "Greater than or equal to 10", "<code>orderMyLogic(11)</code> should return "Greater than or equal to 10"");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function orderMyLogic(val) {
|
||||
if (val < 10) {
|
||||
return "Less than 10";
|
||||
} else if (val < 5) {
|
||||
return "Less than 5";
|
||||
} else {
|
||||
return "Greater than or equal to 10";
|
||||
}
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
orderMyLogic(7);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,70 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392cc
|
||||
title: Manipulate Arrays With pop()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用pop()操作数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">更改数组中数据的另一种方法是使用<code>.pop()</code>函数。 <code>.pop()</code>用于“弹出”数组末尾的值。我们可以通过将其赋值给变量来存储这个“弹出”值。换句话说, <code>.pop()</code>从数组中删除最后一个元素并返回该元素。任何类型的条目都可以从数组“弹出” - 数字,字符串,甚至嵌套数组。 <blockquote> <code>var threeArr = [1, 4, 6]; <br> var oneDown = threeArr.pop(); <br> console.log(oneDown); // Returns 6 <br> console.log(threeArr); // Returns [1, 4]</code> </blockquote> </section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>.pop()</code>函数从<code>myArray</code>删除最后一项,将“弹出”值分配给<code>removedFromMyArray</code> 。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>myArray</code>应该只包含<code>[["John", 23]]</code> 。'
|
||||
testString: 'assert((function(d){if(d[0][0] == "John" && d[0][1] === 23 && d[1] == undefined){return true;}else{return false;}})(myArray), "<code>myArray</code> should only contain <code>[["John", 23]]</code>.");'
|
||||
- text: 在<code>myArray</code>上使用<code>pop()</code>
|
||||
testString: 'assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code), "Use <code>pop()</code> on <code>myArray</code>");'
|
||||
- text: '<code>removedFromMyArray</code>应该只包含<code>["cat", 2]</code> 。'
|
||||
testString: 'assert((function(d){if(d[0] == "cat" && d[1] === 2 && d[2] == undefined){return true;}else{return false;}})(removedFromMyArray), "<code>removedFromMyArray</code> should only contain <code>["cat", 2]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [1,2,3];
|
||||
var removedFromOurArray = ourArray.pop();
|
||||
// removedFromOurArray now equals 3, and ourArray now equals [1,2]
|
||||
|
||||
// Setup
|
||||
var myArray = [["John", 23], ["cat", 2]];
|
||||
|
||||
// Only change code below this line.
|
||||
var removedFromMyArray;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392cb
|
||||
title: Manipulate Arrays With push()
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 用push()操纵数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">将数据附加到数组末尾的简单方法是通过<code>push()</code>函数。 <code>.push()</code>接受一个或多个<dfn>参数</dfn>并将它们“推”到数组的末尾。 <blockquote> var arr = [1,2,3]; <br> arr.push(4); <br> // arr现在是[1,2,3,4] </blockquote></section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">将<code>["dog", 3]</code>推到<code>myArray</code>变量的末尾。 </section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>myArray</code>现在应该等于<code>[["John", 23], ["cat", 2], ["dog", 3]]</code> 。'
|
||||
testString: 'assert((function(d){if(d[2] != undefined && d[0][0] == "John" && d[0][1] === 23 && d[2][0] == "dog" && d[2][1] === 3 && d[2].length == 2){return true;}else{return false;}})(myArray), "<code>myArray</code> should now equal <code>[["John", 23], ["cat", 2], ["dog", 3]]</code>.");'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = ["Stimpson", "J", "cat"];
|
||||
ourArray.push(["happy", "joy"]);
|
||||
// ourArray now equals ["Stimpson", "J", "cat", ["happy", "joy"]]
|
||||
|
||||
// Setup
|
||||
var myArray = [["John", 23], ["cat", 2]];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
```
|
||||
</section>
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user