chore(learn): Applied MDX format to Chinese curriculum files (#40462)
This commit is contained in:
@ -1,77 +1,75 @@
|
||||
---
|
||||
id: a77dbc43c33f39daa4429b4f
|
||||
title: 嘘谁
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 嘘谁
|
||||
---
|
||||
|
||||
## 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>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
检查参数是否归类为布尔基元。返回true或false。布尔基元是true和false。如果卡住,请记得使用[Read-Search-Ask](http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514) 。尝试配对程序。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>booWho(true)</code>应该返回true。
|
||||
testString: assert.strictEqual(booWho(true), true);
|
||||
- text: <code>booWho(false)</code>应该返回true。
|
||||
testString: assert.strictEqual(booWho(false), true);
|
||||
- text: '<code>booWho([1, 2, 3])</code>应该返回false。'
|
||||
testString: assert.strictEqual(booWho([1, 2, 3]), false);
|
||||
- text: '<code>booWho([].slice)</code>应该返回false。'
|
||||
testString: assert.strictEqual(booWho([].slice), false);
|
||||
- text: '<code>booWho({ "a": 1 })</code>应该返回false。'
|
||||
testString: 'assert.strictEqual(booWho({ "a": 1 }), false);'
|
||||
- text: <code>booWho(1)</code>应该返回false。
|
||||
testString: assert.strictEqual(booWho(1), false);
|
||||
- text: <code>booWho(NaN)</code>应该返回false。
|
||||
testString: assert.strictEqual(booWho(NaN), false);
|
||||
- text: <code>booWho("a")</code>应该返回false。
|
||||
testString: assert.strictEqual(booWho("a"), false);
|
||||
- text: <code>booWho("true")</code>应该返回false。
|
||||
testString: assert.strictEqual(booWho("true"), false);
|
||||
- text: <code>booWho("false")</code>应该返回false。
|
||||
testString: assert.strictEqual(booWho("false"), false);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`booWho(true)`应该返回true。
|
||||
|
||||
```js
|
||||
function booWho(bool) {
|
||||
// What is the new fad diet for ghost developers? The Boolean.
|
||||
return bool;
|
||||
}
|
||||
|
||||
booWho(null);
|
||||
|
||||
assert.strictEqual(booWho(true), true);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`booWho(false)`应该返回true。
|
||||
|
||||
```js
|
||||
function booWho(bool) {
|
||||
return typeof bool === "boolean";
|
||||
}
|
||||
|
||||
booWho(null);
|
||||
assert.strictEqual(booWho(false), true);
|
||||
```
|
||||
|
||||
/section>
|
||||
`booWho([1, 2, 3])`应该返回false。
|
||||
|
||||
```js
|
||||
assert.strictEqual(booWho([1, 2, 3]), false);
|
||||
```
|
||||
|
||||
`booWho([].slice)`应该返回false。
|
||||
|
||||
```js
|
||||
assert.strictEqual(booWho([].slice), false);
|
||||
```
|
||||
|
||||
`booWho({ "a": 1 })`应该返回false。
|
||||
|
||||
```js
|
||||
assert.strictEqual(booWho({ a: 1 }), false);
|
||||
```
|
||||
|
||||
`booWho(1)`应该返回false。
|
||||
|
||||
```js
|
||||
assert.strictEqual(booWho(1), false);
|
||||
```
|
||||
|
||||
`booWho(NaN)`应该返回false。
|
||||
|
||||
```js
|
||||
assert.strictEqual(booWho(NaN), false);
|
||||
```
|
||||
|
||||
`booWho("a")`应该返回false。
|
||||
|
||||
```js
|
||||
assert.strictEqual(booWho('a'), false);
|
||||
```
|
||||
|
||||
`booWho("true")`应该返回false。
|
||||
|
||||
```js
|
||||
assert.strictEqual(booWho('true'), false);
|
||||
```
|
||||
|
||||
`booWho("false")`应该返回false。
|
||||
|
||||
```js
|
||||
assert.strictEqual(booWho('false'), false);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,67 +1,84 @@
|
||||
---
|
||||
id: a9bd25c716030ec90084d8a1
|
||||
title: 矮胖的猴子
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 矮胖的猴子
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">编写一个函数,将数组(第一个参数)拆分为<code>size</code>的长度(第二个参数),并将它们作为二维数组返回。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
编写一个函数,将数组(第一个参数)拆分为`size`的长度(第二个参数),并将它们作为二维数组返回。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```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"]]);
|
||||
- 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]]);
|
||||
- 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]]);
|
||||
- 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]]);
|
||||
- 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]]);
|
||||
- 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]]);
|
||||
- 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]]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`chunkArrayInGroups(["a", "b", "c", "d"], 2)`应返回`[["a", "b"], ["c", "d"]]` 。
|
||||
|
||||
```js
|
||||
function chunkArrayInGroups(arr, size) {
|
||||
// Break it up.
|
||||
return arr;
|
||||
}
|
||||
|
||||
chunkArrayInGroups(["a", "b", "c", "d"], 2);
|
||||
|
||||
assert.deepEqual(chunkArrayInGroups(['a', 'b', 'c', 'd'], 2), [
|
||||
['a', 'b'],
|
||||
['c', 'd']
|
||||
]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3)`应返回`[[0, 1, 2], [3, 4, 5]]` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3), [
|
||||
[0, 1, 2],
|
||||
[3, 4, 5]
|
||||
]);
|
||||
```
|
||||
|
||||
/section>
|
||||
`chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2)`应返回`[[0, 1], [2, 3], [4, 5]]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2), [
|
||||
[0, 1],
|
||||
[2, 3],
|
||||
[4, 5]
|
||||
]);
|
||||
```
|
||||
|
||||
`chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4)`应该返回`[[0, 1, 2, 3], [4, 5]]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4), [
|
||||
[0, 1, 2, 3],
|
||||
[4, 5]
|
||||
]);
|
||||
```
|
||||
|
||||
`chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3)`应该返回`[[0, 1, 2], [3, 4, 5], [6]]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3), [
|
||||
[0, 1, 2],
|
||||
[3, 4, 5],
|
||||
[6]
|
||||
]);
|
||||
```
|
||||
|
||||
`chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4)`应返回`[[0, 1, 2, 3], [4, 5, 6, 7], [8]]` `chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4)` `[[0, 1, 2, 3], [4, 5, 6, 7], [8]]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4), [
|
||||
[0, 1, 2, 3],
|
||||
[4, 5, 6, 7],
|
||||
[8]
|
||||
]);
|
||||
```
|
||||
|
||||
`chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2)`应返回`[[0, 1], [2, 3], [4, 5], [6, 7], [8]]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2), [
|
||||
[0, 1],
|
||||
[2, 3],
|
||||
[4, 5],
|
||||
[6, 7],
|
||||
[8]
|
||||
]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,76 +1,91 @@
|
||||
---
|
||||
id: acda2fb1324d9b0fa741e6b5
|
||||
title: 确认结束
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 确认结束
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">检查字符串(第一个参数<code>str</code> )是否以给定的目标字符串(第二个参数, <code>target</code> )结束。这个挑战<em>可以</em>通过<code>.endsWith()</code>中引入的<code>.endsWith()</code>方法来解决。但是出于这个挑战的目的,我们希望您使用其中一个JavaScript子字符串方法。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
检查字符串(第一个参数`str` )是否以给定的目标字符串(第二个参数, `target` )结束。这个挑战*可以*通过`.endsWith()`中引入的`.endsWith()`方法来解决。但是出于这个挑战的目的,我们希望您使用其中一个JavaScript子字符串方法。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>confirmEnding("Bastian", "n")</code>应该返回true。'
|
||||
testString: assert(confirmEnding("Bastian", "n") === true);
|
||||
- text: '<code>confirmEnding("Congratulation", "on")</code>应该返回true。'
|
||||
testString: assert(confirmEnding("Congratulation", "on") === true);
|
||||
- text: '<code>confirmEnding("Connor", "n")</code>应返回false。'
|
||||
testString: assert(confirmEnding("Connor", "n") === 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);
|
||||
- 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);
|
||||
- text: '<code>confirmEnding("Open sesame", "same")</code>应该返回true。'
|
||||
testString: assert(confirmEnding("Open sesame", "same") === true);
|
||||
- text: '<code>confirmEnding("Open sesame", "pen")</code>应该返回false。'
|
||||
testString: assert(confirmEnding("Open sesame", "pen") === false);
|
||||
- text: '<code>confirmEnding("Open sesame", "game")</code>应该返回false。'
|
||||
testString: assert(confirmEnding("Open sesame", "game") === 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);
|
||||
- text: '<code>confirmEnding("Abstraction", "action")</code>应该返回true。'
|
||||
testString: assert(confirmEnding("Abstraction", "action") === true);
|
||||
- text: 不要使用内置方法<code>.endsWith()</code>来解决挑战。
|
||||
testString: assert(!(/\.endsWith\(.*?\)\s*?;?/.test(code)) && !(/\['endsWith'\]/.test(code)));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`confirmEnding("Bastian", "n")`应该返回true。
|
||||
|
||||
```js
|
||||
function confirmEnding(str, target) {
|
||||
// "Never give up and good luck will find you."
|
||||
// -- Falcor
|
||||
return str;
|
||||
}
|
||||
|
||||
confirmEnding("Bastian", "n");
|
||||
|
||||
assert(confirmEnding('Bastian', 'n') === true);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`confirmEnding("Congratulation", "on")`应该返回true。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(confirmEnding('Congratulation', 'on') === true);
|
||||
```
|
||||
|
||||
/section>
|
||||
`confirmEnding("Connor", "n")`应返回false。
|
||||
|
||||
```js
|
||||
assert(confirmEnding('Connor', 'n') === false);
|
||||
```
|
||||
|
||||
`confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification")`应该返回false。
|
||||
|
||||
```js
|
||||
assert(
|
||||
confirmEnding(
|
||||
'Walking on water and developing software from a specification are easy if both are frozen',
|
||||
'specification'
|
||||
) === false
|
||||
);
|
||||
```
|
||||
|
||||
`confirmEnding("He has to give me a new name", "name")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(confirmEnding('He has to give me a new name', 'name') === true);
|
||||
```
|
||||
|
||||
`confirmEnding("Open sesame", "same")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(confirmEnding('Open sesame', 'same') === true);
|
||||
```
|
||||
|
||||
`confirmEnding("Open sesame", "pen")`应该返回false。
|
||||
|
||||
```js
|
||||
assert(confirmEnding('Open sesame', 'pen') === false);
|
||||
```
|
||||
|
||||
`confirmEnding("Open sesame", "game")`应该返回false。
|
||||
|
||||
```js
|
||||
assert(confirmEnding('Open sesame', 'game') === false);
|
||||
```
|
||||
|
||||
`confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain")`应该返回虚假。
|
||||
|
||||
```js
|
||||
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
|
||||
);
|
||||
```
|
||||
|
||||
`confirmEnding("Abstraction", "action")`应该返回true。
|
||||
|
||||
```js
|
||||
assert(confirmEnding('Abstraction', 'action') === true);
|
||||
```
|
||||
|
||||
不要使用内置方法`.endsWith()`来解决挑战。
|
||||
|
||||
```js
|
||||
assert(!/\.endsWith\(.*?\)\s*?;?/.test(code) && !/\['endsWith'\]/.test(code));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,71 +1,51 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b3
|
||||
title: 将摄氏温度转换为华氏温度
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
title: 将摄氏温度转换为华氏温度
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">从摄氏温度转换为华氏温度的算法是以摄氏度乘以<code>9/5</code>,再加上<code>32</code> 。您将获得一个参数<code>celsius</code>代表着摄氏温度。使用已准备好代表华氏温度的变量<code>fahrenheit</code>,将<code>celsius</code>摄氏温度变量值兑换成华氏温度值,然后存储在<code>farenheit</code>变量里。使用以上提到的算法将摄氏温度转换为华氏温度。不需要过多担心函数和返回语句,因为它们将会在未来的挑战中加以解释。目前,只需使用您已经学过的运算符。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
从摄氏温度转换为华氏温度的算法是以摄氏度乘以`9/5`,再加上`32` 。您将获得一个参数`celsius`代表着摄氏温度。使用已准备好代表华氏温度的变量`fahrenheit`,将`celsius`摄氏温度变量值兑换成华氏温度值,然后存储在`farenheit`变量里。使用以上提到的算法将摄氏温度转换为华氏温度。不需要过多担心函数和返回语句,因为它们将会在未来的挑战中加以解释。目前,只需使用您已经学过的运算符。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>convertToF(0)</code>应该返回一个数字
|
||||
testString: assert(typeof convertToF(0) === 'number');
|
||||
- text: <code>convertToF(-30)</code>应该返回值<code>-22</code>
|
||||
testString: assert(convertToF(-30) === -22);
|
||||
- text: <code>convertToF(-10)</code>应该返回值<code>14</code>
|
||||
testString: assert(convertToF(-10) === 14);
|
||||
- text: <code>convertToF(0)</code>应返回值<code>32</code>
|
||||
testString: assert(convertToF(0) === 32);
|
||||
- text: <code>convertToF(20)</code>应返回值<code>68</code>
|
||||
testString: assert(convertToF(20) === 68);
|
||||
- text: <code>convertToF(30)</code>应返回值<code>86</code>
|
||||
testString: assert(convertToF(30) === 86);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`convertToF(0)`应该返回一个数字
|
||||
|
||||
```js
|
||||
function convertToF(celsius) {
|
||||
let fahrenheit;
|
||||
return fahrenheit;
|
||||
}
|
||||
|
||||
convertToF(30);
|
||||
|
||||
assert(typeof convertToF(0) === 'number');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`convertToF(-30)`应该返回值`-22`
|
||||
|
||||
```js
|
||||
function convertToF(celsius) {
|
||||
let fahrenheit = celsius * 9/5 + 32;
|
||||
|
||||
return fahrenheit;
|
||||
}
|
||||
|
||||
convertToF(30);
|
||||
assert(convertToF(-30) === -22);
|
||||
```
|
||||
|
||||
/section>
|
||||
`convertToF(-10)`应该返回值`14`
|
||||
|
||||
```js
|
||||
assert(convertToF(-10) === 14);
|
||||
```
|
||||
|
||||
`convertToF(0)`应返回值`32`
|
||||
|
||||
```js
|
||||
assert(convertToF(0) === 32);
|
||||
```
|
||||
|
||||
`convertToF(20)`应返回值`68`
|
||||
|
||||
```js
|
||||
assert(convertToF(20) === 68);
|
||||
```
|
||||
|
||||
`convertToF(30)`应返回值`86`
|
||||
|
||||
```js
|
||||
assert(convertToF(30) === 86);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,62 +1,45 @@
|
||||
---
|
||||
id: a302f7aae1aa3152a5b413bc
|
||||
title: 对一个数字进行推理
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 对一个数字进行推理
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">返回提供的整数的阶乘。如果整数用字母n表示,则阶乘是所有小于或等于n的正整数的乘积。因子通常用简写符号<code>n!</code>表示<code>n!</code>例如: <code>5! = 1 * 2 * 3 * 4 * 5 = 120</code>只有大于或等于零的整数才会被提供给该函数。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
返回提供的整数的阶乘。如果整数用字母n表示,则阶乘是所有小于或等于n的正整数的乘积。因子通常用简写符号`n!`表示`n!`例如: `5! = 1 * 2 * 3 * 4 * 5 = 120`只有大于或等于零的整数才会被提供给该函数。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>factorialize(5)</code>应该返回一个数字。
|
||||
testString: assert(typeof factorialize(5) === 'number');
|
||||
- text: <code>factorialize(5)</code>应该返回120。
|
||||
testString: assert(factorialize(5) === 120);
|
||||
- text: <code>factorialize(10)</code>应返回3628800。
|
||||
testString: assert(factorialize(10) === 3628800);
|
||||
- text: <code>factorialize(20)</code>应该返回2432902008176640000。
|
||||
testString: assert(factorialize(20) === 2432902008176640000);
|
||||
- text: <code>factorialize(0)</code>应该返回1。
|
||||
testString: assert(factorialize(0) === 1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`factorialize(5)`应该返回一个数字。
|
||||
|
||||
```js
|
||||
function factorialize(num) {
|
||||
return num;
|
||||
}
|
||||
|
||||
factorialize(5);
|
||||
|
||||
assert(typeof factorialize(5) === 'number');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`factorialize(5)`应该返回120。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(factorialize(5) === 120);
|
||||
```
|
||||
|
||||
/section>
|
||||
`factorialize(10)`应返回3628800。
|
||||
|
||||
```js
|
||||
assert(factorialize(10) === 3628800);
|
||||
```
|
||||
|
||||
`factorialize(20)`应该返回2432902008176640000。
|
||||
|
||||
```js
|
||||
assert(factorialize(20) === 2432902008176640000);
|
||||
```
|
||||
|
||||
`factorialize(0)`应该返回1。
|
||||
|
||||
```js
|
||||
assert(factorialize(0) === 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,61 +1,39 @@
|
||||
---
|
||||
id: adf08ec01beb4f99fc7a68f2
|
||||
title: Falsy Bouncer
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 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="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
从数组中删除所有有价值的值。 JavaScript中的Falsy值为`false` , `null` , `0` , `""` , `undefined`和`NaN` 。提示:尝试将每个值转换为布尔值。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```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]);
|
||||
- text: '<code>bouncer(["a", "b", "c"])</code>应返回<code>["a", "b", "c"]</code> 。'
|
||||
testString: assert.deepEqual(bouncer(["a", "b", "c"]), ["a", "b", "c"]);
|
||||
- text: '<code>bouncer([false, null, 0, NaN, undefined, ""])</code>应返回<code>[]</code> 。'
|
||||
testString: assert.deepEqual(bouncer([false, null, 0, NaN, undefined, ""]), []);
|
||||
- text: '<code>bouncer([1, null, NaN, 2, undefined])</code>应该返回<code>[1, 2]</code> 。'
|
||||
testString: assert.deepEqual(bouncer([null, NaN, 1, 2, undefined]), [1, 2]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`bouncer([7, "ate", "", false, 9])`应该返回`[7, "ate", 9]` 。
|
||||
|
||||
```js
|
||||
function bouncer(arr) {
|
||||
// Don't show a false ID to this bouncer.
|
||||
return arr;
|
||||
}
|
||||
|
||||
bouncer([7, "ate", "", false, 9]);
|
||||
|
||||
assert.deepEqual(bouncer([7, 'ate', '', false, 9]), [7, 'ate', 9]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`bouncer(["a", "b", "c"])`应返回`["a", "b", "c"]` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(bouncer(['a', 'b', 'c']), ['a', 'b', 'c']);
|
||||
```
|
||||
|
||||
/section>
|
||||
`bouncer([false, null, 0, NaN, undefined, ""])`应返回`[]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(bouncer([false, null, 0, NaN, undefined, '']), []);
|
||||
```
|
||||
|
||||
`bouncer([1, null, NaN, 2, undefined])`应该返回`[1, 2]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(bouncer([null, NaN, 1, 2, undefined]), [1, 2]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,64 +1,65 @@
|
||||
---
|
||||
id: a26cbbe9ad8655a977e1ceb5
|
||||
title: 找到字符串中最长的单词
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 找到字符串中最长的单词
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">返回所提供句子中最长单词的长度。您的回答应该是一个数字。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
返回所提供句子中最长单词的长度。您的回答应该是一个数字。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```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");
|
||||
- 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);
|
||||
- text: <code>findLongestWordLength("May the force be with you")</code>应该返回5。
|
||||
testString: assert(findLongestWordLength("May the force be with you") === 5);
|
||||
- text: <code>findLongestWordLength("Google do a barrel roll")</code>应返回6。
|
||||
testString: assert(findLongestWordLength("Google do a barrel roll") === 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);
|
||||
- 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);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`findLongestWordLength("The quick brown fox jumped over the lazy dog")`应该返回一个数字。
|
||||
|
||||
```js
|
||||
function findLongestWordLength(str) {
|
||||
return str.length;
|
||||
}
|
||||
|
||||
findLongestWordLength("The quick brown fox jumped over the lazy dog");
|
||||
|
||||
assert(
|
||||
typeof findLongestWordLength(
|
||||
'The quick brown fox jumped over the lazy dog'
|
||||
) === 'number'
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`findLongestWordLength("The quick brown fox jumped over the lazy dog")`应该返回6。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(
|
||||
findLongestWordLength('The quick brown fox jumped over the lazy dog') === 6
|
||||
);
|
||||
```
|
||||
|
||||
/section>
|
||||
`findLongestWordLength("May the force be with you")`应该返回5。
|
||||
|
||||
```js
|
||||
assert(findLongestWordLength('May the force be with you') === 5);
|
||||
```
|
||||
|
||||
`findLongestWordLength("Google do a barrel roll")`应返回6。
|
||||
|
||||
```js
|
||||
assert(findLongestWordLength('Google do a barrel roll') === 6);
|
||||
```
|
||||
|
||||
`findLongestWordLength("What is the average airspeed velocity of an unladen swallow")`应该返回8。
|
||||
|
||||
```js
|
||||
assert(
|
||||
findLongestWordLength(
|
||||
'What is the average airspeed velocity of an unladen swallow'
|
||||
) === 8
|
||||
);
|
||||
```
|
||||
|
||||
`findLongestWordLength("What if we try a super-long word such as otorhinolaryngology")`应该返回19。
|
||||
|
||||
```js
|
||||
assert(
|
||||
findLongestWordLength(
|
||||
'What if we try a super-long word such as otorhinolaryngology'
|
||||
) === 19
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,57 +1,37 @@
|
||||
---
|
||||
id: a6e40f1041b06c996f7b2406
|
||||
title: Finders Keepers
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: Finders Keepers
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">创建一个查看数组(第一个参数)的函数,并返回数组中传递真值测试的第一个元素(第二个参数)。如果没有元素通过测试,则返回undefined。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
创建一个查看数组(第一个参数)的函数,并返回数组中传递真值测试的第一个元素(第二个参数)。如果没有元素通过测试,则返回undefined。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。尝试配对程序。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```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);
|
||||
- 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);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; })`应该返回8。
|
||||
|
||||
```js
|
||||
function findElement(arr, func) {
|
||||
let num = 0;
|
||||
return num;
|
||||
}
|
||||
|
||||
findElement([1, 2, 3, 4], num => num % 2 === 0);
|
||||
|
||||
assert.strictEqual(
|
||||
findElement([1, 3, 5, 8, 9, 10], function (num) {
|
||||
return num % 2 === 0;
|
||||
}),
|
||||
8
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`findElement([1, 3, 5, 9], function(num) { return num % 2 === 0; })`应返回undefined。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.strictEqual(
|
||||
findElement([1, 3, 5, 9], function (num) {
|
||||
return num % 2 === 0;
|
||||
}),
|
||||
undefined
|
||||
);
|
||||
```
|
||||
|
||||
/section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,70 +1,69 @@
|
||||
---
|
||||
id: af2170cad53daa0770fabdea
|
||||
title: 突变
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 突变
|
||||
---
|
||||
|
||||
## 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="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
如果数组的第一个元素中的字符串包含数组第二个元素中字符串的所有字母,则返回true。例如, `["hello", "Hello"]`应该返回true,因为第二个字符串中的所有字母都出现在第一个字母中,忽略大小写。参数`["hello", "hey"]`应返回false,因为字符串“hello”不包含“y”。最后, `["Alien", "line"]`应该返回true,因为“line”中的所有字母都出现在“Alien”中。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>mutation(["hello", "hey"])</code>应该返回false。'
|
||||
testString: assert(mutation(["hello", "hey"]) === false);
|
||||
- text: '<code>mutation(["hello", "Hello"])</code>应该返回true。'
|
||||
testString: assert(mutation(["hello", "Hello"]) === true);
|
||||
- text: '<code>mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"])</code>应该返回true。'
|
||||
testString: assert(mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]) === true);
|
||||
- text: '<code>mutation(["Mary", "Army"])</code>应该返回true。'
|
||||
testString: assert(mutation(["Mary", "Army"]) === true);
|
||||
- text: '<code>mutation(["Mary", "Aarmy"])</code>应该返回true。'
|
||||
testString: assert(mutation(["Mary", "Aarmy"]) === true);
|
||||
- text: '<code>mutation(["Alien", "line"])</code>应该返回true。'
|
||||
testString: assert(mutation(["Alien", "line"]) === true);
|
||||
- text: '<code>mutation(["floor", "for"])</code>应该返回true。'
|
||||
testString: assert(mutation(["floor", "for"]) === true);
|
||||
- text: '<code>mutation(["hello", "neo"])</code>应该返回false。'
|
||||
testString: assert(mutation(["hello", "neo"]) === false);
|
||||
- text: '<code>mutation(["voodoo", "no"])</code>应该返回false。'
|
||||
testString: assert(mutation(["voodoo", "no"]) === false);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`mutation(["hello", "hey"])`应该返回false。
|
||||
|
||||
```js
|
||||
function mutation(arr) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
mutation(["hello", "hey"]);
|
||||
|
||||
assert(mutation(['hello', 'hey']) === false);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`mutation(["hello", "Hello"])`应该返回true。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(mutation(['hello', 'Hello']) === true);
|
||||
```
|
||||
|
||||
/section>
|
||||
`mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"])`应该返回true。
|
||||
|
||||
```js
|
||||
assert(mutation(['zyxwvutsrqponmlkjihgfedcba', 'qrstu']) === true);
|
||||
```
|
||||
|
||||
`mutation(["Mary", "Army"])`应该返回true。
|
||||
|
||||
```js
|
||||
assert(mutation(['Mary', 'Army']) === true);
|
||||
```
|
||||
|
||||
`mutation(["Mary", "Aarmy"])`应该返回true。
|
||||
|
||||
```js
|
||||
assert(mutation(['Mary', 'Aarmy']) === true);
|
||||
```
|
||||
|
||||
`mutation(["Alien", "line"])`应该返回true。
|
||||
|
||||
```js
|
||||
assert(mutation(['Alien', 'line']) === true);
|
||||
```
|
||||
|
||||
`mutation(["floor", "for"])`应该返回true。
|
||||
|
||||
```js
|
||||
assert(mutation(['floor', 'for']) === true);
|
||||
```
|
||||
|
||||
`mutation(["hello", "neo"])`应该返回false。
|
||||
|
||||
```js
|
||||
assert(mutation(['hello', 'neo']) === false);
|
||||
```
|
||||
|
||||
`mutation(["voodoo", "no"])`应该返回false。
|
||||
|
||||
```js
|
||||
assert(mutation(['voodoo', 'no']) === false);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,67 +1,57 @@
|
||||
---
|
||||
id: afcc8d540bea9ea2669306b6
|
||||
title: 重复一个字符串重复字符串
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 重复一个字符串重复字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">为<code>num</code> times(第二个参数)重复给定的字符串<code>str</code> (第一个参数)。如果<code>num</code>不是正数,则返回空字符串。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
为`num` times(第二个参数)重复给定的字符串`str` (第一个参数)。如果`num`不是正数,则返回空字符串。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>repeatStringNumTimes("*", 3)</code>应该返回<code>"***"</code> 。'
|
||||
testString: assert(repeatStringNumTimes("*", 3) === "***");
|
||||
- text: '<code>repeatStringNumTimes("abc", 3)</code>应该返回<code>"abcabcabc"</code> 。'
|
||||
testString: assert(repeatStringNumTimes("abc", 3) === "abcabcabc");
|
||||
- text: '<code>repeatStringNumTimes("abc", 4)</code>应返回<code>"abcabcabcabc"</code> 。'
|
||||
testString: assert(repeatStringNumTimes("abc", 4) === "abcabcabcabc");
|
||||
- text: '<code>repeatStringNumTimes("abc", 1)</code>应该返回<code>"abc"</code> 。'
|
||||
testString: assert(repeatStringNumTimes("abc", 1) === "abc");
|
||||
- text: '<code>repeatStringNumTimes("*", 8)</code>应该返回<code>"********"</code> 。'
|
||||
testString: assert(repeatStringNumTimes("*", 8) === "********");
|
||||
- text: '<code>repeatStringNumTimes("abc", -2)</code>应返回<code>""</code> 。'
|
||||
testString: assert(repeatStringNumTimes("abc", -2) === "");
|
||||
- text: 不应使用内置的<code>repeat()</code>方法
|
||||
testString: assert(!/\.repeat/g.test(code));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`repeatStringNumTimes("*", 3)`应该返回`"***"` 。
|
||||
|
||||
```js
|
||||
function repeatStringNumTimes(str, num) {
|
||||
// repeat after me
|
||||
return str;
|
||||
}
|
||||
|
||||
repeatStringNumTimes("abc", 3);
|
||||
|
||||
assert(repeatStringNumTimes('*', 3) === '***');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`repeatStringNumTimes("abc", 3)`应该返回`"abcabcabc"` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(repeatStringNumTimes('abc', 3) === 'abcabcabc');
|
||||
```
|
||||
|
||||
/section>
|
||||
`repeatStringNumTimes("abc", 4)`应返回`"abcabcabcabc"` 。
|
||||
|
||||
```js
|
||||
assert(repeatStringNumTimes('abc', 4) === 'abcabcabcabc');
|
||||
```
|
||||
|
||||
`repeatStringNumTimes("abc", 1)`应该返回`"abc"` 。
|
||||
|
||||
```js
|
||||
assert(repeatStringNumTimes('abc', 1) === 'abc');
|
||||
```
|
||||
|
||||
`repeatStringNumTimes("*", 8)`应该返回`"********"` 。
|
||||
|
||||
```js
|
||||
assert(repeatStringNumTimes('*', 8) === '********');
|
||||
```
|
||||
|
||||
`repeatStringNumTimes("abc", -2)`应返回`""` 。
|
||||
|
||||
```js
|
||||
assert(repeatStringNumTimes('abc', -2) === '');
|
||||
```
|
||||
|
||||
不应使用内置的`repeat()`方法
|
||||
|
||||
```js
|
||||
assert(!/\.repeat/g.test(code));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,61 +1,70 @@
|
||||
---
|
||||
id: a789b3483989747d63b0e427
|
||||
title: 返回数组中的最大数字
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 返回数组中的最大数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">返回一个数组,该数组由每个提供的子数组中的最大数字组成。为简单起见,提供的数组将包含4个子数组。请记住,您可以使用简单的for循环遍历数组,并使用数组语法<code>arr[i]</code>访问每个成员。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
返回一个数组,该数组由每个提供的子数组中的最大数字组成。为简单起见,提供的数组将包含4个子数组。请记住,您可以使用简单的for循环遍历数组,并使用数组语法`arr[i]`访问每个成员。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```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);
|
||||
- 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]);
|
||||
- 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]);
|
||||
- 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]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])`应该返回一个数组。
|
||||
|
||||
```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]]);
|
||||
|
||||
assert(
|
||||
largestOfFour([
|
||||
[4, 5, 1, 3],
|
||||
[13, 27, 18, 26],
|
||||
[32, 35, 37, 39],
|
||||
[1000, 1001, 857, 1]
|
||||
]).constructor === Array
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]])`应该返回`[27, 5, 39, 1001]` `largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]])` `[27, 5, 39, 1001]` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(
|
||||
largestOfFour([
|
||||
[13, 27, 18, 26],
|
||||
[4, 5, 1, 3],
|
||||
[32, 35, 37, 39],
|
||||
[1000, 1001, 857, 1]
|
||||
]),
|
||||
[27, 5, 39, 1001]
|
||||
);
|
||||
```
|
||||
|
||||
/section>
|
||||
`largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]])`应该返回`[9, 35, 97, 1000000]` `largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]])` `[9, 35, 97, 1000000]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
largestOfFour([
|
||||
[4, 9, 1, 3],
|
||||
[13, 35, 18, 26],
|
||||
[32, 35, 97, 39],
|
||||
[1000000, 1001, 857, 1]
|
||||
]),
|
||||
[9, 35, 97, 1000000]
|
||||
);
|
||||
```
|
||||
|
||||
`largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]])`应该返回`[25, 48, 21, -3]` 。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
largestOfFour([
|
||||
[17, 23, 25, 12],
|
||||
[25, 7, 34, 48],
|
||||
[4, -10, 18, 21],
|
||||
[-72, -3, -17, -10]
|
||||
]),
|
||||
[25, 48, 21, -3]
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,60 +1,39 @@
|
||||
---
|
||||
id: a202eed8fc186c8434cb6d61
|
||||
title: 反转字符串
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 反转字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">反转提供的字符串。您可能需要先将字符串转换为数组,然后才能将其反转。您的结果必须是字符串。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
反转提供的字符串。您可能需要先将字符串转换为数组,然后才能将其反转。您的结果必须是字符串。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>reverseString("hello")</code>应该返回一个字符串。
|
||||
testString: assert(typeof reverseString("hello") === "string");
|
||||
- text: <code>reverseString("hello")</code>应该变成<code>"olleh"</code> 。
|
||||
testString: assert(reverseString("hello") === "olleh");
|
||||
- text: <code>reverseString("Howdy")</code>应该变成<code>"ydwoH"</code> 。
|
||||
testString: assert(reverseString("Howdy") === "ydwoH");
|
||||
- text: <code>reverseString("Greetings from Earth")</code>应返回<code>"htraE morf sgniteerG"</code> 。
|
||||
testString: assert(reverseString("Greetings from Earth") === "htraE morf sgniteerG");
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`reverseString("hello")`应该返回一个字符串。
|
||||
|
||||
```js
|
||||
function reverseString(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
reverseString("hello");
|
||||
|
||||
assert(typeof reverseString('hello') === 'string');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`reverseString("hello")`应该变成`"olleh"` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(reverseString('hello') === 'olleh');
|
||||
```
|
||||
|
||||
/section>
|
||||
`reverseString("Howdy")`应该变成`"ydwoH"` 。
|
||||
|
||||
```js
|
||||
assert(reverseString('Howdy') === 'ydwoH');
|
||||
```
|
||||
|
||||
`reverseString("Greetings from Earth")`应返回`"htraE morf sgniteerG"` 。
|
||||
|
||||
```js
|
||||
assert(reverseString('Greetings from Earth') === 'htraE morf sgniteerG');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,74 +1,59 @@
|
||||
---
|
||||
id: 579e2a2c335b9d72dd32e05c
|
||||
isBeta: true
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 切片和拼接
|
||||
challengeType: 5
|
||||
forumTopicId: 301148
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您将获得两个数组和一个索引。使用数组方法<code>slice</code>和<code>splice</code>按顺序将第一个数组的每个元素复制到第二个数组中。开始在第二个数组的索引<code>n</code>处插入元素。返回结果数组。函数运行后,输入数组应保持不变。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
您将获得两个数组和一个索引。使用数组方法`slice`和`splice`按顺序将第一个数组的每个元素复制到第二个数组中。开始在第二个数组的索引`n`处插入元素。返回结果数组。函数运行后,输入数组应保持不变。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```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]);
|
||||
- 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"]);
|
||||
- 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"]);
|
||||
- text: 第一个数组中的所有元素都应按原始顺序添加到第二个数组中。
|
||||
testString: assert.deepEqual(frankenSplice([1, 2, 3, 4], [], 0), [1, 2, 3, 4]);
|
||||
- text: 函数运行后,第一个数组应保持不变。
|
||||
testString: assert(testArr1[0] === 1 && testArr1[1] === 2);
|
||||
- text: 函数运行后,第二个数组应保持不变。
|
||||
testString: assert(testArr2[0] === "a" && testArr2[1] === "b");
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`frankenSplice([1, 2, 3], [4, 5], 1)`应该返回`[4, 1, 2, 3, 5]`。
|
||||
|
||||
```js
|
||||
function frankenSplice(arr1, arr2, n) {
|
||||
// It's alive. It's alive!
|
||||
return arr2;
|
||||
}
|
||||
|
||||
frankenSplice([1, 2, 3], [4, 5, 6], 1);
|
||||
|
||||
assert.deepEqual(frankenSplice([1, 2, 3], [4, 5], 1), [4, 1, 2, 3, 5]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`frankenSplice([1, 2], ["a", "b"], 1)` 应返回 `["a", 1, 2, "b"]`
|
||||
|
||||
```js
|
||||
console.info('after the test');
|
||||
assert.deepEqual(frankenSplice(testArr1, testArr2, 1), ['a', 1, 2, 'b']);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2)` 应该返回 `["head", "shoulders", "claw", "tentacle", "knees", "toes"]`
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert.deepEqual(
|
||||
frankenSplice(
|
||||
['claw', 'tentacle'],
|
||||
['head', 'shoulders', 'knees', 'toes'],
|
||||
2
|
||||
),
|
||||
['head', 'shoulders', 'claw', 'tentacle', 'knees', 'toes']
|
||||
);
|
||||
```
|
||||
|
||||
/section>
|
||||
第一个数组中的所有元素都应按原始顺序添加到第二个数组中。
|
||||
|
||||
```js
|
||||
assert.deepEqual(frankenSplice([1, 2, 3, 4], [], 0), [1, 2, 3, 4]);
|
||||
```
|
||||
|
||||
函数运行后,第一个数组应保持不变。
|
||||
|
||||
```js
|
||||
frankenSplice(testArr1, testArr2, 1);
|
||||
assert.deepEqual(testArr1, [1, 2]);
|
||||
```
|
||||
|
||||
函数运行后,第二个数组应保持不变。
|
||||
|
||||
```js
|
||||
frankenSplice(testArr1, testArr2, 1);
|
||||
assert.deepEqual(testArr2, ['a', 'b']);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
@ -1,60 +1,42 @@
|
||||
---
|
||||
id: ab6137d4e35944e21037b769
|
||||
title: 标题案例句子
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 标题案例句子
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">返回提供的字符串,每个单词的首字母大写。确保单词的其余部分为小写。出于本练习的目的,您还应该将诸如“the”和“of”之类的连接词大写。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
返回提供的字符串,每个单词的首字母大写。确保单词的其余部分为小写。出于本练习的目的,您还应该将诸如“the”和“of”之类的连接词大写。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>titleCase("I'm a little tea pot")</code>应该返回一个字符串。'
|
||||
testString: assert(typeof titleCase("I'm a little tea pot") === "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");
|
||||
- text: <code>titleCase("sHoRt AnD sToUt")</code>应返回<code>Short And Stout</code> 。
|
||||
testString: assert(titleCase("sHoRt AnD sToUt") === "Short And Stout");
|
||||
- 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");
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`titleCase("I'm a little tea pot")`应该返回一个字符串。
|
||||
|
||||
```js
|
||||
function titleCase(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
titleCase("I'm a little tea pot");
|
||||
|
||||
assert(typeof titleCase("I'm a little tea pot") === 'string');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`titleCase("I'm a little tea pot")`应该归还`I'm A Little Tea Pot` 。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(titleCase("I'm a little tea pot") === "I'm A Little Tea Pot");
|
||||
```
|
||||
|
||||
/section>
|
||||
`titleCase("sHoRt AnD sToUt")`应返回`Short And Stout` 。
|
||||
|
||||
```js
|
||||
assert(titleCase('sHoRt AnD sToUt') === 'Short And Stout');
|
||||
```
|
||||
|
||||
`titleCase("HERE IS MY HANDLE HERE IS MY SPOUT")` `Here Is My Handle Here Is My Spout` `titleCase("HERE IS MY HANDLE HERE IS MY SPOUT")`应该回到`Here Is My Handle Here Is My Spout` 。
|
||||
|
||||
```js
|
||||
assert(
|
||||
titleCase('HERE IS MY HANDLE HERE IS MY SPOUT') ===
|
||||
'Here Is My Handle Here Is My Spout'
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,65 +1,67 @@
|
||||
---
|
||||
id: ac6993d51946422351508a41
|
||||
title: 截断字符串
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 截断字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">如果字符串(第一个参数)长于给定的最大字符串长度(第二个参数),则截断该字符串。返回带有<code>...</code>结尾的截断字符串。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
如果字符串(第一个参数)长于给定的最大字符串长度(第二个参数),则截断该字符串。返回带有`...`结尾的截断字符串。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```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...");
|
||||
- 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...");
|
||||
- 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");
|
||||
- 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');
|
||||
- text: '<code>truncateString("A-", 1)</code>应返回“A ...”。'
|
||||
testString: assert(truncateString("A-", 1) === "A...");
|
||||
- text: '<code>truncateString("Absolutely Longer", 2)</code>应返回“Ab ...”。'
|
||||
testString: assert(truncateString("Absolutely Longer", 2) === "Ab...");
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`truncateString("A-tisket a-tasket A green and yellow basket", 8)`应该返回“A-tisket ......”。
|
||||
|
||||
```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);
|
||||
|
||||
assert(
|
||||
truncateString('A-tisket a-tasket A green and yellow basket', 8) ===
|
||||
'A-tisket...'
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`truncateString("Peter Piper picked a peck of pickled peppers", 11)`应该回归“Peter Piper ......”。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(
|
||||
truncateString('Peter Piper picked a peck of pickled peppers', 11) ===
|
||||
'Peter Piper...'
|
||||
);
|
||||
```
|
||||
|
||||
/section>
|
||||
`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”。
|
||||
|
||||
```js
|
||||
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'
|
||||
);
|
||||
```
|
||||
|
||||
`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”。
|
||||
|
||||
```js
|
||||
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'
|
||||
);
|
||||
```
|
||||
|
||||
`truncateString("A-", 1)`应返回“A ...”。
|
||||
|
||||
```js
|
||||
assert(truncateString('A-', 1) === 'A...');
|
||||
```
|
||||
|
||||
`truncateString("Absolutely Longer", 2)`应返回“Ab ...”。
|
||||
|
||||
```js
|
||||
assert(truncateString('Absolutely Longer', 2) === 'Ab...');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,85 +1,111 @@
|
||||
---
|
||||
id: a24c1a4622e3c05097f71d67
|
||||
title: 我属于哪里?
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
title: 我属于哪里?
|
||||
---
|
||||
|
||||
## 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="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。编写自己的代码。 </section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">
|
||||
</section>
|
||||
返回一个值(第二个参数)应该在排序后插入数组(第一个参数)的最低索引。返回的值应该是一个数字。例如, `getIndexToIns([1,2,3,4], 1.5)`应返回`1`因为它大于`1` (索引0),但小于`2` (索引1)。同样, `getIndexToIns([20,3,5], 19)`应返回`2`因为一旦数组已经排序,它将看起来像`[3,5,20]` , `19`小于`20` (索引2)并且大于`5` (指数1)。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。编写自己的代码。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```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);
|
||||
- text: '<code>getIndexToIns([10, 20, 30, 40, 50], 35)</code>应返回一个数字。'
|
||||
testString: assert(typeof(getIndexToIns([10, 20, 30, 40, 50], 35)) === "number");
|
||||
- text: '<code>getIndexToIns([10, 20, 30, 40, 50], 30)</code>应该返回<code>2</code> 。'
|
||||
testString: assert(getIndexToIns([10, 20, 30, 40, 50], 30) === 2);
|
||||
- text: '<code>getIndexToIns([10, 20, 30, 40, 50], 30)</code>应该返回一个数字。'
|
||||
testString: assert(typeof(getIndexToIns([10, 20, 30, 40, 50], 30)) === "number");
|
||||
- text: '<code>getIndexToIns([40, 60], 50)</code>应返回<code>1</code> 。'
|
||||
testString: assert(getIndexToIns([40, 60], 50) === 1);
|
||||
- text: '<code>getIndexToIns([40, 60], 50)</code>应返回一个数字。'
|
||||
testString: assert(typeof(getIndexToIns([40, 60], 50)) === "number");
|
||||
- text: '<code>getIndexToIns([3, 10, 5], 3)</code>应该返回<code>0</code> 。'
|
||||
testString: assert(getIndexToIns([3, 10, 5], 3) === 0);
|
||||
- text: '<code>getIndexToIns([3, 10, 5], 3)</code>应返回一个数字。'
|
||||
testString: assert(typeof(getIndexToIns([3, 10, 5], 3)) === "number");
|
||||
- text: '<code>getIndexToIns([5, 3, 20, 3], 5)</code>应返回<code>2</code> 。'
|
||||
testString: assert(getIndexToIns([5, 3, 20, 3], 5) === 2);
|
||||
- text: '<code>getIndexToIns([5, 3, 20, 3], 5)</code>应返回一个数字。'
|
||||
testString: assert(typeof(getIndexToIns([5, 3, 20, 3], 5)) === "number");
|
||||
- text: '<code>getIndexToIns([2, 20, 10], 19)</code>应该返回<code>2</code> 。'
|
||||
testString: assert(getIndexToIns([2, 20, 10], 19) === 2);
|
||||
- text: '<code>getIndexToIns([2, 20, 10], 19)</code>应返回一个数字。'
|
||||
testString: assert(typeof(getIndexToIns([2, 20, 10], 19)) === "number");
|
||||
- text: '<code>getIndexToIns([2, 5, 10], 15)</code>应该返回<code>3</code> 。'
|
||||
testString: assert(getIndexToIns([2, 5, 10], 15) === 3);
|
||||
- text: '<code>getIndexToIns([2, 5, 10], 15)</code>应返回一个数字。'
|
||||
testString: assert(typeof(getIndexToIns([2, 5, 10], 15)) === "number");
|
||||
- text: '<code>getIndexToIns([], 1)</code>应该返回<code>0</code> 。'
|
||||
testString: assert(getIndexToIns([], 1) === 0);
|
||||
- text: '<code>getIndexToIns([], 1)</code>应该返回一个数字。'
|
||||
testString: assert(typeof(getIndexToIns([], 1)) === "number");
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`getIndexToIns([10, 20, 30, 40, 50], 35)`应返回`3` 。
|
||||
|
||||
```js
|
||||
function getIndexToIns(arr, num) {
|
||||
// Find my place in this sorted array.
|
||||
return num;
|
||||
}
|
||||
|
||||
getIndexToIns([40, 60], 50);
|
||||
|
||||
assert(getIndexToIns([10, 20, 30, 40, 50], 35) === 3);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`getIndexToIns([10, 20, 30, 40, 50], 35)`应返回一个数字。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
assert(typeof getIndexToIns([10, 20, 30, 40, 50], 35) === 'number');
|
||||
```
|
||||
|
||||
/section>
|
||||
`getIndexToIns([10, 20, 30, 40, 50], 30)`应该返回`2` 。
|
||||
|
||||
```js
|
||||
assert(getIndexToIns([10, 20, 30, 40, 50], 30) === 2);
|
||||
```
|
||||
|
||||
`getIndexToIns([10, 20, 30, 40, 50], 30)`应该返回一个数字。
|
||||
|
||||
```js
|
||||
assert(typeof getIndexToIns([10, 20, 30, 40, 50], 30) === 'number');
|
||||
```
|
||||
|
||||
`getIndexToIns([40, 60], 50)`应返回`1` 。
|
||||
|
||||
```js
|
||||
assert(getIndexToIns([40, 60], 50) === 1);
|
||||
```
|
||||
|
||||
`getIndexToIns([40, 60], 50)`应返回一个数字。
|
||||
|
||||
```js
|
||||
assert(typeof getIndexToIns([40, 60], 50) === 'number');
|
||||
```
|
||||
|
||||
`getIndexToIns([3, 10, 5], 3)`应该返回`0` 。
|
||||
|
||||
```js
|
||||
assert(getIndexToIns([3, 10, 5], 3) === 0);
|
||||
```
|
||||
|
||||
`getIndexToIns([3, 10, 5], 3)`应返回一个数字。
|
||||
|
||||
```js
|
||||
assert(typeof getIndexToIns([3, 10, 5], 3) === 'number');
|
||||
```
|
||||
|
||||
`getIndexToIns([5, 3, 20, 3], 5)`应返回`2` 。
|
||||
|
||||
```js
|
||||
assert(getIndexToIns([5, 3, 20, 3], 5) === 2);
|
||||
```
|
||||
|
||||
`getIndexToIns([5, 3, 20, 3], 5)`应返回一个数字。
|
||||
|
||||
```js
|
||||
assert(typeof getIndexToIns([5, 3, 20, 3], 5) === 'number');
|
||||
```
|
||||
|
||||
`getIndexToIns([2, 20, 10], 19)`应该返回`2` 。
|
||||
|
||||
```js
|
||||
assert(getIndexToIns([2, 20, 10], 19) === 2);
|
||||
```
|
||||
|
||||
`getIndexToIns([2, 20, 10], 19)`应返回一个数字。
|
||||
|
||||
```js
|
||||
assert(typeof getIndexToIns([2, 20, 10], 19) === 'number');
|
||||
```
|
||||
|
||||
`getIndexToIns([2, 5, 10], 15)`应该返回`3` 。
|
||||
|
||||
```js
|
||||
assert(getIndexToIns([2, 5, 10], 15) === 3);
|
||||
```
|
||||
|
||||
`getIndexToIns([2, 5, 10], 15)`应返回一个数字。
|
||||
|
||||
```js
|
||||
assert(typeof getIndexToIns([2, 5, 10], 15) === 'number');
|
||||
```
|
||||
|
||||
`getIndexToIns([], 1)`应该返回`0` 。
|
||||
|
||||
```js
|
||||
assert(getIndexToIns([], 1) === 0);
|
||||
```
|
||||
|
||||
`getIndexToIns([], 1)`应该返回一个数字。
|
||||
|
||||
```js
|
||||
assert(typeof getIndexToIns([], 1) === 'number');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,87 +1,65 @@
|
||||
---
|
||||
id: 5a661e0f1068aca922b3ef17
|
||||
title: 使用方括号访问数组的内容
|
||||
challengeType: 1
|
||||
forumTopicId: 301149
|
||||
title: 使用方括号访问数组的内容
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
所有数据结构的基本特性是,它们不仅能够存储数据,我们还能够按照需求来访问存放在其中的数据。我们已经学习了如何创建一个数组结构,现在让我们开始学习如何访问这个数组结构中的数据。
|
||||
|
||||
我们先定义一个包含 3 个元素的数组:
|
||||
|
||||
```js
|
||||
let ourArray = ["a", "b", "c"];
|
||||
```
|
||||
|
||||
在一个数组结构中,其内部的每个元素都有一个与之对应的<dfn>索引</dfn>(<dfn>index</dfn>)。索引是该元素在数组中的位置,可被用于引用该元素。但需要注意的是,JavaScript 数组的索引是从0开始的(<dfn>zero-indexed</dfn>),即一个数组的第一个元素是在数组中的<em><strong>第 0 个</strong></em>位置,而不是第 1 个位置。
|
||||
要从一个数组中获取一个元素,我们可以在一个数组变量名的后面加一个使用“方括号”括起来的索引。这叫做<dfn>方括号符号</dfn>(<dfn>bracket notation</dfn>)。
|
||||
例如我们要从<code>ourArray</code>数组变量中获取数据元素<code>"a"</code>并将其赋值给一个变量,我们可以编写如下所示的代码:
|
||||
在一个数组结构中,其内部的每个元素都有一个与之对应的<dfn>索引</dfn>(<dfn>index</dfn>)。索引是该元素在数组中的位置,可被用于引用该元素。但需要注意的是,JavaScript 数组的索引是从0开始的(<dfn>zero-indexed</dfn>),即一个数组的第一个元素是在数组中的***第 0 个***位置,而不是第 1 个位置。 要从一个数组中获取一个元素,我们可以在一个数组变量名的后面加一个使用“方括号”括起来的索引。这叫做<dfn>方括号符号</dfn>(<dfn>bracket notation</dfn>)。 例如我们要从`ourArray`数组变量中获取数据元素`"a"`并将其赋值给一个变量,我们可以编写如下所示的代码:
|
||||
|
||||
```js
|
||||
let ourVariable = ourArray[0];
|
||||
// ourVariable 的值为 "a"
|
||||
```
|
||||
|
||||
除了使用 “索引” 来获取某个元素值以外,你还可以通过类似的方法来<em>设置</em>一个索引位置所对应的元素值:
|
||||
除了使用 “索引” 来获取某个元素值以外,你还可以通过类似的方法来*设置*一个索引位置所对应的元素值:
|
||||
|
||||
```js
|
||||
ourArray[1] = "not b anymore";
|
||||
// ourArray 现在的值为 ["a", "not b anymore", "c"];
|
||||
```
|
||||
|
||||
我们现在已经利用方括号将索引为 1 的元素从<code>"b"</code>设置为了<code>"not b anymore"</code>。
|
||||
</section>
|
||||
我们现在已经利用方括号将索引为 1 的元素从`"b"`设置为了`"not b anymore"`。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
在本挑战中,请你将<code>myArray</code>中第二个元素(索引<code>1</code>)设置为除了<code>"b"</code>以外的任意值。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
在本挑战中,请你将`myArray`中第二个元素(索引`1`)设置为除了`"b"`以外的任意值。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>myArray[0]</code>等于<code>"a"</code>'
|
||||
testString: assert.strictEqual(myArray[0], "a");
|
||||
- text: '<code>myArray[1]</code>不再设置为<code>"b"</code>'
|
||||
testString: assert.notStrictEqual(myArray[1], "b");
|
||||
- text: '<code>myArray[2]</code>等于<code>"c"</code>'
|
||||
testString: assert.strictEqual(myArray[2], "c");
|
||||
- text: '<code>myArray[3]</code>等于<code>"d"</code>'
|
||||
testString: assert.strictEqual(myArray[3], "d");
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myArray[0]`等于`"a"`
|
||||
|
||||
```js
|
||||
let myArray = ["a", "b", "c", "d"];
|
||||
// change code below this line
|
||||
|
||||
//change code above this line
|
||||
console.log(myArray);
|
||||
assert.strictEqual(myArray[0], 'a');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`myArray[1]`不再设置为`"b"`
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let myArray = ["a", "b", "c", "d"];
|
||||
myArray[1] = "e";
|
||||
assert.notStrictEqual(myArray[1], 'b');
|
||||
```
|
||||
|
||||
</section>
|
||||
`myArray[2]`等于`"c"`
|
||||
|
||||
```js
|
||||
assert.strictEqual(myArray[2], 'c');
|
||||
```
|
||||
|
||||
`myArray[3]`等于`"d"`
|
||||
|
||||
```js
|
||||
assert.strictEqual(myArray[3], 'd');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,95 +1,63 @@
|
||||
---
|
||||
id: 587d7b7c367417b2b2512b1a
|
||||
title: 使用方括号访问属性名称
|
||||
challengeType: 1
|
||||
forumTopicId: 301150
|
||||
title: 使用方括号访问属性名称
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
在关于对象的第一个挑战中,我们提到可以在方括号符号中用一个变量作为属性名来访问属性值。假设一个超市的收银台的程序中使用了一个<code>foods</code>对象,并且有一些程序逻辑会设置<code>selectedFood</code>,我们需要查询<code>foods</code>对象来检查某种食物是否存在,我们可以这样写检查逻辑:
|
||||
# --description--
|
||||
|
||||
在关于对象的第一个挑战中,我们提到可以在方括号符号中用一个变量作为属性名来访问属性值。假设一个超市的收银台的程序中使用了一个`foods`对象,并且有一些程序逻辑会设置`selectedFood`,我们需要查询`foods`对象来检查某种食物是否存在,我们可以这样写检查逻辑:
|
||||
|
||||
```js
|
||||
let selectedFood = getCurrentFood(scannedItem);
|
||||
let inventory = foods[selectedFood];
|
||||
```
|
||||
|
||||
上述代码会先计算<code>selectedFood</code>变量的值,并返回<code>foods</code>对象中以该值命名的属性对应的值,若没有以该值命名的属性则会返回<code>undefined</code>。有时候对象的属性名在运行之前是不确定的,或者我们需要动态地访问对象的属性,这时方括号符号就会很有用。
|
||||
</section>
|
||||
上述代码会先计算`selectedFood`变量的值,并返回`foods`对象中以该值命名的属性对应的值,若没有以该值命名的属性则会返回`undefined`。有时候对象的属性名在运行之前是不确定的,或者我们需要动态地访问对象的属性,这时方括号符号就会很有用。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
我们已经定义了一个<code>checkInventory</code>函数,它接受一个被扫描到的商品名作为输入参数。它要返回<code>foods</code>对象中以<code>scannedItem</code>的值命名的属性的值。只有有效的属性名会作为参数传入<code>checkInventory</code>,你在完成挑战时不需处理参数无效的情况。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
我们已经定义了一个`checkInventory`函数,它接受一个被扫描到的商品名作为输入参数。它要返回`foods`对象中以`scannedItem`的值命名的属性的值。只有有效的属性名会作为参数传入`checkInventory`,你在完成挑战时不需处理参数无效的情况。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>checkInventory</code>是一个函数
|
||||
testString: assert.strictEqual(typeof checkInventory, '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});'
|
||||
- text: <code>checkInventory("apples")</code>应该返回<code>25</code>
|
||||
testString: assert.strictEqual(checkInventory('apples'), 25);
|
||||
- text: <code>checkInventory("bananas")</code>应该返回<code>13</code>
|
||||
testString: assert.strictEqual(checkInventory('bananas'), 13);
|
||||
- text: <code>checkInventory("strawberries")</code>应该返回<code>27</code>
|
||||
testString: assert.strictEqual(checkInventory('strawberries'), 27);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`checkInventory`是一个函数
|
||||
|
||||
```js
|
||||
let foods = {
|
||||
assert.strictEqual(typeof checkInventory, 'function');
|
||||
```
|
||||
|
||||
`foods`对象应该只有以下键值对: `apples: 25` , `oranges: 32` , `plums: 28` , `bananas: 13` , `grapes: 35` , `strawberries: 27`
|
||||
|
||||
```js
|
||||
assert.deepEqual(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'>
|
||||
`checkInventory("apples")`应该返回`25`
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let foods = {
|
||||
apples: 25,
|
||||
oranges: 32,
|
||||
plums: 28,
|
||||
bananas: 13,
|
||||
grapes: 35,
|
||||
strawberries: 27
|
||||
};
|
||||
|
||||
function checkInventory(scannedItem) {
|
||||
return foods[scannedItem];
|
||||
}
|
||||
assert.strictEqual(checkInventory('apples'), 25);
|
||||
```
|
||||
|
||||
</section>
|
||||
`checkInventory("bananas")`应该返回`13`
|
||||
|
||||
```js
|
||||
assert.strictEqual(checkInventory('bananas'), 13);
|
||||
```
|
||||
|
||||
`checkInventory("strawberries")`应该返回`27`
|
||||
|
||||
```js
|
||||
assert.strictEqual(checkInventory('strawberries'), 27);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d78b2367417b2b2512b0e
|
||||
title: 使用 push() 和 unshift() 添加项目到数组中
|
||||
challengeType: 1
|
||||
forumTopicId: 301151
|
||||
title: 使用 push() 和 unshift() 添加项目到数组中
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
一个数组的长度与其包含的数据类型一样,是不固定的。数组可以包含任意数量的元素,可以不限次数地往数组中添加元素或者从中移除元素,或者说数组是<dfn>可变的</dfn>(<dfn>mutable</dfn>)。在本挑战中,我们要学习两个以编程方式修改数组的方法:<code>Array.push()</code>和<code>Array.unshift()</code>。
|
||||
这两个方法都接收一个或多个元素作为参数;对一个数组调用这两个方法都可以将输入的元素插入到该数组中;<code>push()</code>方法将元素插入到一个数组的末尾,而<code>unshift()</code>方法将元素插入到一个数组的开头。请看以下例子:
|
||||
# --description--
|
||||
|
||||
一个数组的长度与其包含的数据类型一样,是不固定的。数组可以包含任意数量的元素,可以不限次数地往数组中添加元素或者从中移除元素,或者说数组是<dfn>可变的</dfn>(<dfn>mutable</dfn>)。在本挑战中,我们要学习两个以编程方式修改数组的方法:`Array.push()`和`Array.unshift()`。
|
||||
|
||||
这两个方法都接收一个或多个元素作为参数;对一个数组调用这两个方法都可以将输入的元素插入到该数组中;`push()`方法将元素插入到一个数组的末尾,而`unshift()`方法将元素插入到一个数组的开头。请看以下例子:
|
||||
|
||||
```js
|
||||
let twentyThree = 'XXIII';
|
||||
@ -22,63 +23,40 @@ romanNumerals.push(twentyThree);
|
||||
```
|
||||
|
||||
注意,我们还可以输入变量,这允许我们很灵活地动态改变我们数组中的数据。
|
||||
</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>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
我们已经定义了一个`mixedNumbers`函数,它会接受一个数组作为参数。请你修改这个函数,使用`push()`和`unshift()`来将`'I', 2, 'three'`插入到数组的开头,将`7, 'VIII', 9`插入到数组的末尾,使得这个函数返回一个依次包含 1-9 的数组。
|
||||
|
||||
```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]);
|
||||
- text: <code>mixedNumbers</code>函数应该使用<code>push()</code>方法
|
||||
testString: assert(mixedNumbers.toString().match(/\.push/));
|
||||
- text: <code>mixedNumbers</code>函数应该使用<code>unshift()</code>方法
|
||||
testString: assert(mixedNumbers.toString().match(/\.unshift/));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`mixedNumbers(["IV", 5, "six"])`现在应该返回`["I", 2, "three", "IV", 5, "six", 7, "VIII", 9]`
|
||||
|
||||
```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']));
|
||||
assert.deepEqual(mixedNumbers(['IV', 5, 'six']), [
|
||||
'I',
|
||||
2,
|
||||
'three',
|
||||
'IV',
|
||||
5,
|
||||
'six',
|
||||
7,
|
||||
'VIII',
|
||||
9
|
||||
]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`mixedNumbers`函数应该使用`push()`方法
|
||||
|
||||
```js
|
||||
function mixedNumbers(arr) {
|
||||
// change code below this line
|
||||
arr.push(7,'VIII',9);
|
||||
arr.unshift('I',2,'three');
|
||||
// change code above this line
|
||||
return arr;
|
||||
}
|
||||
assert(mixedNumbers.toString().match(/\.push/));
|
||||
```
|
||||
|
||||
</section>
|
||||
`mixedNumbers`函数应该使用`unshift()`方法
|
||||
|
||||
```js
|
||||
assert(mixedNumbers.toString().match(/\.unshift/));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
---
|
||||
id: 587d78b3367417b2b2512b11
|
||||
title: 使用 splice() 增加项目
|
||||
challengeType: 1
|
||||
forumTopicId: 301152
|
||||
title: 使用 splice() 增加项目
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
你还记得在上个挑战中我们提到<code>splice()</code>方法可以接受最多 3 个参数吗?我们现在可以进一步了解<code>splice()</code>。除了移除元素,我们还可以利用它的第三个参数来向数组中<em>添加</em>元素。第三个参数可以是一个或多个元素,这些元素会被添加到数组中。这使我们能够便捷地将数组中的一个或一系列元素换成其他的元素。例如:
|
||||
# --description--
|
||||
|
||||
你还记得在上个挑战中我们提到`splice()`方法可以接受最多 3 个参数吗?我们现在可以进一步了解`splice()`。除了移除元素,我们还可以利用它的第三个参数来向数组中*添加*元素。第三个参数可以是一个或多个元素,这些元素会被添加到数组中。这使我们能够便捷地将数组中的一个或一系列元素换成其他的元素。例如:
|
||||
|
||||
```js
|
||||
const numbers = [10, 11, 12, 12, 15];
|
||||
@ -20,63 +20,52 @@ console.log(numbers);
|
||||
// 返回 [ 10, 11, 12, 13, 14, 15 ]
|
||||
```
|
||||
|
||||
以一个数字数组开始。接着调用 <code>splice()</code> 方法,在 (3) 的索引位置开始删除元素,删除的元素数量是 (1),(13, 14) 是在删除位置插入的元素,可以在 <code>amountToDelete</code> 后面插入任意数量的元素(以逗号分隔),每个都会被插入。
|
||||
</section>
|
||||
以一个数字数组开始。接着调用 `splice()` 方法,在 (3) 的索引位置开始删除元素,删除的元素数量是 (1),(13, 14) 是在删除位置插入的元素,可以在 `amountToDelete` 后面插入任意数量的元素(以逗号分隔),每个都会被插入。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
我们已经定义了一个<code>htmlColorNames</code>函数,它以一个 HTML 颜色的数组作为输入参数。请修改这个函数,利用<code>splice()</code>来移除数组中的前两个元素,并在对应的位置上添加<code>'DarkSalmon'</code>和<code>'BlanchedAlmond'</code>。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
我们已经定义了一个`htmlColorNames`函数,它以一个 HTML 颜色的数组作为输入参数。请修改这个函数,利用`splice()`来移除数组中的前两个元素,并在对应的位置上添加`'DarkSalmon'`和`'BlanchedAlmond'`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>htmlColorNames</code>应该返回<code>["DarkSalmon", "BlanchedAlmond", "LavenderBlush", "PaleTurqoise", "FireBrick"]</code>'
|
||||
testString: assert.deepEqual(htmlColorNames(['DarkGoldenRod', 'WhiteSmoke', 'LavenderBlush', 'PaleTurquoise', 'FireBrick']), ['DarkSalmon', 'BlanchedAlmond', 'LavenderBlush', 'PaleTurquoise', 'FireBrick']);
|
||||
- text: <code>htmlColorNames</code>函数应该使用<code>splice()</code>方法
|
||||
testString: assert(/.splice/.test(code));
|
||||
- text: 你不应该使用<code>shift()</code>或<code>unshift()</code> 。
|
||||
testString: assert(!/shift|unshift/.test(code));
|
||||
- text: 您不应该使用数组括号表示法。
|
||||
testString: assert(!/\[\d\]\s*=/.test(code));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`htmlColorNames`应该返回`["DarkSalmon", "BlanchedAlmond", "LavenderBlush", "PaleTurqoise", "FireBrick"]`
|
||||
|
||||
```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']));
|
||||
assert.deepEqual(
|
||||
htmlColorNames([
|
||||
'DarkGoldenRod',
|
||||
'WhiteSmoke',
|
||||
'LavenderBlush',
|
||||
'PaleTurquoise',
|
||||
'FireBrick'
|
||||
]),
|
||||
[
|
||||
'DarkSalmon',
|
||||
'BlanchedAlmond',
|
||||
'LavenderBlush',
|
||||
'PaleTurquoise',
|
||||
'FireBrick'
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`htmlColorNames`函数应该使用`splice()`方法
|
||||
|
||||
```js
|
||||
function htmlColorNames(arr) {
|
||||
arr.splice(0,2,'DarkSalmon', 'BlanchedAlmond');
|
||||
return arr;
|
||||
}
|
||||
assert(/.splice/.test(code));
|
||||
```
|
||||
|
||||
</section>
|
||||
你不应该使用`shift()`或`unshift()` 。
|
||||
|
||||
```js
|
||||
assert(!/shift|unshift/.test(code));
|
||||
```
|
||||
|
||||
您不应该使用数组括号表示法。
|
||||
|
||||
```js
|
||||
assert(!/\[\d\]\s*=/.test(code));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
---
|
||||
id: 587d7b7c367417b2b2512b18
|
||||
title: 将键值对添加到对象中
|
||||
challengeType: 1
|
||||
forumTopicId: 301153
|
||||
title: 将键值对添加到对象中
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
对象(object)本质上是<dfn>键值对(key-value pair)</dfn>的集合,或者说,一系列被映射到唯一标识符(叫做<dfn>属性(property)</dfn>或者<dfn>键(key)</dfn>)的数据。让我们来看一个很简单的例子:
|
||||
|
||||
```js
|
||||
@ -18,7 +18,7 @@ let FCC_User = {
|
||||
};
|
||||
```
|
||||
|
||||
上面的代码定义了一个叫做<code>FCC_User</code>的对象,它有 4 个<dfn>属性</dfn>,每个属性映射一个特定的值。如果我们想知道<code>FCC_User</code>有多少<code>followers</code>,我们可以这样访问其<code>followers</code>属性:
|
||||
上面的代码定义了一个叫做`FCC_User`的对象,它有 4 个<dfn>属性</dfn>,每个属性映射一个特定的值。如果我们想知道`FCC_User`有多少`followers`,我们可以这样访问其`followers`属性:
|
||||
|
||||
```js
|
||||
let userData = FCC_User.followers;
|
||||
@ -32,74 +32,47 @@ let userData = FCC_User['followers'];
|
||||
// userData 等于 572
|
||||
```
|
||||
|
||||
注意,在用<dfn>方括号符号</dfn>时,我们在括号里写的是字符串<code>followers</code>(用引号括起)。方括号符号让我们能用一个变量作为属性名来访问对象的属性(请记住)。若我们在方括号中不写引号而直接写<code>followers</code>,JavaScript 引擎会将其看作一个变量,并抛出一个<code>ReferenceError: followers is not defined</code>的错误。
|
||||
</section>
|
||||
注意,在用<dfn>方括号符号</dfn>时,我们在括号里写的是字符串`followers`(用引号括起)。方括号符号让我们能用一个变量作为属性名来访问对象的属性(请记住)。若我们在方括号中不写引号而直接写`followers`,JavaScript 引擎会将其看作一个变量,并抛出一个`ReferenceError: followers is not defined`的错误。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
用这样的语法,我们还可以向对象中<em><strong>新增</strong></em>键值对。我们已经创建了一个有 3 个属性的<code>foods</code>对象,请为其新增 3 项:值为<code>13</code>的<code>bananas</code>属性、值为<code>35</code>的<code>grapes</code>属性和值为<code>27</code>的<code>strawberries</code>属性。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
用这样的语法,我们还可以向对象中***新增***键值对。我们已经创建了一个有 3 个属性的`foods`对象,请为其新增 3 项:值为`13`的`bananas`属性、值为`35`的`grapes`属性和值为`27`的`strawberries`属性。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>foods</code>应该是一个对象。
|
||||
testString: assert(typeof foods === 'object');
|
||||
- text: <code>foods</code>应该有一个值为<code>13</code>的<code>"bananas"</code>属性。
|
||||
testString: assert(foods.bananas === 13);
|
||||
- text: <code>foods</code>应该有一个值为<code>35</code>的<code>"grapes"</code>属性。
|
||||
testString: assert(foods.grapes === 35);
|
||||
- text: <code>foods</code>应该有一个值为<code>27</code>的<code>"strawberries"</code>属性。
|
||||
testString: assert(foods.strawberries === 27);
|
||||
- text: 你应该用点符号或者方括号符号来设置对象的属性。
|
||||
testString: assert(code.search(/bananas:/) === -1 && code.search(/grapes:/) === -1 && code.search(/strawberries:/) === -1);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`foods`应该是一个对象。
|
||||
|
||||
```js
|
||||
let foods = {
|
||||
apples: 25,
|
||||
oranges: 32,
|
||||
plums: 28
|
||||
};
|
||||
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
|
||||
console.log(foods);
|
||||
assert(typeof foods === 'object');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`foods`应该有一个值为`13`的`"bananas"`属性。
|
||||
|
||||
```js
|
||||
let foods = {
|
||||
apples: 25,
|
||||
oranges: 32,
|
||||
plums: 28
|
||||
};
|
||||
|
||||
// change code below this line
|
||||
foods['bananas'] = 13;
|
||||
foods['grapes'] = 35;
|
||||
foods['strawberries'] = 27;
|
||||
// change code above this line
|
||||
assert(foods.bananas === 13);
|
||||
```
|
||||
|
||||
</section>
|
||||
`foods`应该有一个值为`35`的`"grapes"`属性。
|
||||
|
||||
```js
|
||||
assert(foods.grapes === 35);
|
||||
```
|
||||
|
||||
`foods`应该有一个值为`27`的`"strawberries"`属性。
|
||||
|
||||
```js
|
||||
assert(foods.strawberries === 27);
|
||||
```
|
||||
|
||||
你应该用点符号或者方括号符号来设置对象的属性。
|
||||
|
||||
```js
|
||||
assert(
|
||||
code.search(/bananas:/) === -1 &&
|
||||
code.search(/grapes:/) === -1 &&
|
||||
code.search(/strawberries:/) === -1
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
---
|
||||
id: 587d7b7b367417b2b2512b14
|
||||
title: 使用 indexOf() 检查元素是否存在
|
||||
challengeType: 1
|
||||
forumTopicId: 301154
|
||||
title: 使用 indexOf() 检查元素是否存在
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
由于数组可以在任意时间被修改或者说<em>被改变(mutated)</em>,我们不能保证某个数据在一个给定数组中的位置,甚至不能保证该元素还存在于该数组中。幸运的是,JavaScript 给我们提供了另一个内置方法<code>indexOf()</code>。这个方法让我们可以便捷地检查某个元素是否存在于一个数组中。<code>indexOf()</code>方法接受一个元素作为输入参数,并返回该元素在数组中的位置(索引);若该元素不存在于数组中则返回<code>-1</code>。
|
||||
# --description--
|
||||
|
||||
由于数组可以在任意时间被修改或者说*被改变(mutated)*,我们不能保证某个数据在一个给定数组中的位置,甚至不能保证该元素还存在于该数组中。幸运的是,JavaScript 给我们提供了另一个内置方法`indexOf()`。这个方法让我们可以便捷地检查某个元素是否存在于一个数组中。`indexOf()`方法接受一个元素作为输入参数,并返回该元素在数组中的位置(索引);若该元素不存在于数组中则返回`-1`。
|
||||
|
||||
例如:
|
||||
|
||||
```js
|
||||
@ -18,64 +19,47 @@ fruits.indexOf('oranges'); // 返回 2
|
||||
fruits.indexOf('pears'); // 返回 1,即第一个出现的 'pears' 元素在数组中的索引为 1
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
<code>indexOf()</code>在快速检查一个数组中是否存在某个元素时非常有用。我们已经定义了一个<code>quickCheck</code>函数,它接受一个数组和一个元素作为输入参数。请修改这个函数,利用<code>indexOf()</code>方法,使得当输入的数组中含有输入的元素时,函数返回<code>true</code>;不含有输入的元素时,函数返回<code>false</code>。
|
||||
</section>
|
||||
`indexOf()`在快速检查一个数组中是否存在某个元素时非常有用。我们已经定义了一个`quickCheck`函数,它接受一个数组和一个元素作为输入参数。请修改这个函数,利用`indexOf()`方法,使得当输入的数组中含有输入的元素时,函数返回`true`;不含有输入的元素时,函数返回`false`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>quickCheck(["squash", "onions", "shallots"], "mushrooms")</code>应该返回<code>false</code>'
|
||||
testString: assert.strictEqual(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'), false);
|
||||
- text: '<code>quickCheck(["squash", "onions", "shallots"], "onions")</code>应该返回<code>true</code>'
|
||||
testString: assert.strictEqual(quickCheck(['onions', 'squash', 'shallots'], 'onions'), true);
|
||||
- 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);
|
||||
- text: '<code>quickCheck([true, false, false], undefined)</code>应返回<code>false</code>'
|
||||
testString: assert.strictEqual(quickCheck([true, false, false], undefined), false);
|
||||
- text: <code>quickCheck</code>函数应该使用<code>indexOf()</code>方法
|
||||
testString: assert.notStrictEqual(quickCheck.toString().search(/\.indexOf\(/), -1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`quickCheck(["squash", "onions", "shallots"], "mushrooms")`应该返回`false`
|
||||
|
||||
```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'));
|
||||
assert.strictEqual(
|
||||
quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'),
|
||||
false
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`quickCheck(["squash", "onions", "shallots"], "onions")`应该返回`true`
|
||||
|
||||
```js
|
||||
function quickCheck(arr, elem) {
|
||||
// change code below this line
|
||||
return arr.indexOf(elem) >= 0;
|
||||
// change code above this line
|
||||
}
|
||||
assert.strictEqual(
|
||||
quickCheck(['onions', 'squash', 'shallots'], 'onions'),
|
||||
true
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
`quickCheck([3, 5, 9, 125, 45, 2], 125)`应该返回`true`
|
||||
|
||||
```js
|
||||
assert.strictEqual(quickCheck([3, 5, 9, 125, 45, 2], 125), true);
|
||||
```
|
||||
|
||||
`quickCheck([true, false, false], undefined)`应返回`false`
|
||||
|
||||
```js
|
||||
assert.strictEqual(quickCheck([true, false, false], undefined), false);
|
||||
```
|
||||
|
||||
`quickCheck`函数应该使用`indexOf()`方法
|
||||
|
||||
```js
|
||||
assert.notStrictEqual(quickCheck.toString().search(/\.indexOf\(/), -1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
---
|
||||
id: 587d7b7d367417b2b2512b1c
|
||||
title: 检查对象是否具有某个属性
|
||||
challengeType: 1
|
||||
forumTopicId: 301155
|
||||
title: 检查对象是否具有某个属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
现在我们可以新增、修改和移除对象中的属性。但如果我们想知道一个对象中是否含有某个属性呢?JavaScript 为我们提供了两种不同的方式来实现这个功能,一个是<code>hasOwnProperty()</code>方法,另一个是<code>in</code>关键字。如果我们有一个<code>users</code>对象,它有一个<code>Alan</code>属性,我们可以用以下两种方式之一来检查该属性在对象中是否存在:
|
||||
# --description--
|
||||
|
||||
现在我们可以新增、修改和移除对象中的属性。但如果我们想知道一个对象中是否含有某个属性呢?JavaScript 为我们提供了两种不同的方式来实现这个功能,一个是`hasOwnProperty()`方法,另一个是`in`关键字。如果我们有一个`users`对象,它有一个`Alan`属性,我们可以用以下两种方式之一来检查该属性在对象中是否存在:
|
||||
|
||||
```js
|
||||
users.hasOwnProperty('Alan');
|
||||
@ -15,107 +15,73 @@ users.hasOwnProperty('Alan');
|
||||
// 都返回 true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
我们已经创建了一个含有一些用户的<code>users</code>对象和一个<code>isEveryoneHere</code>函数,该函数接受<code>users</code>对象作为参数。请完成该函数使其在<code>users</code>对象中包含以下 4 个键<code>Alan</code>、<code>Jeff</code>、<code>Sarah</code>和<code>Ryan</code>时才返回<code>true</code>,否则返回<code>false</code>。
|
||||
</section>
|
||||
我们已经创建了一个含有一些用户的`users`对象和一个`isEveryoneHere`函数,该函数接受`users`对象作为参数。请完成该函数使其在`users`对象中包含以下 4 个键`Alan`、`Jeff`、`Sarah`和`Ryan`时才返回`true`,否则返回`false`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>users</code>对象应该只含有<code>Alan</code>、<code>Jeff</code>、<code>Sarah</code>和<code>Ryan</code>4 个键。
|
||||
testString: assert("Alan" in users && "Jeff" in users && "Sarah" in users && "Ryan" in users && Object.keys(users).length === 4);
|
||||
- text: <code>isEveryoneHere</code>函数在<code>users</code>对象包含<code>Alan</code>、<code>Jeff</code>、<code>Sarah</code>和<code>Ryan</code>4 个键时应该返回<code>true</code>。
|
||||
testString: assert(isEveryoneHere(users) === true);
|
||||
- text: <code>isEveryoneHere</code>函数在<code>users</code>对象不包含<code>Alan</code>、<code>Jeff</code>、<code>Sarah</code>或<code>Ryan</code>4 个键时应该返回<code>false</code>。
|
||||
testString: assert((function() { delete users.Alan; return isEveryoneHere(users) })() === false);
|
||||
- text: 如果 <code>Jeff</code> 不是 <code>users</code> 对象的属性,函数 <code>isEveryoneHere</code> 应该返回 <code>false</code>。
|
||||
testString: assert((function() { delete users.Jeff; return isEveryoneHere(users) })() === false);
|
||||
- text: 如果 <code>Sarah</code> 不是 <code>users</code> 对象的属性,函数 <code>isEveryoneHere</code> 应该返回 <code>false</code>。
|
||||
testString: assert((function() { delete users.Sarah; return isEveryoneHere(users) })() === false);
|
||||
- text: 如果 <code>Ryan</code> 不是 <code>users</code> 对象的属性,函数 <code>isEveryoneHere</code> 应该返回 <code>false</code>。
|
||||
testString: assert((function() { delete users.Ryan; return isEveryoneHere(users) })() === false);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`users`对象应该只含有`Alan`、`Jeff`、`Sarah`和`Ryan`4 个键。
|
||||
|
||||
```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));
|
||||
assert(
|
||||
'Alan' in users &&
|
||||
'Jeff' in users &&
|
||||
'Sarah' in users &&
|
||||
'Ryan' in users &&
|
||||
Object.keys(users).length === 4
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`isEveryoneHere`函数在`users`对象包含`Alan`、`Jeff`、`Sarah`和`Ryan`4 个键时应该返回`true`。
|
||||
|
||||
```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) {
|
||||
return [
|
||||
'Alan',
|
||||
'Jeff',
|
||||
'Sarah',
|
||||
'Ryan'
|
||||
].every(i => obj.hasOwnProperty(i));
|
||||
}
|
||||
|
||||
console.log(isEveryoneHere(users));
|
||||
assert(isEveryoneHere(users) === true);
|
||||
```
|
||||
|
||||
</section>
|
||||
`isEveryoneHere`函数在`users`对象不包含`Alan`、`Jeff`、`Sarah`或`Ryan`4 个键时应该返回`false`。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
delete users.Alan;
|
||||
return isEveryoneHere(users);
|
||||
})() === false
|
||||
);
|
||||
```
|
||||
|
||||
如果 `Jeff` 不是 `users` 对象的属性,函数 `isEveryoneHere` 应该返回 `false`。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
delete users.Jeff;
|
||||
return isEveryoneHere(users);
|
||||
})() === false
|
||||
);
|
||||
```
|
||||
|
||||
如果 `Sarah` 不是 `users` 对象的属性,函数 `isEveryoneHere` 应该返回 `false`。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
delete users.Sarah;
|
||||
return isEveryoneHere(users);
|
||||
})() === false
|
||||
);
|
||||
```
|
||||
|
||||
如果 `Ryan` 不是 `users` 对象的属性,函数 `isEveryoneHere` 应该返回 `false`。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
delete users.Ryan;
|
||||
return isEveryoneHere(users);
|
||||
})() === false
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
---
|
||||
id: 587d7b7b367417b2b2512b17
|
||||
title: 组合使用数组和扩展运算符
|
||||
challengeType: 1
|
||||
forumTopicId: 301156
|
||||
title: 组合使用数组和扩展运算符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
<dfn>展开运算符</dfn>的另一个大用处是合并数组,或者将某个数组的所有元素插入到另一个数组的任意位置。用传统的语法我们也可以连接两个数组,但只能两个数组首尾相接。而展开语法能使下面的操作变得极其简单:
|
||||
|
||||
```js
|
||||
@ -17,61 +17,24 @@ let thatArray = ['basil', 'cilantro', ...thisArray, 'coriander'];
|
||||
```
|
||||
|
||||
使用展开语法,我们这样就实现了一个用传统方法要写得很复杂冗长的操作。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
我们已经定义了一个返回<code>sentence</code>变量的<code>spreadOut</code>函数,请修改该函数,利用<dfn>展开运算符</dfn>使该函数返回数组<code>['learning', 'to', 'code', 'is', 'fun']</code>。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
我们已经定义了一个返回`sentence`变量的`spreadOut`函数,请修改该函数,利用<dfn>展开运算符</dfn>使该函数返回数组`['learning', 'to', 'code', 'is', 'fun']`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>spreadOut</code>应该返回<code>["learning", "to", "code", "is", "fun"]</code>'
|
||||
testString: assert.deepEqual(spreadOut(), ['learning', 'to', 'code', 'is', 'fun']);
|
||||
- text: <code>spreadOut</code>函数里应该用到展开语法
|
||||
testString: assert.notStrictEqual(spreadOut.toString().search(/[...]/), -1);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`spreadOut`应该返回`["learning", "to", "code", "is", "fun"]`
|
||||
|
||||
```js
|
||||
function spreadOut() {
|
||||
let fragment = ['to', 'code'];
|
||||
let sentence; // change this line
|
||||
return sentence;
|
||||
}
|
||||
|
||||
// do not change code below this line
|
||||
console.log(spreadOut());
|
||||
assert.deepEqual(spreadOut(), ['learning', 'to', 'code', 'is', 'fun']);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`spreadOut`函数里应该用到展开语法
|
||||
|
||||
```js
|
||||
// solution required
|
||||
|
||||
function spreadOut() {
|
||||
let fragment = ['to', 'code'];
|
||||
let sentence = ['learning', ...fragment, 'is', 'fun'];
|
||||
return sentence;
|
||||
}
|
||||
|
||||
assert.notStrictEqual(spreadOut.toString().search(/[...]/), -1);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
---
|
||||
id: 587d7b7b367417b2b2512b13
|
||||
title: 使用扩展运算符复制数组
|
||||
challengeType: 1
|
||||
forumTopicId: 301157
|
||||
title: 使用扩展运算符复制数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<code>slice()</code>已经能让我们从一个数组中选择一些元素来复制到新数组中了,而 ES6 中又新引入了一个简洁且可读性强的语法<dfn>展开运算符(spread operator)</dfn>,它能让我们方便地复制数组中的<em>所有</em>元素。展开语法是这样的:<code>...</code>
|
||||
# --description--
|
||||
|
||||
`slice()`已经能让我们从一个数组中选择一些元素来复制到新数组中了,而 ES6 中又新引入了一个简洁且可读性强的语法<dfn>展开运算符(spread operator)</dfn>,它能让我们方便地复制数组中的*所有*元素。展开语法是这样的:`...`
|
||||
|
||||
在实践中,我们可以这样用展开运算符来复制一个数组:
|
||||
|
||||
```js
|
||||
@ -17,82 +18,54 @@ let thatArray = [...thisArray];
|
||||
// thisArray 保持不变,等于 thatArray
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
我们已经定义了一个<code>copyMachine</code>函数,它接受<code>arr</code>(一个数组)和<code>num</code>(一个数字)作为输入参数。该函数应该返回一个由<code>num</code>个<code>arr</code>组成的新数组。我们已经为你写好了大部分的代码,但它还不能正确地工作。请修改这个函数,使用展开语法,使该函数正确工作(提示:我们已经学到过的一个方法很适合用在这里!)
|
||||
</section>
|
||||
我们已经定义了一个`copyMachine`函数,它接受`arr`(一个数组)和`num`(一个数字)作为输入参数。该函数应该返回一个由`num`个`arr`组成的新数组。我们已经为你写好了大部分的代码,但它还不能正确地工作。请修改这个函数,使用展开语法,使该函数正确工作(提示:我们已经学到过的一个方法很适合用在这里!)
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```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]]);
|
||||
- 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]]);
|
||||
- text: '<code>copyMachine([true, true, null], 1)</code>应该返回<code>[[true, true, null]]</code>'
|
||||
testString: assert.deepEqual(copyMachine([true, true, null], 1), [[true, true, null]]);
|
||||
- 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']]);
|
||||
- text: <code>copyMachine</code>函数中应该对数组<code>arr</code>使用<code>spread operator</code>。
|
||||
testString: assert(removeJSComments(code).match(/\.\.\.arr/));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`copyMachine([true, false, true], 2)`应该返回`[[true, false, true], [true, false, true]]`
|
||||
|
||||
```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));
|
||||
assert.deepEqual(copyMachine([true, false, true], 2), [
|
||||
[true, false, true],
|
||||
[true, false, true]
|
||||
]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`copyMachine([1, 2, 3], 5)`应该返回`[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]`
|
||||
|
||||
```js
|
||||
const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, '');
|
||||
assert.deepEqual(copyMachine([1, 2, 3], 5), [
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
[1, 2, 3]
|
||||
]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`copyMachine([true, true, null], 1)`应该返回`[[true, true, null]]`
|
||||
|
||||
```js
|
||||
function copyMachine(arr,num){
|
||||
let newArr=[];
|
||||
while(num >=1){
|
||||
// change code below this line
|
||||
newArr.push([...arr]);
|
||||
//change code above this line
|
||||
num--;
|
||||
}
|
||||
return newArr;
|
||||
}
|
||||
console.log(copyMachine([true, false, true], 2));
|
||||
assert.deepEqual(copyMachine([true, true, null], 1), [[true, true, null]]);
|
||||
```
|
||||
|
||||
</section>
|
||||
`copyMachine(["it works"], 3)`应该返回`[["it works"], ["it works"], ["it works"]]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(copyMachine(['it works'], 3), [
|
||||
['it works'],
|
||||
['it works'],
|
||||
['it works']
|
||||
]);
|
||||
```
|
||||
|
||||
`copyMachine`函数中应该对数组`arr`使用`spread operator`。
|
||||
|
||||
```js
|
||||
assert(removeJSComments(code).match(/\.\.\.arr/));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
---
|
||||
id: 587d7b7a367417b2b2512b12
|
||||
title: 使用 slice() 拷贝数组项目
|
||||
challengeType: 1
|
||||
forumTopicId: 301158
|
||||
title: 使用 slice() 拷贝数组项目
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
接下来我们要介绍<code>slice()</code>方法。<code>slice()</code>并不修改数组,而是复制或者说<em>提取(extract)</em>给定数量的元素到一个新数组里,而调用方法的数组则保持不变。<code>slice()</code>只接受 2 个输入参数—第一个是开始提取元素的位置(索引),第二个是结束提取元素的位置(索引)。slice 方法会提取直到截止索引的元素,但被提取的元素不包括截止索引对应的元素。请看以下例子:
|
||||
# --description--
|
||||
|
||||
接下来我们要介绍`slice()`方法。`slice()`并不修改数组,而是复制或者说*提取(extract)*给定数量的元素到一个新数组里,而调用方法的数组则保持不变。`slice()`只接受 2 个输入参数—第一个是开始提取元素的位置(索引),第二个是结束提取元素的位置(索引)。slice 方法会提取直到截止索引的元素,但被提取的元素不包括截止索引对应的元素。请看以下例子:
|
||||
|
||||
```js
|
||||
let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];
|
||||
@ -18,56 +18,27 @@ let todaysWeather = weatherConditions.slice(1, 3);
|
||||
```
|
||||
|
||||
现在我们从一个已有的数组中提取了一些元素,并用这些元素创建了一个新数组。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
我们已经定义了一个<code>forecast</code>函数,它接受一个数组作为参数。请修改这个函数,利用<code>slice()</code>来从输入的数组中提取信息,并返回一个包含元素<code>'warm'</code>和<code>'sunny'</code> 的新数组。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
我们已经定义了一个`forecast`函数,它接受一个数组作为参数。请修改这个函数,利用`slice()`来从输入的数组中提取信息,并返回一个包含元素`'warm'`和`'sunny'` 的新数组。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>forecast</code>应该返回<code>["warm", "sunny"]</code>'
|
||||
testString: assert.deepEqual(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']), ['warm', 'sunny']);
|
||||
- text: <code>forecast</code>函数应该使用<code>slice()</code>方法
|
||||
testString: assert(/\.slice\(/.test(code));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`forecast`应该返回`["warm", "sunny"]`
|
||||
|
||||
```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']));
|
||||
assert.deepEqual(
|
||||
forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']),
|
||||
['warm', 'sunny']
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`forecast`函数应该使用`slice()`方法
|
||||
|
||||
```js
|
||||
function forecast(arr) {
|
||||
return arr.slice(2,4);
|
||||
}
|
||||
assert(/\.slice\(/.test(code));
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
---
|
||||
id: 587d7b7b367417b2b2512b16
|
||||
title: 创建复杂的多维数组
|
||||
challengeType: 1
|
||||
forumTopicId: 301159
|
||||
title: 创建复杂的多维数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
很好!你已经学到很多关于数组的知识了!但这些只是一个开始,你将在接下来的小节中学习到与数组相关的更多知识。但在继续去学习<dfn>对象</dfn>(<dfn>Objects</dfn>)之前,让我们再花一点时间看一看,数组怎样能够变得比之前的挑战中更复杂一点。
|
||||
|
||||
数组的一个强大的特性是,它可以包含其他数组,甚至完全由其他数组组成。我们已经在上一个挑战中看到了包含数组的数组,但它还算是比较简单的。数组中的数组还可以在包含其他数组,数组中是可以嵌套任意层的数组的。数组从而可以被用来实现非常复杂的叫做<dfn>多维(multi-dimensional)</dfn>或嵌套(nested)数组的数据结构。请看如下例子:
|
||||
|
||||
```js
|
||||
@ -29,8 +30,7 @@ let nestedArray = [ // 顶层,或第 1 层——最外层的数组
|
||||
];
|
||||
```
|
||||
|
||||
虽然这个例子看起来错综复杂,但这样复杂的数组并不算罕见,尤其是在处理大量数据的时候。
|
||||
但我们仍能简单地用方括号符号来访问到嵌套得最深的数组:
|
||||
虽然这个例子看起来错综复杂,但这样复杂的数组并不算罕见,尤其是在处理大量数据的时候。 但我们仍能简单地用方括号符号来访问到嵌套得最深的数组:
|
||||
|
||||
```js
|
||||
console.log(nestedArray[2][1][0][0][0]);
|
||||
@ -46,69 +46,145 @@ console.log(nestedArray[2][1][0][0][0]);
|
||||
// now logs: deeper still
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
我们已经定义了一个<code>myNestedArray</code>数组变量。请修改<code>myNestedArray</code>,用<dfn>字符串(string)</dfn>、<dfn>数字(number)</dfn>或者<dfn>布尔值(boolean)</dfn>作为数组的数据元素,使得<code>myNestedArray</code>刚好有 5 层数组嵌套(记住,最外层的数组是第 1 层)。请在第 3 层的数组中包含字符串<code>'deep'</code>,在第 4 层的数组中包含字符串<code>'deeper'</code>,在第 5 层的数组中包含字符串<code>'deepest'</code>。
|
||||
</section>
|
||||
我们已经定义了一个`myNestedArray`数组变量。请修改`myNestedArray`,用<dfn>字符串(string)</dfn>、<dfn>数字(number)</dfn>或者<dfn>布尔值(boolean)</dfn>作为数组的数据元素,使得`myNestedArray`刚好有 5 层数组嵌套(记住,最外层的数组是第 1 层)。请在第 3 层的数组中包含字符串`'deep'`,在第 4 层的数组中包含字符串`'deeper'`,在第 5 层的数组中包含字符串`'deepest'`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```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);'
|
||||
- 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);'
|
||||
- text: <code>myNestedArray</code>里应该有且只有一个字符串<code>"deep"</code>,并且应该出现在第 3 层数组中。
|
||||
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);
|
||||
- text: <code>myNestedArray</code>里应该有且只有一个字符串<code>"deeper"</code>,并且应该出现在第 4 层数组中。
|
||||
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);
|
||||
- text: <code>myNestedArray</code>里应该有且只有一个字符串<code>"deepest"</code>,并且应该出现在第 5 层数组中。
|
||||
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);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myNestedArray`中的数据元素应当只能是字符串、数字或者布尔值。
|
||||
|
||||
```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
|
||||
];
|
||||
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
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`myNestedArray`应该刚好有 5 层数组嵌套。
|
||||
|
||||
```js
|
||||
let myNestedArray = [
|
||||
// change code below this line
|
||||
['unshift', ['deep', ['deeper', ['deepest']]],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
|
||||
];
|
||||
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
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
`myNestedArray`里应该有且只有一个字符串`"deep"`,并且应该出现在第 3 层数组中。
|
||||
|
||||
```js
|
||||
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
|
||||
);
|
||||
```
|
||||
|
||||
`myNestedArray`里应该有且只有一个字符串`"deeper"`,并且应该出现在第 4 层数组中。
|
||||
|
||||
```js
|
||||
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
|
||||
);
|
||||
```
|
||||
|
||||
`myNestedArray`里应该有且只有一个字符串`"deepest"`,并且应该出现在第 5 层数组中。
|
||||
|
||||
```js
|
||||
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
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,102 +1,51 @@
|
||||
---
|
||||
id: 587d7b7d367417b2b2512b1e
|
||||
title: 使用 Object.Keys() 生成对象所有键组成的数组
|
||||
challengeType: 1
|
||||
forumTopicId: 301160
|
||||
title: 使用 Object.Keys() 生成对象所有键组成的数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
我们还可以输入一个对象作为参数来调用<code>Object.keys()</code>方法,使其生成一个包含对象中所有键的数组。这会返回一个由对象中所有键的名称(字符串)组成的数组。再次说明,这个数组中的项的顺序是不确定的。
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
请你完成<code>getArrayOfUsers</code>函数,使其返回一个包含输入的对象的所有属性的数组。
|
||||
</section>
|
||||
我们还可以输入一个对象作为参数来调用`Object.keys()`方法,使其生成一个包含对象中所有键的数组。这会返回一个由对象中所有键的名称(字符串)组成的数组。再次说明,这个数组中的项的顺序是不确定的。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>users</code>对象应该只包含<code>Alan</code>、<code>Jeff</code>、<code>Sarah</code>和<code>Ryan</code>这 4 个键
|
||||
testString: assert('Alan' in users && 'Jeff' in users && 'Sarah' in users && 'Ryan' in users && Object.keys(users).length === 4);
|
||||
- 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);
|
||||
请你完成`getArrayOfUsers`函数,使其返回一个包含输入的对象的所有属性的数组。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`users`对象应该只包含`Alan`、`Jeff`、`Sarah`和`Ryan`这 4 个键
|
||||
|
||||
```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));
|
||||
assert(
|
||||
'Alan' in users &&
|
||||
'Jeff' in users &&
|
||||
'Sarah' in users &&
|
||||
'Ryan' in users &&
|
||||
Object.keys(users).length === 4
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`getArrayOfUsers`函数应该返回一个包含`users`对象中所有键的数组
|
||||
|
||||
```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) {
|
||||
return Object.keys(obj);
|
||||
}
|
||||
|
||||
console.log(getArrayOfUsers(users));
|
||||
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
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
---
|
||||
id: 587d7b7b367417b2b2512b15
|
||||
title: 使用 For 循环迭代数组的所有项
|
||||
challengeType: 1
|
||||
forumTopicId: 301161
|
||||
title: 使用 For 循环迭代数组的所有项
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
在进行与数组有关的编程时,我们有时需要遍历数组的所有元素来找出我们需要的元素,或者对数组执行特定的操作。JavaScript 提供了几个内置的方法,它们以不同的方式遍历数组来获得不同的结果(如<code>every()</code>、<code>forEach()</code>、<code>map()</code>等等)。而简单的<code>for</code>循环不仅能实现这些功能,而且相比之下也更灵活。
|
||||
# --description--
|
||||
|
||||
在进行与数组有关的编程时,我们有时需要遍历数组的所有元素来找出我们需要的元素,或者对数组执行特定的操作。JavaScript 提供了几个内置的方法,它们以不同的方式遍历数组来获得不同的结果(如`every()`、`forEach()`、`map()`等等)。而简单的`for`循环不仅能实现这些功能,而且相比之下也更灵活。
|
||||
|
||||
请看以下例子:
|
||||
|
||||
```js
|
||||
@ -25,73 +26,86 @@ greaterThanTen([2, 12, 8, 14, 80, 0, 1]);
|
||||
// 返回 [12, 14, 80]
|
||||
```
|
||||
|
||||
这个函数使用一个<code>for</code>循环来遍历一个数组,逐一对其中的元素进行测试。我们用这个方法简单地以编程的方式找出了数组中大于<code>10</code>的元素,并返回了一个包含这些元素的数组。
|
||||
</section>
|
||||
这个函数使用一个`for`循环来遍历一个数组,逐一对其中的元素进行测试。我们用这个方法简单地以编程的方式找出了数组中大于`10`的元素,并返回了一个包含这些元素的数组。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
我们已经定义了一个<code>filteredArray</code>函数,它接受一个嵌套的数组参数<code>arr</code>以及一个<code>elem</code>参数,并要返回一个新数组。<code>arr</code>数组中的数组可能包含<code>elem</code>元素,也可能不包含。请修改该函数,用一个<code>for</code>循环来做筛选,使函数返回一个由<code>arr</code>中不包含<code>elem</code>的数组组成的新数组。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
我们已经定义了一个`filteredArray`函数,它接受一个嵌套的数组参数`arr`以及一个`elem`参数,并要返回一个新数组。`arr`数组中的数组可能包含`elem`元素,也可能不包含。请修改该函数,用一个`for`循环来做筛选,使函数返回一个由`arr`中不包含`elem`的数组组成的新数组。
|
||||
|
||||
```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]]);
|
||||
- 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]]);
|
||||
- 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']]);
|
||||
- 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), []);
|
||||
- text: <code>filteredArray</code>函数应该使用<code>for</code>循环
|
||||
testString: assert.notStrictEqual(filteredArray.toString().search(/for/), -1);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)`应该返回`[ [10, 8, 3], [14, 6, 23] ]`
|
||||
|
||||
```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));
|
||||
assert.deepEqual(
|
||||
filteredArray(
|
||||
[
|
||||
[10, 8, 3],
|
||||
[14, 6, 23],
|
||||
[3, 18, 6]
|
||||
],
|
||||
18
|
||||
),
|
||||
[
|
||||
[10, 8, 3],
|
||||
[14, 6, 23]
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)`应返回`[ ["flutes", 4] ]`
|
||||
|
||||
```js
|
||||
function filteredArray(arr, elem) {
|
||||
let newArr = [];
|
||||
// change code below this line
|
||||
for (let i = 0; i<arr.length; i++) {
|
||||
if (arr[i].indexOf(elem) < 0) {
|
||||
newArr.push(arr[i]);
|
||||
}
|
||||
}
|
||||
// change code above this line
|
||||
return newArr;
|
||||
}
|
||||
assert.deepEqual(
|
||||
filteredArray(
|
||||
[
|
||||
['trumpets', 2],
|
||||
['flutes', 4],
|
||||
['saxophones', 2]
|
||||
],
|
||||
2
|
||||
),
|
||||
[['flutes', 4]]
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
`filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")`应该返回`[ ["amy", "beth", "sam"] ]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
filteredArray(
|
||||
[
|
||||
['amy', 'beth', 'sam'],
|
||||
['dave', 'sean', 'peter']
|
||||
],
|
||||
'peter'
|
||||
),
|
||||
[['amy', 'beth', 'sam']]
|
||||
);
|
||||
```
|
||||
|
||||
`filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)`应该返回`[ ]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
filteredArray(
|
||||
[
|
||||
[3, 2, 3],
|
||||
[1, 6, 3],
|
||||
[3, 13, 26],
|
||||
[19, 3, 9]
|
||||
],
|
||||
3
|
||||
),
|
||||
[]
|
||||
);
|
||||
```
|
||||
|
||||
`filteredArray`函数应该使用`for`循环
|
||||
|
||||
```js
|
||||
assert.notStrictEqual(filteredArray.toString().search(/for/), -1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,13 @@
|
||||
---
|
||||
id: 587d7b7d367417b2b2512b1d
|
||||
title: 使用 for...in 语句迭代对象
|
||||
challengeType: 1
|
||||
forumTopicId: 301162
|
||||
title: '使用 for...in 语句迭代对象'
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
有时候你需要遍历一个对象中的所有键。这需要 JavaScript 中的一个特殊语法:<dfn>for...in</dfn> 语句。以遍历 <code>users</code> 对象的键为例:
|
||||
有时候你需要遍历一个对象中的所有键。这需要 JavaScript 中的一个特殊语法:<dfn>for...in</dfn> 语句。以遍历 `users` 对象的键为例:
|
||||
|
||||
```js
|
||||
for (let user in users) {
|
||||
@ -22,15 +21,12 @@ Sarah
|
||||
Ryan
|
||||
```
|
||||
|
||||
在这个语句中,我们定义了一个<code>user</code>变量,你可以看到,这个变量在 for...in 语句对对象的每一个键的遍历中都会被重置。
|
||||
<strong>注意:</strong><br>跟数组不同,对象中的键是无序的,因此一个对象中某个键的位置,或者说它出现的相对顺序,在引用或访问该键时是不确定的。
|
||||
</section>
|
||||
在这个语句中,我们定义了一个`user`变量,你可以看到,这个变量在 for...in 语句对对象的每一个键的遍历中都会被重置。 **注意:**
|
||||
跟数组不同,对象中的键是无序的,因此一个对象中某个键的位置,或者说它出现的相对顺序,在引用或访问该键时是不确定的。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
我们已经定义了一个<code>countOnline</code>函数,请在其中使用一个 <dfn>for...in</dfn> 语句来遍历<code>users</code>对象中的用户,并返回<code>online</code>属性为<code>true</code>的用户的数量。
|
||||
# --instructions--
|
||||
|
||||
我们已经定义了一个`countOnline`函数,请在其中使用一个 <dfn>for...in</dfn> 语句来遍历`users`对象中的用户,并返回`online`属性为`true`的用户的数量。
|
||||
|
||||
```js
|
||||
{
|
||||
@ -46,102 +42,35 @@ Ryan
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 函数 <code>countOnline</code> 应该使用 `for in` 语句遍历传入对象的key。
|
||||
testString: assert(code.match(/for\s*\(\s*(var|let)\s+[a-zA-Z_$]\w*\s+in\s+[a-zA-Z_$]\w*\s*\)\s*{/));
|
||||
- text: '当传入 <code>{ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }</code> 时,函数 <code>countOnline</code> 应该返回 <code>1</code>。'
|
||||
testString: assert(countOnline(usersObj1) === 1);
|
||||
- text: '当传入 <code>{ Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }</code> 时,函数 <code>countOnline</code> 应该返回 <code>2</code>。'
|
||||
testString: assert(countOnline(usersObj2) === 2);
|
||||
- text: '当传入 <code>{ Alan: { online: false }, Jeff: { online: false }, Sarah: { online: false } }</code> 时,函数 <code>countOnline</code> 应该返回 <code>0</code>。'
|
||||
testString: assert(countOnline(usersObj3) === 0);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
函数 `countOnline` 应该使用 `for in` 语句遍历传入对象的key。
|
||||
|
||||
```js
|
||||
function countOnline(usersObj) {
|
||||
// change code below this line
|
||||
|
||||
// change code above this line
|
||||
}
|
||||
assert(
|
||||
code.match(
|
||||
/for\s*\(\s*(var|let)\s+[a-zA-Z_$]\w*\s+in\s+[a-zA-Z_$]\w*\s*\)\s*{/
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
当传入 `{ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }` 时,函数 `countOnline` 应该返回 `1`。
|
||||
|
||||
```js
|
||||
const usersObj1 = {
|
||||
Alan: {
|
||||
online: false
|
||||
},
|
||||
Jeff: {
|
||||
online: true
|
||||
},
|
||||
Sarah: {
|
||||
online: false
|
||||
}
|
||||
}
|
||||
|
||||
const usersObj2 = {
|
||||
Alan: {
|
||||
online: true
|
||||
},
|
||||
Jeff: {
|
||||
online: false
|
||||
},
|
||||
Sarah: {
|
||||
online: true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const usersObj3 = {
|
||||
Alan: {
|
||||
online: false
|
||||
},
|
||||
Jeff: {
|
||||
online: false
|
||||
},
|
||||
Sarah: {
|
||||
online: false
|
||||
}
|
||||
}
|
||||
assert(countOnline(usersObj1) === 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
当传入 `{ Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }` 时,函数 `countOnline` 应该返回 `2`。
|
||||
|
||||
```js
|
||||
|
||||
function countOnline(usersObj) {
|
||||
let online = 0;
|
||||
for(let user in usersObj){
|
||||
if(usersObj[user].online) {
|
||||
online++;
|
||||
}
|
||||
}
|
||||
return online;
|
||||
}
|
||||
|
||||
assert(countOnline(usersObj2) === 2);
|
||||
```
|
||||
|
||||
</section>
|
||||
当传入 `{ Alan: { online: false }, Jeff: { online: false }, Sarah: { online: false } }` 时,函数 `countOnline` 应该返回 `0`。
|
||||
|
||||
```js
|
||||
assert(countOnline(usersObj3) === 0);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,105 +1,51 @@
|
||||
---
|
||||
id: 587d7b7d367417b2b2512b1f
|
||||
title: 修改存储在对象中的数组
|
||||
challengeType: 1
|
||||
forumTopicId: 301163
|
||||
title: 修改存储在对象中的数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
现在你已经接触到 JavaScript 对象的所有运算。你可以增加、修改和移除键值对,检查某个键是否存在,并且遍历一个对象中的所有键。在你继续学习 JavaScript 的过程中,你会看到对象的更多用法。另外,后续的《高级数据结构》课程还会介绍 ES6 的 <dfn>Map</dfn> 和 <dfn>Set</dfn> 对象。这两种对象都跟一般的对象相似,但它们提供了一些额外的特性。现在你已经学到了数组和对象的基础知识,你已经可以继续用 JavaScript 来解决更加复杂的问题了!
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
请你看一下代码编辑器中我们提供的对象。<code>user</code>对象包含 3 个键。<code>data</code>对象包含 5 个键,其中一个包含一个<code>friends</code>数组。从这个例子你可以看到对象作为数据结构是多么的灵活。我们已经写了<code>addFriend</code>函数的一部分,请你完成这个函数,使其接受一个<code>user</code>对象,将<code>friend</code>参数中的名字添加到<code>user.data.friends</code>数组中并返回该数组。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
请你看一下代码编辑器中我们提供的对象。`user`对象包含 3 个键。`data`对象包含 5 个键,其中一个包含一个`friends`数组。从这个例子你可以看到对象作为数据结构是多么的灵活。我们已经写了`addFriend`函数的一部分,请你完成这个函数,使其接受一个`user`对象,将`friend`参数中的名字添加到`user.data.friends`数组中并返回该数组。
|
||||
|
||||
```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);
|
||||
- text: <code>addFriend</code>函数应该接受一个<code>user</code>对象和一个<code>friend</code>字符串作为输入参数,并将 friend 插入到<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); })());
|
||||
- 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']);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`user`对象应该包含`name`、`age`和`data`三个键。
|
||||
|
||||
```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'));
|
||||
assert('name' in user && 'age' in user && 'data' in user);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`addFriend`函数应该接受一个`user`对象和一个`friend`字符串作为输入参数,并将 friend 插入到`user`对象的`friends`数组中。
|
||||
|
||||
```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) {
|
||||
userObj.data.friends.push(friend);
|
||||
return userObj.data.friends;
|
||||
}
|
||||
assert(
|
||||
(function () {
|
||||
let L1 = user.data.friends.length;
|
||||
addFriend(user, 'Sean');
|
||||
let L2 = user.data.friends.length;
|
||||
return L2 === L1 + 1;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
`addFriend(user, "Pete")`应该返回`["Sam", "Kira", "Tomo", "Pete"]`。
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
(function () {
|
||||
delete user.data.friends;
|
||||
user.data.friends = ['Sam', 'Kira', 'Tomo'];
|
||||
return addFriend(user, 'Pete');
|
||||
})(),
|
||||
['Sam', 'Kira', 'Tomo', 'Pete']
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
---
|
||||
id: 587d7b7c367417b2b2512b19
|
||||
title: 修改嵌套在对象中的对象
|
||||
challengeType: 1
|
||||
forumTopicId: 301164
|
||||
title: 修改嵌套在对象中的对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
现在我们来看一个稍微复杂一点的对象。对象中也可以嵌套任意层的对象。对象的属性值可以是 JavaScript 支持的任意类型,包括数组和其他对象。请看以下例子:
|
||||
|
||||
```js
|
||||
@ -24,74 +24,39 @@ let nestedObject = {
|
||||
};
|
||||
```
|
||||
|
||||
<code>nestedObject</code>有 3 个唯一的键:值为一个数字的<code>id</code>、值为一个字符串的<code>date</code>和值为一个嵌套了其他对象的对象的<code>data</code>。虽然对象中的数据可能很复杂,我们仍能使用上一个挑战中讲的符号来访问我们需要的信息。
|
||||
</section>
|
||||
`nestedObject`有 3 个唯一的键:值为一个数字的`id`、值为一个字符串的`date`和值为一个嵌套了其他对象的对象的`data`。虽然对象中的数据可能很复杂,我们仍能使用上一个挑战中讲的符号来访问我们需要的信息。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
我们已经定义了一个<code>userActivity</code>对象,它包含了另一个对象。你可以用上一个挑战中那样的方式来修改被嵌套的对象的属性。请将<code>online</code>属性设为<code>45</code>。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
我们已经定义了一个`userActivity`对象,它包含了另一个对象。你可以用上一个挑战中那样的方式来修改被嵌套的对象的属性。请将`online`属性设为`45`。
|
||||
|
||||
```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);
|
||||
- text: <code>userActivity</code>应该有一个<code>data</code>属性,该属性要是一个含有<code>totalUsers</code>和<code>online</code>属性的对象。
|
||||
testString: assert('totalUsers' in userActivity.data && 'online' in userActivity.data);
|
||||
- text: <code>userActivity</code>的<code>data</code>属性值中的<code>online</code>属性应该被设为<code>45</code>。
|
||||
testString: assert(userActivity.data.online === 45);
|
||||
- text: 你应该用点符号或者方括号符号来设置<code>online</code>属性。
|
||||
testString: 'assert.strictEqual(code.search(/online: 45/), -1);'
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`userActivity`应该含有`id`、`date`和`data`属性。
|
||||
|
||||
```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);
|
||||
assert(
|
||||
'id' in userActivity && 'date' in userActivity && 'data' in userActivity
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`userActivity`应该有一个`data`属性,该属性要是一个含有`totalUsers`和`online`属性的对象。
|
||||
|
||||
```js
|
||||
let userActivity = {
|
||||
id: 23894201352,
|
||||
date: 'January 1, 2017',
|
||||
data: {
|
||||
totalUsers: 51,
|
||||
online: 42
|
||||
}
|
||||
};
|
||||
|
||||
userActivity.data.online = 45;
|
||||
assert('totalUsers' in userActivity.data && 'online' in userActivity.data);
|
||||
```
|
||||
|
||||
</section>
|
||||
`userActivity`的`data`属性值中的`online`属性应该被设为`45`。
|
||||
|
||||
```js
|
||||
assert(userActivity.data.online === 45);
|
||||
```
|
||||
|
||||
你应该用点符号或者方括号符号来设置`online`属性。
|
||||
|
||||
```js
|
||||
assert.strictEqual(code.search(/online: 45/), -1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
---
|
||||
id: 587d78b2367417b2b2512b0f
|
||||
title: 使用 pop() 和 shift() 从数组中删除项目
|
||||
challengeType: 1
|
||||
forumTopicId: 301165
|
||||
title: 使用 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>的关键区别在于,前者不能接受输入参数,而且每次只能修改数组中的一个元素。
|
||||
# --description--
|
||||
|
||||
`push()`和`unshift()`都分别有一个作用基本与之相反的函数:`pop()`和`shift()`。你现在或许已经猜到,与插入元素相反,`pop()`从数组的末尾*移除*一个元素,而`shift()`从数组的开头移除一个元素。`pop()`和`shift()`与对应的`push()`和`unshift()`的关键区别在于,前者不能接受输入参数,而且每次只能修改数组中的一个元素。
|
||||
|
||||
让我们来看以下的例子:
|
||||
|
||||
```js
|
||||
@ -28,60 +29,32 @@ let popped = greetings.pop();
|
||||
// greetings now equals []
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
我们已经定义了一个<code>popShift</code>函数,它会接收一个数组作为输入参数并返回一个新的数组。请你修改这个函数,使用<code>pop()</code>和<code>shift()</code>来移除输入的数组的第一个元素和最后一个元素,并将这两个被移除的元素赋值给对应的变量,使得返回的数组包含它们的值。
|
||||
</section>
|
||||
我们已经定义了一个`popShift`函数,它会接收一个数组作为输入参数并返回一个新的数组。请你修改这个函数,使用`pop()`和`shift()`来移除输入的数组的第一个元素和最后一个元素,并将这两个被移除的元素赋值给对应的变量,使得返回的数组包含它们的值。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>popShift(["challenge", "is", "not", "complete"])</code>应返回<code>["challenge", "complete"]</code>'
|
||||
testString: assert.deepEqual(popShift(['challenge', 'is', 'not', 'complete']), ["challenge", "complete"]);
|
||||
- text: <code>popShift</code>函数应该使用<code>pop()</code>方法
|
||||
testString: assert.notStrictEqual(popShift.toString().search(/\.pop\(/), -1);
|
||||
- text: <code>popShift</code>函数应该使用<code>shift()</code>方法
|
||||
testString: assert.notStrictEqual(popShift.toString().search(/\.shift\(/), -1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`popShift(["challenge", "is", "not", "complete"])`应返回`["challenge", "complete"]`
|
||||
|
||||
```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']));
|
||||
assert.deepEqual(popShift(['challenge', 'is', 'not', 'complete']), [
|
||||
'challenge',
|
||||
'complete'
|
||||
]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`popShift`函数应该使用`pop()`方法
|
||||
|
||||
```js
|
||||
function popShift(arr) {
|
||||
let popped = arr.pop(); // change this line
|
||||
let shifted = arr.shift(); // change this line
|
||||
return [shifted, popped];
|
||||
}
|
||||
assert.notStrictEqual(popShift.toString().search(/\.pop\(/), -1);
|
||||
```
|
||||
|
||||
</section>
|
||||
`popShift`函数应该使用`shift()`方法
|
||||
|
||||
```js
|
||||
assert.notStrictEqual(popShift.toString().search(/\.shift\(/), -1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d78b2367417b2b2512b10
|
||||
title: 使用 splice() 删除项目
|
||||
challengeType: 1
|
||||
forumTopicId: 301166
|
||||
title: 使用 splice() 删除项目
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
在上面的挑战中,我们已经学到了如何利用<code>shift()</code>和<code>pop()</code>从数组的开头或者末尾移除元素,但如果我们想移除数组中间的一个元素呢?或者想一次移除多个元素呢?这时候我们就需要<code>splice()</code>了。<code>splice()</code>让我们可以从数组中的任意位置<strong>移除任意数量的连续的元素</strong>。
|
||||
<code>splice()</code>最多可以接受 3 个参数,但现在我们先关注前两个。<code>splice()</code>接收的前两个参数基于调用<code>splice()</code>数组中元素的索引。记住,数组的索引是<em>从 0 开始的</em>(<em>zero-indexed</em>),所以我们要用<code>0</code>来指示数组中的第一个元素。<code>splice()</code>的第一个参数代表从数组中的哪个索引开始移除元素,而第二个参数指示要从数组中删除多少个元素。例如:
|
||||
# --description--
|
||||
|
||||
在上面的挑战中,我们已经学到了如何利用`shift()`和`pop()`从数组的开头或者末尾移除元素,但如果我们想移除数组中间的一个元素呢?或者想一次移除多个元素呢?这时候我们就需要`splice()`了。`splice()`让我们可以从数组中的任意位置**移除任意数量的连续的元素**。
|
||||
|
||||
`splice()`最多可以接受 3 个参数,但现在我们先关注前两个。`splice()`接收的前两个参数基于调用`splice()`数组中元素的索引。记住,数组的索引是*从 0 开始的*(*zero-indexed*),所以我们要用`0`来指示数组中的第一个元素。`splice()`的第一个参数代表从数组中的哪个索引开始移除元素,而第二个参数指示要从数组中删除多少个元素。例如:
|
||||
|
||||
```js
|
||||
let array = ['today', 'was', 'not', 'so', 'great'];
|
||||
@ -18,7 +19,7 @@ array.splice(2, 2);
|
||||
// array 现在是 ['today', 'was', 'great']
|
||||
```
|
||||
|
||||
<code>splice()</code>不仅从被调用的数组中移除元素,还会返回一个包含被移除元素的数组:
|
||||
`splice()`不仅从被调用的数组中移除元素,还会返回一个包含被移除元素的数组:
|
||||
|
||||
```js
|
||||
let array = ['I', 'am', 'feeling', 'really', 'happy'];
|
||||
@ -27,57 +28,38 @@ let newArray = array.splice(3, 2);
|
||||
// newArray 是 ['really', 'happy']
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
给定初始化的数组 `arr`。使用 `splice()` 从 `arr` 里移除元素,使剩余的元素的和为 `10`。
|
||||
|
||||
给定初始化的数组 `arr`。使用 `splice()` 从 `arr` 里移除元素,使剩余的元素的和为 <code>10</code>。
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 不应该修改这一行 <code>const arr = [2, 4, 5, 1, 7, 5, 2, 1];</code>。
|
||||
testString: assert(code.replace(/\s/g, '').match(/constarr=\[2,4,5,1,7,5,2,1\];?/));
|
||||
- text: <code>arr</code> 的剩余元素和应该为 <code>10</code>。
|
||||
testString: assert.strictEqual(arr.reduce((a, b) => a + b), 10);
|
||||
- text: 应该利用 <code>arr</code> 的 <code>splice()</code>。
|
||||
testString: assert(code.replace(/\s/g, '').match(/arr\.splice\(/));
|
||||
- text: splice 应该只删除 <code>arr</code> 里面的元素,不能给 <code>arr</code> 添加元素。
|
||||
testString: assert(!code.replace(/\s/g, '').match(/arr\.splice\(\d+,\d+,\d+.*\)/g));
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
不应该修改这一行 `const arr = [2, 4, 5, 1, 7, 5, 2, 1];`。
|
||||
|
||||
```js
|
||||
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
|
||||
// only change code below this line
|
||||
|
||||
// only change code above this line
|
||||
console.log(arr);
|
||||
assert(code.replace(/\s/g, '').match(/constarr=\[2,4,5,1,7,5,2,1\];?/));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`arr` 的剩余元素和应该为 `10`。
|
||||
|
||||
```js
|
||||
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
|
||||
arr.splice(1, 4);
|
||||
assert.strictEqual(
|
||||
arr.reduce((a, b) => a + b),
|
||||
10
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
应该利用 `arr` 的 `splice()`。
|
||||
|
||||
```js
|
||||
assert(code.replace(/\s/g, '').match(/arr\.splice\(/));
|
||||
```
|
||||
|
||||
splice 应该只删除 `arr` 里面的元素,不能给 `arr` 添加元素。
|
||||
|
||||
```js
|
||||
assert(!code.replace(/\s/g, '').match(/arr\.splice\(\d+,\d+,\d+.*\)/g));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
---
|
||||
id: 587d7b7e367417b2b2512b20
|
||||
title: 使用数组存储数据集合
|
||||
challengeType: 1
|
||||
forumTopicId: 301167
|
||||
title: 使用数组存储数据集合
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
以下是<dfn>数组(Array)</dfn>数据结构的最简单的实现例子。这是一个<dfn>一维数组(one-dimensional array)</dfn>,它只有一层,或者说在它里面没有包含其它的数组结构。可以看到它里面包含了<dfn>布尔值(booleans)</dfn>、<dfn>字符串(strings)</dfn>、<dfn>数字(numbers)</dfn>以及一些其他的 JavaScript 语言中合法的数据类型:
|
||||
|
||||
```js
|
||||
@ -15,8 +15,7 @@ console.log(simpleArray.length);
|
||||
// logs 7
|
||||
```
|
||||
|
||||
可以在上述例子中看到,所有数组都有一个<dfn>长度(length)</dfn>属性。可以简单地使用<code>Array.length</code>方法来访问它。
|
||||
下面是一个关于数组的更复杂的例子。这是一个<dfn>多维数组(multi-dimensional Array</dfn>),或者说是一个包含了其他数组的数组。可以注意到,在它的内部还包含了 JavaScript 中的<dfn>对象(objects)</dfn>结构。我们会在后面的小节中讨论该数据结构,但现在你只需要知道数组能够存储复杂的对象类型数据。
|
||||
可以在上述例子中看到,所有数组都有一个<dfn>长度(length)</dfn>属性。可以简单地使用`Array.length`方法来访问它。 下面是一个关于数组的更复杂的例子。这是一个<dfn>多维数组(multi-dimensional Array</dfn>),或者说是一个包含了其他数组的数组。可以注意到,在它的内部还包含了 JavaScript 中的<dfn>对象(objects)</dfn>结构。我们会在后面的小节中讨论该数据结构,但现在你只需要知道数组能够存储复杂的对象类型数据。
|
||||
|
||||
```js
|
||||
let complexArray = [
|
||||
@ -43,53 +42,41 @@ let complexArray = [
|
||||
];
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
我们已经定义了一个名为<code>yourArray</code>的变量。请修改题目中的语句,将一个含有至少 5 个元素的数组赋值给<code>yourArray</code>变量。你的数组应该包含至少一个 <dfn>string</dfn> 类型的数据、一个 <dfn>number</dfn> 类型的数据和一个 <dfn>boolean</dfn> 类型的数据。
|
||||
</section>
|
||||
我们已经定义了一个名为`yourArray`的变量。请修改题目中的语句,将一个含有至少 5 个元素的数组赋值给`yourArray`变量。你的数组应该包含至少一个 <dfn>string</dfn> 类型的数据、一个 <dfn>number</dfn> 类型的数据和一个 <dfn>boolean</dfn> 类型的数据。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: yourArray 应该是一个数组。
|
||||
testString: assert.strictEqual(Array.isArray(yourArray), true);
|
||||
- text: <code>yourArray</code>至少要包含 5 个元素。
|
||||
testString: assert.isAtLeast(yourArray.length, 5);
|
||||
- text: <code>yourArray</code>应该包含至少一个<code>boolean</code>。
|
||||
testString: assert(yourArray.filter( el => typeof el === 'boolean').length >= 1);
|
||||
- text: <code>yourArray</code>应该包含至少一个<code>number</code>。
|
||||
testString: assert(yourArray.filter( el => typeof el === 'number').length >= 1);
|
||||
- text: <code>yourArray</code>应该包含至少一个<code>string</code>。
|
||||
testString: assert(yourArray.filter( el => typeof el === 'string').length >= 1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
yourArray 应该是一个数组。
|
||||
|
||||
```js
|
||||
let yourArray; // change this line
|
||||
assert.strictEqual(Array.isArray(yourArray), true);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`yourArray`至少要包含 5 个元素。
|
||||
|
||||
```js
|
||||
let yourArray = ['a string', 100, true, ['one', 2], 'another string'];
|
||||
assert.isAtLeast(yourArray.length, 5);
|
||||
```
|
||||
|
||||
</section>
|
||||
`yourArray`应该包含至少一个`boolean`。
|
||||
|
||||
```js
|
||||
assert(yourArray.filter((el) => typeof el === 'boolean').length >= 1);
|
||||
```
|
||||
|
||||
`yourArray`应该包含至少一个`number`。
|
||||
|
||||
```js
|
||||
assert(yourArray.filter((el) => typeof el === 'number').length >= 1);
|
||||
```
|
||||
|
||||
`yourArray`应该包含至少一个`string`。
|
||||
|
||||
```js
|
||||
assert(yourArray.filter((el) => typeof el === 'string').length >= 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,88 +1,48 @@
|
||||
---
|
||||
id: 587d7b7c367417b2b2512b1b
|
||||
title: 使用 delete 关键字删除对象属性
|
||||
challengeType: 1
|
||||
forumTopicId: 301168
|
||||
title: 使用 delete 关键字删除对象属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
现在你已经知道什么是对象以及对象的基本特性和用途。总之,对象是以键值对的形式,灵活、直观地存储结构化数据的一种方式,<strong><em>并且</em></strong>查找对象属性的速度是很快的。在本章剩下的挑战中,我们会讲对象的几种常用操作,这样你能更好地在你的程序中使用这种有用的数据结构。
|
||||
在之前的挑战中,我们已经试过新增和修改对象中的键值对。现在我们来看如何从一个对象中<em>移除</em>一个键值对。
|
||||
我们再来看上一个挑战中的<code>foods</code>对象。如果我们想移除<code>apples</code>属性,我们可以使用<code>delete</code>关键字:
|
||||
# --description--
|
||||
|
||||
现在你已经知道什么是对象以及对象的基本特性和用途。总之,对象是以键值对的形式,灵活、直观地存储结构化数据的一种方式,***并且***查找对象属性的速度是很快的。在本章剩下的挑战中,我们会讲对象的几种常用操作,这样你能更好地在你的程序中使用这种有用的数据结构。
|
||||
|
||||
在之前的挑战中,我们已经试过新增和修改对象中的键值对。现在我们来看如何从一个对象中*移除*一个键值对。
|
||||
|
||||
我们再来看上一个挑战中的`foods`对象。如果我们想移除`apples`属性,我们可以使用`delete`关键字:
|
||||
|
||||
```js
|
||||
delete foods.apples;
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
请你用 delete 关键字来移除<code>foods</code>中的<code>oranges</code>、<code>plums</code>和<code>strawberries</code>属性。
|
||||
</section>
|
||||
请你用 delete 关键字来移除`foods`中的`oranges`、`plums`和`strawberries`属性。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>foods</code>对象应该只含有 3 个键:<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);'
|
||||
- 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);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`foods`对象应该只含有 3 个键:`apples`、`grapes`和`bananas`。
|
||||
|
||||
```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);
|
||||
assert(
|
||||
!foods.hasOwnProperty('oranges') &&
|
||||
!foods.hasOwnProperty('plums') &&
|
||||
!foods.hasOwnProperty('strawberries') &&
|
||||
Object.keys(foods).length === 3
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你应该用`delete`关键字来移除`oranges`、`plums`和`strawberries`属性。
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let foods = {
|
||||
apples: 25,
|
||||
oranges: 32,
|
||||
plums: 28,
|
||||
bananas: 13,
|
||||
grapes: 35,
|
||||
strawberries: 27
|
||||
};
|
||||
|
||||
delete foods.oranges;
|
||||
delete foods.plums;
|
||||
delete foods.strawberries;
|
||||
|
||||
console.log(foods);
|
||||
assert(
|
||||
code.search(/oranges:/) !== -1 &&
|
||||
code.search(/plums:/) !== -1 &&
|
||||
code.search(/strawberries:/) !== -1
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,18 +1,17 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392ca
|
||||
title: 通过索引访问数组中的数据
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cBZQbTz'
|
||||
forumTopicId: 16158
|
||||
title: 通过索引访问数组中的数据
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
我们可以使用索引 <code>indexes</code> 来访问数组中的数据。
|
||||
# --description--
|
||||
|
||||
数组索引与字符串索引一样使用中括号,但字符串索引得到的是一个字符,而数组索引得到的是一个元素。数组索引与字符串索引一样是从 0 开始的,所以数组中第一个元素的索引编号是 0。
|
||||
<br/>
|
||||
<strong>示例</strong>
|
||||
我们可以使用索引 `indexes` 来访问数组中的数据。
|
||||
|
||||
数组索引与字符串索引一样使用中括号,但字符串索引得到的是一个字符,而数组索引得到的是一个元素。数组索引与字符串索引一样是从 0 开始的,所以数组中第一个元素的索引编号是 0。
|
||||
**示例**
|
||||
|
||||
```js
|
||||
var array = [50,60,70];
|
||||
@ -20,66 +19,46 @@ array[0]; // equals 50
|
||||
var data = array[1]; // equals 60
|
||||
```
|
||||
|
||||
<strong>提示</strong><br>数组名称和方括号之间不应有任何空格,如<code>array [0]</code>尽管 JavaScript 能够正确处理,但可能会让看你代码的其他程序员感到困惑
|
||||
</section>
|
||||
**提示**
|
||||
数组名称和方括号之间不应有任何空格,如`array [0]`尽管 JavaScript 能够正确处理,但可能会让看你代码的其他程序员感到困惑
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
创建一个名为<code>myData</code>的变量,并把<code>myArray</code>的第一个索引上的值赋给它。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
创建一个名为`myData`的变量,并把`myArray`的第一个索引上的值赋给它。
|
||||
|
||||
```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;}})());
|
||||
- text: 应使用方括号访问变量<code>myArray</code>中的数据。
|
||||
testString: assert((function(){if(code.match(/\s*=\s*myArray\[0\]/g)){return true;}else{return false;}})());
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
变量`myData`的值应该等于`myArray`的第一个值。
|
||||
|
||||
```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.
|
||||
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
typeof myArray !== 'undefined' &&
|
||||
typeof myData !== 'undefined' &&
|
||||
myArray[0] === myData
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
应使用方括号访问变量`myArray`中的数据。
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined" && typeof myData !== "undefined"){(function(y,z){return 'myArray = ' + JSON.stringify(y) + ', myData = ' + JSON.stringify(z);})(myArray, myData);}
|
||||
assert(
|
||||
(function () {
|
||||
if (code.match(/\s*=\s*myArray\[0\]/g)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var myArray = [50,60,70];
|
||||
var myData = myArray[0];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,15 +1,16 @@
|
||||
---
|
||||
id: 56592a60ddddeae28f7aa8e1
|
||||
title: 使用索引访问多维数组
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ckND4Cq'
|
||||
forumTopicId: 16159
|
||||
title: 使用索引访问多维数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
可以把 <dfn>多维</dfn> 数组看作成是一个 <em>数组中的数组</em>。当使用方括号去访问数组的时候,第一个<code>[index]</code>访问的是第 N 个子数组,第二个<code>[index]</code>访问的是第 N 个子数组的第N个元素。
|
||||
<strong>示例</strong>
|
||||
# --description--
|
||||
|
||||
可以把 <dfn>多维</dfn> 数组看作成是一个 *数组中的数组*。当使用方括号去访问数组的时候,第一个`[index]`访问的是第 N 个子数组,第二个`[index]`访问的是第 N 个子数组的第N个元素。
|
||||
|
||||
**示例**
|
||||
|
||||
```js
|
||||
var arr = [
|
||||
@ -23,63 +24,29 @@ arr[3][0]; // equals [10,11,12]
|
||||
arr[3][0][1]; // equals 11
|
||||
```
|
||||
|
||||
<strong>提示</strong><br>数组名称和方括号之间不应该有任何空格,如<code>array [0][0]</code>,甚至<code>array [0] [0]</code>,都是不正确的。尽管 JavaScript 能够处理,但可能会让看你代码的其他程序员感到困惑。
|
||||
</section>
|
||||
**提示**
|
||||
数组名称和方括号之间不应该有任何空格,如`array [0][0]`,甚至`array [0][0]`,都是不正确的。尽管 JavaScript 能够处理,但可能会让看你代码的其他程序员感到困惑。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用索引从<code>myArray</code>选择一个元素,使得<code>myData</code>的值为<code>8</code>。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
使用索引从`myArray`选择一个元素,使得`myData`的值为`8`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myData</code>应该等于<code>8</code>。
|
||||
testString: assert(myData === 8);
|
||||
- text: 你应该使用方括号从<code>myArray</code>中取值。
|
||||
testString: 'assert(/myArray\[2\]\[1\]/g.test(code) && !/myData\s*=\s*(?:.*[-+*/%]|\d)/g.test(code));'
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myData`应该等于`8`。
|
||||
|
||||
```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];
|
||||
|
||||
assert(myData === 8);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
你应该使用方括号从`myArray`中取值。
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return "myData: " + myData + " myArray: " + JSON.stringify(myArray);})();}
|
||||
assert(
|
||||
/myArray\[2\]\[1\]/g.test(code) &&
|
||||
!/myData\s*=\s*(?:.*[-+*/%]|\d)/g.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var myArray = [[1,2,3],[4,5,6], [7,8,9], [[10,11,12], 13, 14]];
|
||||
var myData = myArray[2][1];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244cd
|
||||
title: 访问嵌套数组
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cLeGDtZ'
|
||||
forumTopicId: 16160
|
||||
title: 访问嵌套数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
正如我们在前面的例子所见,对象可以嵌套对象和数组。与访问嵌套对象一样,用中括号操作符同样可以访问嵌套数组。
|
||||
|
||||
下面是如何访问嵌套数组的例子:
|
||||
|
||||
```js
|
||||
@ -34,105 +35,23 @@ ourPets[0].names[1]; // "Fluffy"
|
||||
ourPets[1].names[0]; // "Spot"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用点操作符和中括号操作符来检索变量<code>myPlants</code>的第二棵树。
|
||||
</section>
|
||||
使用点操作符和中括号操作符来检索变量`myPlants`的第二棵树。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>secondTree</code>应该等于 "pine"。
|
||||
testString: assert(secondTree === "pine");
|
||||
- text: 使用点操作符和中括号操作符来检索变量<code>myPlants</code>。
|
||||
testString: assert(/=\s*myPlants\[1\].list\[1\]/.test(code));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`secondTree`应该等于 "pine"。
|
||||
|
||||
```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
|
||||
|
||||
assert(secondTree === 'pine');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
使用点操作符和中括号操作符来检索变量`myPlants`。
|
||||
|
||||
```js
|
||||
(function(x) {
|
||||
if(typeof x != 'undefined') {
|
||||
return "secondTree = " + x;
|
||||
}
|
||||
return "secondTree is undefined";
|
||||
})(secondTree);
|
||||
assert(/=\s*myPlants\[1\].list\[1\]/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var myPlants = [
|
||||
{
|
||||
type: "flowers",
|
||||
list: [
|
||||
"rose",
|
||||
"tulip",
|
||||
"dandelion"
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "trees",
|
||||
list: [
|
||||
"fir",
|
||||
"pine",
|
||||
"birch"
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var secondTree = myPlants[1].list[1];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244cc
|
||||
title: 访问嵌套对象
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cRnRnfa'
|
||||
forumTopicId: 16161
|
||||
title: 访问嵌套对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
通过串联起来的点操作符或中括号操作符来访问对象的嵌套属性。
|
||||
|
||||
下面是一个嵌套的对象:
|
||||
|
||||
```js
|
||||
@ -28,86 +29,23 @@ ourStorage.cabinet["top drawer"].folder2; // "secrets"
|
||||
ourStorage.desk.drawer; // "stapler"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
读取<code>myStorage</code>对象,将<code>glove box</code>属性的内容赋值给变量<code>gloveBoxContents</code>。在适用的地方使用点操作符来访问属性,否则使用中括号操作符。
|
||||
</section>
|
||||
读取`myStorage`对象,将`glove box`属性的内容赋值给变量`gloveBoxContents`。在适用的地方使用点操作符来访问属性,否则使用中括号操作符。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>gloveBoxContents</code>应该等于"maps"。
|
||||
testString: assert(gloveBoxContents === "maps");
|
||||
- text: 应使用点操作符和中括号操作符来访问<code>myStorage</code>。
|
||||
testString: assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`gloveBoxContents`应该等于"maps"。
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myStorage = {
|
||||
"car": {
|
||||
"inside": {
|
||||
"glove box": "maps",
|
||||
"passenger seat": "crumbs"
|
||||
},
|
||||
"outside": {
|
||||
"trunk": "jack"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var gloveBoxContents = undefined; // Change this line
|
||||
|
||||
assert(gloveBoxContents === 'maps');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
应使用点操作符和中括号操作符来访问`myStorage`。
|
||||
|
||||
```js
|
||||
(function(x) {
|
||||
if(typeof x != 'undefined') {
|
||||
return "gloveBoxContents = " + x;
|
||||
}
|
||||
return "gloveBoxContents is undefined";
|
||||
})(gloveBoxContents);
|
||||
assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var myStorage = {
|
||||
"car":{
|
||||
"inside":{
|
||||
"glove box":"maps",
|
||||
"passenger seat":"crumbs"
|
||||
},
|
||||
"outside":{
|
||||
"trunk":"jack"
|
||||
}
|
||||
}
|
||||
};
|
||||
var gloveBoxContents = myStorage.car.inside["glove box"];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,16 +1,18 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c8
|
||||
title: 通过方括号访问对象属性
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cBvmEHP'
|
||||
forumTopicId: 16163
|
||||
title: 通过方括号访问对象属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
第二种访问对象的方式就是中括号操作符(<code>[]</code>),如果你想访问的属性的名称有一个空格,这时你只能使用中括号操作符(<code>[]</code>)。
|
||||
# --description--
|
||||
|
||||
第二种访问对象的方式就是中括号操作符(`[]`),如果你想访问的属性的名称有一个空格,这时你只能使用中括号操作符(`[]`)。
|
||||
|
||||
当然,如果属性名不包含空格,也可以使用中括号操作符。
|
||||
这是一个使用中括号操作符(<code>[]</code>)读取对象属性的例子:
|
||||
|
||||
这是一个使用中括号操作符(`[]`)读取对象属性的例子:
|
||||
|
||||
```js
|
||||
var myObj = {
|
||||
@ -24,78 +26,42 @@ myObj["NoSpace"]; // USS Enterprise
|
||||
```
|
||||
|
||||
提示:属性名称中如果有空格,必须把属性名称用单引号或双引号包裹起来。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
用中括号操作符读取对象<code>testObj</code>的<code>an entree</code>属性值和<code>the drink</code>属性值,并分别赋值给<code>entreeValue</code>和<code>drinkValue</code>。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
用中括号操作符读取对象`testObj`的`an entree`属性值和`the drink`属性值,并分别赋值给`entreeValue`和`drinkValue`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>entreeValue</code>应该是一个字符串。
|
||||
testString: assert(typeof entreeValue === 'string' );
|
||||
- text: <code>entreeValue</code>的值应该是<code>"hamburger"</code>。
|
||||
testString: assert(entreeValue === 'hamburger' );
|
||||
- text: <code>drinkValue</code>应该是一个字符串。
|
||||
testString: assert(typeof drinkValue === 'string' );
|
||||
- text: <code>drinkValue</code>的值应该是<code>"water"</code>。
|
||||
testString: assert(drinkValue === 'water' );
|
||||
- text: 你应该使用中括号两次。
|
||||
testString: assert(code.match(/testObj\s*?\[('|")[^'"]+\1\]/g).length > 1);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`entreeValue`应该是一个字符串。
|
||||
|
||||
```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
|
||||
assert(typeof entreeValue === 'string');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`entreeValue`的值应该是`"hamburger"`。
|
||||
|
||||
```js
|
||||
(function(a,b) { return "entreeValue = '" + a + "', drinkValue = '" + b + "'"; })(entreeValue,drinkValue);
|
||||
assert(entreeValue === 'hamburger');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`drinkValue`应该是一个字符串。
|
||||
|
||||
```js
|
||||
var testObj = {
|
||||
"an entree": "hamburger",
|
||||
"my side": "veggies",
|
||||
"the drink": "water"
|
||||
};
|
||||
var entreeValue = testObj["an entree"];
|
||||
var drinkValue = testObj['the drink'];
|
||||
assert(typeof drinkValue === 'string');
|
||||
```
|
||||
|
||||
</section>
|
||||
`drinkValue`的值应该是`"water"`。
|
||||
|
||||
```js
|
||||
assert(drinkValue === 'water');
|
||||
```
|
||||
|
||||
你应该使用中括号两次。
|
||||
|
||||
```js
|
||||
assert(code.match(/testObj\s*?\[('|")[^'"]+\1\]/g).length > 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,17 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c7
|
||||
title: 通过点符号访问对象属性
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cGryJs8'
|
||||
forumTopicId: 16164
|
||||
title: 通过点符号访问对象属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
有两种方式访问对象属性,一个是点操作符(<code>.</code>),一个是中括号操作符(<code>[]</code>)。
|
||||
# --description--
|
||||
|
||||
有两种方式访问对象属性,一个是点操作符(`.`),一个是中括号操作符(`[]`)。
|
||||
|
||||
当你知道所要读取的属性的名称的时候,使用点操作符。
|
||||
|
||||
这是一个使用点操作符读取对象属性的例子:
|
||||
|
||||
```js
|
||||
@ -21,79 +23,41 @@ var prop1val = myObj.prop1; // val1
|
||||
var prop2val = myObj.prop2; // val2
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
通过点操作符读取对象<code>testObj</code>,把<code>hat</code>的属性值赋给变量<code>hatValue</code>,把<code>shirt</code>的属性值赋给<code>shirtValue</code>。
|
||||
</section>
|
||||
通过点操作符读取对象`testObj`,把`hat`的属性值赋给变量`hatValue`,把`shirt`的属性值赋给`shirtValue`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>hatValue</code>应该是一个字符串。
|
||||
testString: assert(typeof hatValue === 'string' );
|
||||
- text: <code>hatValue</code>的值应该是<code>"ballcap"</code>。
|
||||
testString: assert(hatValue === 'ballcap' );
|
||||
- text: <code>shirtValue</code>应该是一个字符串。
|
||||
testString: assert(typeof shirtValue === 'string' );
|
||||
- text: <code>shirtValue</code>的值应该是<code>"jersey"</code>。
|
||||
testString: assert(shirtValue === 'jersey' );
|
||||
- text: 你应该使用点操作符两次。
|
||||
testString: assert(code.match(/testObj\.\w+/g).length > 1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`hatValue`应该是一个字符串。
|
||||
|
||||
```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
|
||||
assert(typeof hatValue === 'string');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`hatValue`的值应该是`"ballcap"`。
|
||||
|
||||
```js
|
||||
(function(a,b) { return "hatValue = '" + a + "', shirtValue = '" + b + "'"; })(hatValue,shirtValue);
|
||||
assert(hatValue === 'ballcap');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`shirtValue`应该是一个字符串。
|
||||
|
||||
```js
|
||||
var testObj = {
|
||||
"hat": "ballcap",
|
||||
"shirt": "jersey",
|
||||
"shoes": "cleats"
|
||||
};
|
||||
|
||||
var hatValue = testObj.hat;
|
||||
var shirtValue = testObj.shirt;
|
||||
assert(typeof shirtValue === 'string');
|
||||
```
|
||||
|
||||
</section>
|
||||
`shirtValue`的值应该是`"jersey"`。
|
||||
|
||||
```js
|
||||
assert(shirtValue === 'jersey');
|
||||
```
|
||||
|
||||
你应该使用点操作符两次。
|
||||
|
||||
```js
|
||||
assert(code.match(/testObj\.\w+/g).length > 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c9
|
||||
title: 通过变量访问对象属性
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cnQyKur'
|
||||
forumTopicId: 16165
|
||||
title: 通过变量访问对象属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
中括号操作符的另一个使用方式是访问赋值给变量的属性。当你需要遍历对象的属性列表或访问查找表(lookup tables)时,这种方式极为有用。
|
||||
|
||||
这有一个使用变量来访问属性的例子:
|
||||
|
||||
```js
|
||||
@ -35,80 +36,48 @@ console.log(someObj[someProp]); // "John"
|
||||
```
|
||||
|
||||
提示:当我们通过变量名访问属性的时候,不需要给变量名包裹引号。因为实际上我们使用的是变量的值,而不是变量的名称。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用变量<code>playerNumber</code>,通过中括号操作符找到<code>testObj</code>中<code>playerNumber</code>为<code>16</code>的值。然后把名字赋给变量<code>player</code>。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
使用变量`playerNumber`,通过中括号操作符找到`testObj`中`playerNumber`为`16`的值。然后把名字赋给变量`player`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>playerNumber</code>应该是一个数字。
|
||||
testString: assert(typeof playerNumber === 'number');
|
||||
- text: 变量<code>player</code>应该是一个字符串。
|
||||
testString: assert(typeof player === 'string');
|
||||
- text: <code>player</code>点值应该是 "Montana"。
|
||||
testString: assert(player === 'Montana');
|
||||
- text: 你应该使用中括号访问<code>testObj</code>。
|
||||
testString: assert(/testObj\s*?\[.*?\]/.test(code));
|
||||
- text: 你不应该直接将<code>Montana</code>赋给<code>player</code>。
|
||||
testString: assert(!code.match(/player\s*=\s*"|\'\s*Montana\s*"|\'\s*;/gi));
|
||||
- text: 你应该在中括号中使用<code>playerNumber</code>变量。
|
||||
testString: assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`playerNumber`应该是一个数字。
|
||||
|
||||
```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
|
||||
assert(typeof playerNumber === 'number');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
变量`player`应该是一个字符串。
|
||||
|
||||
```js
|
||||
if(typeof player !== "undefined"){(function(v){return v;})(player);}
|
||||
assert(typeof player === 'string');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`player`点值应该是 "Montana"。
|
||||
|
||||
```js
|
||||
var testObj = {
|
||||
12: "Namath",
|
||||
16: "Montana",
|
||||
19: "Unitas"
|
||||
};
|
||||
var playerNumber = 16;
|
||||
var player = testObj[playerNumber];
|
||||
assert(player === 'Montana');
|
||||
```
|
||||
|
||||
</section>
|
||||
你应该使用中括号访问`testObj`。
|
||||
|
||||
```js
|
||||
assert(/testObj\s*?\[.*?\]/.test(code));
|
||||
```
|
||||
|
||||
你不应该直接将`Montana`赋给`player`。
|
||||
|
||||
```js
|
||||
assert(!code.match(/player\s*=\s*"|\'\s*Montana\s*"|\'\s*;/gi));
|
||||
```
|
||||
|
||||
你应该在中括号中使用`playerNumber`变量。
|
||||
|
||||
```js
|
||||
assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,94 +1,42 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d2
|
||||
title: 给对象添加新属性
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cQe38UD'
|
||||
forumTopicId: 301169
|
||||
title: 给对象添加新属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
你也可以像更改属性一样给对象添加属性。
|
||||
看看我们是如何给<code>ourDog</code>添加<code>"bark"</code>属性:
|
||||
<code>ourDog.bark = "bow-wow";</code>
|
||||
|
||||
看看我们是如何给`ourDog`添加`"bark"`属性:
|
||||
|
||||
`ourDog.bark = "bow-wow";`
|
||||
|
||||
或者
|
||||
<code>ourDog["bark"] = "bow-wow";</code>
|
||||
现在当我们访问<code>ourDog.bark</code>时会得到 ourDog 的 bark 值 "bow-wow".
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
给<code>myDog</code>添加一个<code>"bark"</code>属性,设置它的值为狗的声音,例如:"woof"。你可以使用点或中括号操作符。
|
||||
</section>
|
||||
`ourDog["bark"] = "bow-wow";`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
现在当我们访问`ourDog.bark`时会得到 ourDog 的 bark 值 "bow-wow".
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 给<code>myDog</code>添加<code>"bark"</code>属性。
|
||||
testString: assert(myDog.bark !== undefined);
|
||||
- text: 不能在初始化 myDog 的时候添加<code>"bark"</code>属性。
|
||||
testString: 'assert(!/bark[^\n]:/.test(code));'
|
||||
# --instructions--
|
||||
|
||||
```
|
||||
给`myDog`添加一个`"bark"`属性,设置它的值为狗的声音,例如:"woof"。你可以使用点或中括号操作符。
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
给`myDog`添加`"bark"`属性。
|
||||
|
||||
```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.
|
||||
|
||||
assert(myDog.bark !== undefined);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
不能在初始化 myDog 的时候添加`"bark"`属性。
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
assert(!/bark[^\n]:/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var myDog = {
|
||||
"name": "Happy Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"]
|
||||
};
|
||||
myDog.bark = "Woof Woof";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,73 +1,42 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb3bdef
|
||||
title: 加法运算
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cM2KBAG'
|
||||
forumTopicId: 16650
|
||||
title: 加法运算
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<code>Number</code>是 JavaScript 中的一种数据类型,表示数值。
|
||||
# --description--
|
||||
|
||||
`Number`是 JavaScript 中的一种数据类型,表示数值。
|
||||
|
||||
现在让我们来尝试在 JavaScript 中做加法运算。
|
||||
JavaScript 中使用<code>+</code>号进行加法运算。
|
||||
<strong>示例</strong>
|
||||
|
||||
JavaScript 中使用`+`号进行加法运算。
|
||||
|
||||
**示例**
|
||||
|
||||
```js
|
||||
myVar = 5 + 10; // assigned 15
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
改变数字<code>0</code>让变量 sum 的值为<code>20</code>。
|
||||
</section>
|
||||
改变数字`0`让变量 sum 的值为`20`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>sum</code>应该等于<code>20</code>。
|
||||
testString: assert(sum === 20);
|
||||
- text: 要使用<code>+</code>运算符。
|
||||
testString: assert(/\+/.test(code));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`sum`应该等于`20`。
|
||||
|
||||
```js
|
||||
var sum = 10 + 0;
|
||||
|
||||
assert(sum === 20);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
要使用`+`运算符。
|
||||
|
||||
```js
|
||||
(function(z){return 'sum = '+z;})(sum);
|
||||
assert(/\+/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var sum = 10 + 10;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,15 +1,16 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244de
|
||||
title: 在 Switch 语句中添加默认选项
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c3JvVfg'
|
||||
forumTopicId: 16653
|
||||
title: 在 Switch 语句中添加默认选项
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
在<code>switch</code>语句中你可能无法用 case 来指定所有情况,这时你可以添加 default 语句。当再也找不到 case 匹配的时候 default 语句会执行,非常类似于 if/else 组合中的 else 语句。
|
||||
<code>default</code>语句应该是最后一个 case。
|
||||
# --description--
|
||||
|
||||
在`switch`语句中你可能无法用 case 来指定所有情况,这时你可以添加 default 语句。当再也找不到 case 匹配的时候 default 语句会执行,非常类似于 if/else 组合中的 else 语句。
|
||||
|
||||
`default`语句应该是最后一个 case。
|
||||
|
||||
```js
|
||||
switch (num) {
|
||||
@ -26,89 +27,63 @@ switch (num) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
写一个 switch 语句,根据下面的条件来设置<code>answer</code>的switch语句:<br><code>"a"</code> - "apple"<br><code>"b"</code> - "bird"<br><code>"c"</code> - "cat"<br><code>default</code> - "stuff"
|
||||
</section>
|
||||
写一个 switch 语句,根据下面的条件来设置`answer`的switch语句:
|
||||
`"a"` - "apple"
|
||||
`"b"` - "bird"
|
||||
`"c"` - "cat"
|
||||
`default` - "stuff"
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>switchOfStuff("a")</code>应该有一个值为 "apple"。
|
||||
testString: assert(switchOfStuff("a") === "apple");
|
||||
- text: <code>switchOfStuff("b")</code>应该有一个值为 "bird"。
|
||||
testString: assert(switchOfStuff("b") === "bird");
|
||||
- text: <code>switchOfStuff("c")</code>应该有一个值为 "cat"。
|
||||
testString: assert(switchOfStuff("c") === "cat");
|
||||
- text: <code>switchOfStuff("d")</code>应该有一个值为 "stuff"。
|
||||
testString: assert(switchOfStuff("d") === "stuff");
|
||||
- text: <code>switchOfStuff(4)</code>应该有一个值为 "stuff"。
|
||||
testString: assert(switchOfStuff(4) === "stuff");
|
||||
- text: 不能使用任何<code>if</code>或<code>else</code>表达式。
|
||||
testString: assert(!/else/g.test(code) || !/if/g.test(code));
|
||||
- text: 你应该有一个<code>default</code>表达式。
|
||||
testString: assert(switchOfStuff("string-to-trigger-default-case") === "stuff");
|
||||
- text: 你应该有至少 3 个<code>break</code>表达式。
|
||||
testString: assert(code.match(/break/g).length > 2);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`switchOfStuff("a")`应该有一个值为 "apple"。
|
||||
|
||||
```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);
|
||||
|
||||
assert(switchOfStuff('a') === 'apple');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`switchOfStuff("b")`应该有一个值为 "bird"。
|
||||
|
||||
```js
|
||||
function switchOfStuff(val) {
|
||||
var answer = "";
|
||||
|
||||
switch(val) {
|
||||
case "a":
|
||||
answer = "apple";
|
||||
break;
|
||||
case "b":
|
||||
answer = "bird";
|
||||
break;
|
||||
case "c":
|
||||
answer = "cat";
|
||||
break;
|
||||
default:
|
||||
answer = "stuff";
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
assert(switchOfStuff('b') === 'bird');
|
||||
```
|
||||
|
||||
</section>
|
||||
`switchOfStuff("c")`应该有一个值为 "cat"。
|
||||
|
||||
```js
|
||||
assert(switchOfStuff('c') === 'cat');
|
||||
```
|
||||
|
||||
`switchOfStuff("d")`应该有一个值为 "stuff"。
|
||||
|
||||
```js
|
||||
assert(switchOfStuff('d') === 'stuff');
|
||||
```
|
||||
|
||||
`switchOfStuff(4)`应该有一个值为 "stuff"。
|
||||
|
||||
```js
|
||||
assert(switchOfStuff(4) === 'stuff');
|
||||
```
|
||||
|
||||
不能使用任何`if`或`else`表达式。
|
||||
|
||||
```js
|
||||
assert(!/else/g.test(code) || !/if/g.test(code));
|
||||
```
|
||||
|
||||
你应该有一个`default`表达式。
|
||||
|
||||
```js
|
||||
assert(switchOfStuff('string-to-trigger-default-case') === 'stuff');
|
||||
```
|
||||
|
||||
你应该有至少 3 个`break`表达式。
|
||||
|
||||
```js
|
||||
assert(code.match(/break/g).length > 2);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,92 +1,32 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ed
|
||||
title: 将变量附加到字符串
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cbQmZfa'
|
||||
forumTopicId: 16656
|
||||
title: 将变量附加到字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
我们不仅可以创建出多行的字符串,还可以使用加等号(<code>+=</code>)运算符来将变量追加到字符串。
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
设置变量<code>someAdjective</code>的值,并使用<code>+=</code>运算符把它追加到变量<code>myStr</code>上。
|
||||
</section>
|
||||
我们不仅可以创建出多行的字符串,还可以使用加等号(`+=`)运算符来将变量追加到字符串。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>someAdjective</code>应该是一个至少包含三个字符的字符串。
|
||||
testString: assert(typeof someAdjective !== 'undefined' && someAdjective.length > 2);
|
||||
- text: 使用<code>+=</code>操作符把<code>someAdjective</code>追加到<code>myStr</code>的后面。
|
||||
testString: assert(code.match(/myStr\s*\+=\s*someAdjective\s*/).length > 0);
|
||||
设置变量`someAdjective`的值,并使用`+=`运算符把它追加到变量`myStr`上。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`someAdjective`应该是一个至少包含三个字符的字符串。
|
||||
|
||||
```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 ";
|
||||
|
||||
assert(typeof someAdjective !== 'undefined' && someAdjective.length > 2);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
使用`+=`操作符把`someAdjective`追加到`myStr`的后面。
|
||||
|
||||
```js
|
||||
(function(){
|
||||
var output = [];
|
||||
if(typeof someAdjective === 'string') {
|
||||
output.push('someAdjective = "' + someAdjective + '"');
|
||||
} else {
|
||||
output.push('someAdjective is not a string');
|
||||
}
|
||||
if(typeof myStr === 'string') {
|
||||
output.push('myStr = "' + myStr + '"');
|
||||
} else {
|
||||
output.push('myStr is not a string');
|
||||
}
|
||||
return output.join('\n');
|
||||
})();
|
||||
assert(code.match(/myStr\s*\+=\s*someAdjective\s*/).length > 0);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var anAdjective = "awesome!";
|
||||
var ourStr = "freeCodeCamp is ";
|
||||
ourStr += anAdjective;
|
||||
|
||||
var someAdjective = "neat";
|
||||
var myStr = "Learning to code is ";
|
||||
myStr += someAdjective;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -0,0 +1,85 @@
|
||||
---
|
||||
id: 5ee127a03c3b35dd45426493
|
||||
title: Assigning the Value of One Variable to Another
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 418265
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
After a value is assigned to a variable using the <dfn>assignment</dfn> operator, you can assign the value of that variable to another variable using the <dfn>assignment</dfn> operator.
|
||||
|
||||
```js
|
||||
var myVar;
|
||||
myVar = 5;
|
||||
var myNum;
|
||||
myNum = myVar;
|
||||
```
|
||||
|
||||
The above declares a `myVar` variable with no value, then assigns it the value `5`. Next, a variable named `myNum` is declared with no value. Then, the contents of `myVar` (which is `5`) is assigned to the variable `myNum`. Now, `myNum` also has the value of `5`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Assign the contents of `a` to variable `b`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should not change code above the specified comment.
|
||||
|
||||
```js
|
||||
assert(/var a;/.test(code) && /a = 7;/.test(code) && /var b;/.test(code));
|
||||
```
|
||||
|
||||
`b` should have a value of 7.
|
||||
|
||||
```js
|
||||
assert(typeof b === 'number' && b === 7);
|
||||
```
|
||||
|
||||
`a` should be assigned to `b` with `=`.
|
||||
|
||||
```js
|
||||
assert(/b\s*=\s*a\s*/g.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
if (typeof a != 'undefined') {
|
||||
a = undefined;
|
||||
}
|
||||
if (typeof b != 'undefined') {
|
||||
b = undefined;
|
||||
}
|
||||
```
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a, b) {
|
||||
return 'a = ' + a + ', b = ' + b;
|
||||
})(a, b);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var a;
|
||||
a = 7;
|
||||
var b;
|
||||
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a;
|
||||
a = 7;
|
||||
var b;
|
||||
b = a;
|
||||
```
|
@ -1,91 +1,38 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c3
|
||||
title: 用返回值来赋值
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ce2pEtB'
|
||||
forumTopicId: 16658
|
||||
title: 用返回值来赋值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
如果你还记得我们在这一节<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>return</code>了一个数值<code>17</code>,然后把它赋值给了<code>ourSum</code>变量。
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
调用<code>processArg</code>函数并给参数一个值<code>7</code>,然后把返回的值赋值给变量<code>processed</code>。
|
||||
</section>
|
||||
如果你还记得我们在这一节[使用赋值运算符存储值](/javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator)的讨论,赋值之前,先完成等号右边的操作。这意味着我们可把一个函数的返回值,赋值给一个变量。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
假设我们预先定义的函数`sum`其功能就是将两个数字相加,那么:
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>processed</code>的值应该是<code>2</code>。
|
||||
testString: assert(processed === 2);
|
||||
- text: 你应该把<code>processArg</code>的返回值赋给<code>processed</code>。
|
||||
testString: assert(/processed\s*=\s*processArg\(\s*7\s*\)\s*;/.test(code));
|
||||
`ourSum = sum(5, 12);`
|
||||
|
||||
```
|
||||
将调用`sum`函数,返回`return`了一个数值`17`,然后把它赋值给了`ourSum`变量。
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
调用`processArg`函数并给参数一个值`7`,然后把返回的值赋值给变量`processed`。
|
||||
|
||||
<div id='js-seed'>
|
||||
# --hints--
|
||||
|
||||
`processed`的值应该是`2`。
|
||||
|
||||
```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
|
||||
|
||||
|
||||
assert(processed === 2);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
你应该把`processArg`的返回值赋给`processed`。
|
||||
|
||||
```js
|
||||
(function(){return "processed = " + processed})();
|
||||
assert(/processed\s*=\s*processArg\(\s*7\s*\)\s*;/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var processed = 0;
|
||||
|
||||
function processArg(num) {
|
||||
return (num + 3) / 5;
|
||||
}
|
||||
|
||||
processed = processArg(7);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,16 +1,19 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d0
|
||||
title: 新建 JavaScript 对象
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cWGkbtd'
|
||||
forumTopicId: 16769
|
||||
title: 新建 JavaScript 对象
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
你之前可能听说过对象<code>object</code>。
|
||||
# --description--
|
||||
|
||||
你之前可能听说过对象`object`。
|
||||
|
||||
对象和数组很相似,数组是通过索引来访问和修改数据,而对象是通过属性来访问和修改数据。
|
||||
|
||||
对象适合用来存储结构化数据,就和真实世界的对象一模一样,比如一只猫。
|
||||
|
||||
这是一个对象的示例:
|
||||
|
||||
```js
|
||||
@ -22,7 +25,7 @@ var cat = {
|
||||
};
|
||||
```
|
||||
|
||||
在这个示例中所有的属性以字符串的形式储存,例如,<code>"name"</code>、<code>"legs"</code>和<code>"tails"</code>。但是,你也可以使用数字作为属性,你甚至可以省略字符串属性的引号,如下所示:
|
||||
在这个示例中所有的属性以字符串的形式储存,例如,`"name"`、`"legs"`和`"tails"`。但是,你也可以使用数字作为属性,你甚至可以省略字符串属性的引号,如下所示:
|
||||
|
||||
```js
|
||||
var anotherObject = {
|
||||
@ -33,83 +36,96 @@ var anotherObject = {
|
||||
```
|
||||
|
||||
但是,如果你的对象具有任何非字符串属性,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>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
创建一个叫做`myDog`的对象,它里面有这些属性:`"name"`、`"legs"`、`"tails"`、`"friends"`。
|
||||
|
||||
```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));
|
||||
- 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));
|
||||
- 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));
|
||||
- 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));
|
||||
- text: <code>myDog</code>应该只包含给出的属性。
|
||||
testString: assert((function(z){return Object.keys(z).length === 4;})(myDog));
|
||||
你可以设置对象属性为任何值,只需要确保`"name"`是字符串,`"legs"`和`"tails"`是数字,`"friends"`是数组。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myDog`应该包含`name`属性,并且是一个字符串`string`。
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"]
|
||||
};
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
var myDog = {
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
assert(
|
||||
(function (z) {
|
||||
if (
|
||||
z.hasOwnProperty('name') &&
|
||||
z.name !== undefined &&
|
||||
typeof z.name === 'string'
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`myDog`应该包含`legs`属性,并且是一个数字`number`。
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
assert(
|
||||
(function (z) {
|
||||
if (
|
||||
z.hasOwnProperty('legs') &&
|
||||
z.legs !== undefined &&
|
||||
typeof z.legs === 'number'
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`myDog`应该包含`tails`属性,并且是一个数字`number`。
|
||||
|
||||
```js
|
||||
var myDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"]
|
||||
};
|
||||
assert(
|
||||
(function (z) {
|
||||
if (
|
||||
z.hasOwnProperty('tails') &&
|
||||
z.tails !== undefined &&
|
||||
typeof z.tails === 'number'
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
`myDog`应该包含`friends`属性,并且是一个数组`array`。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (z) {
|
||||
if (
|
||||
z.hasOwnProperty('friends') &&
|
||||
z.friends !== undefined &&
|
||||
Array.isArray(z.friends)
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
`myDog`应该只包含给出的属性。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (z) {
|
||||
return Object.keys(z).length === 4;
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,14 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244dc
|
||||
title: 多个 if else 语句
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/caeJgsw'
|
||||
forumTopicId: 16772
|
||||
title: 多个 if else 语句
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<code>if/else</code>语句串联在一起可以实现复杂的逻辑,这是多个<code>if/else if</code>语句串联在一起的伪代码:
|
||||
# --description--
|
||||
|
||||
`if/else`语句串联在一起可以实现复杂的逻辑,这是多个`if/else if`语句串联在一起的伪代码:
|
||||
|
||||
```js
|
||||
if (condition1) {
|
||||
@ -23,92 +23,95 @@ if (condition1) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
把<code>if</code>/<code>else if</code>语句串联起来实现下面的逻辑:
|
||||
<code>num < 5</code>- return "Tiny"<br><code>num < 10</code>- return "Small"<br><code>num < 15</code>- return "Medium"<br><code>num < 20</code>- return "Large"<br><code>num >= 20</code> - return "Huge"
|
||||
</section>
|
||||
把`if`/`else if`语句串联起来实现下面的逻辑:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`num < 5`- return "Tiny"
|
||||
`num < 10`- return "Small"
|
||||
`num < 15`- return "Medium"
|
||||
`num < 20`- return "Large"
|
||||
`num >= 20` - return "Huge"
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该有至少 4 个<code>else</code>表达式。
|
||||
testString: assert(code.match(/else/g).length > 3);
|
||||
- text: 你应该有至少 4 个<code>if</code>表达式。
|
||||
testString: assert(code.match(/if/g).length > 3);
|
||||
- text: 你应该有至少 1 个<code>return</code>表达式。
|
||||
testString: assert(code.match(/return/g).length >= 1);
|
||||
- text: <code>testSize(0)</code>应该返回 "Tiny"。
|
||||
testString: assert(testSize(0) === "Tiny");
|
||||
- text: <code>testSize(4)</code>应该返回 "Tiny"。
|
||||
testString: assert(testSize(4) === "Tiny");
|
||||
- text: <code>testSize(5)</code>应该返回 "Small"。
|
||||
testString: assert(testSize(5) === "Small");
|
||||
- text: <code>testSize(8)</code>应该返回 "Small"。
|
||||
testString: assert(testSize(8) === "Small");
|
||||
- text: <code>testSize(10)</code>应该返回 "Medium"。
|
||||
testString: assert(testSize(10) === "Medium");
|
||||
- text: <code>testSize(14)</code>应该返回 "Medium"。
|
||||
testString: assert(testSize(14) === "Medium");
|
||||
- text: <code>testSize(15)</code>应该返回 "Large"。
|
||||
testString: assert(testSize(15) === "Large");
|
||||
- text: <code>testSize(17)</code>应该返回 "Large"。
|
||||
testString: assert(testSize(17) === "Large");
|
||||
- text: <code>testSize(20)</code>应该返回 "Huge"。
|
||||
testString: assert(testSize(20) === "Huge");
|
||||
- text: <code>testSize(25)</code>应该返回 "Huge"。
|
||||
testString: assert(testSize(25) === "Huge");
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该有至少 4 个`else`表达式。
|
||||
|
||||
```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);
|
||||
assert(code.match(/else/g).length > 3);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
你应该有至少 4 个`if`表达式。
|
||||
|
||||
```js
|
||||
function testSize(num) {
|
||||
if (num < 5) {
|
||||
return "Tiny";
|
||||
} else if (num < 10) {
|
||||
return "Small";
|
||||
} else if (num < 15) {
|
||||
return "Medium";
|
||||
} else if (num < 20) {
|
||||
return "Large";
|
||||
} else {
|
||||
return "Huge";
|
||||
}
|
||||
}
|
||||
assert(code.match(/if/g).length > 3);
|
||||
```
|
||||
|
||||
</section>
|
||||
你应该有至少 1 个`return`表达式。
|
||||
|
||||
```js
|
||||
assert(code.match(/return/g).length >= 1);
|
||||
```
|
||||
|
||||
`testSize(0)`应该返回 "Tiny"。
|
||||
|
||||
```js
|
||||
assert(testSize(0) === 'Tiny');
|
||||
```
|
||||
|
||||
`testSize(4)`应该返回 "Tiny"。
|
||||
|
||||
```js
|
||||
assert(testSize(4) === 'Tiny');
|
||||
```
|
||||
|
||||
`testSize(5)`应该返回 "Small"。
|
||||
|
||||
```js
|
||||
assert(testSize(5) === 'Small');
|
||||
```
|
||||
|
||||
`testSize(8)`应该返回 "Small"。
|
||||
|
||||
```js
|
||||
assert(testSize(8) === 'Small');
|
||||
```
|
||||
|
||||
`testSize(10)`应该返回 "Medium"。
|
||||
|
||||
```js
|
||||
assert(testSize(10) === 'Medium');
|
||||
```
|
||||
|
||||
`testSize(14)`应该返回 "Medium"。
|
||||
|
||||
```js
|
||||
assert(testSize(14) === 'Medium');
|
||||
```
|
||||
|
||||
`testSize(15)`应该返回 "Large"。
|
||||
|
||||
```js
|
||||
assert(testSize(15) === 'Large');
|
||||
```
|
||||
|
||||
`testSize(17)`应该返回 "Large"。
|
||||
|
||||
```js
|
||||
assert(testSize(17) === 'Large');
|
||||
```
|
||||
|
||||
`testSize(20)`应该返回 "Huge"。
|
||||
|
||||
```js
|
||||
assert(testSize(20) === 'Huge');
|
||||
```
|
||||
|
||||
`testSize(25)`应该返回 "Huge"。
|
||||
|
||||
```js
|
||||
assert(testSize(25) === 'Huge');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,72 +1,50 @@
|
||||
---
|
||||
id: bd7123c9c441eddfaeb4bdef
|
||||
title: 给代码添加注释
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c7ynnTp'
|
||||
forumTopicId: 16783
|
||||
title: 给代码添加注释
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
被注释的代码块在 JavaScript 之中是不会执行的。在代码中写注释是一个非常好的方式让你自己和其他人理解代码。
|
||||
|
||||
JavaScript 中的注释方式有以下两种:
|
||||
使用<code>//</code>注释掉当前行的代码
|
||||
|
||||
使用`//`注释掉当前行的代码
|
||||
|
||||
```js
|
||||
// This is an in-line comment.
|
||||
```
|
||||
|
||||
你也可以使用多行注释来注释你的代码,以<code>/*</code>开始,用<code>*/</code>来结束,就像下面这样:
|
||||
你也可以使用多行注释来注释你的代码,以<code>/<em></em></code>*开始,用\`\`*`/`来结束,就像下面这样:
|
||||
|
||||
```js
|
||||
/* This is a
|
||||
multi-line comment */
|
||||
```
|
||||
|
||||
<strong>最佳实践</strong><br>写代码的时候,要定期添加注释对部分代码块进行解释。适当的注释能让别人和你自己更容易看懂代码。
|
||||
</section>
|
||||
**最佳实践**
|
||||
写代码的时候,要定期添加注释对部分代码块进行解释。适当的注释能让别人和你自己更容易看懂代码。
|
||||
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
尝试创建这两种类型的注释。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 创建一个<code>//</code>样式的注释, 被注释的文本至少要包含 5 个字符。
|
||||
testString: assert(code.match(/(\/\/)...../g));
|
||||
- text: 创建一个<code>/* */</code>样式的注释, 被注释的文本至少要包含 5 个字符。
|
||||
testString: assert(code.match(/(\/\*)([^\/]{5,})(?=\*\/)/gm));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
创建一个`//`样式的注释, 被注释的文本至少要包含 5 个字符。
|
||||
|
||||
```js
|
||||
|
||||
assert(code.match(/(\/\/)...../g));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
创建一个`/* */`样式的注释, 被注释的文本至少要包含 5 个字符。
|
||||
|
||||
```js
|
||||
// Fake Comment
|
||||
/* Another Comment */
|
||||
assert(code.match(/(\/\*)([^\/]{5,})(?=\*\/)/gm));
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,16 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d0
|
||||
title: 相等运算符
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cKyVMAL'
|
||||
forumTopicId: 16784
|
||||
title: 相等运算符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
在 JavaScript 中,有很多<dfn>相互比较的操作</dfn>。所有这些操作符都返回一个<code>true</code>或<code>false</code>值。
|
||||
最基本的运算符是相等运算符:<code>==</code>。相等运算符比较两个值,如果它们是同等,返回<code>true</code>,如果它们不等,返回<code>false</code>。值得注意的是相等运算符不同于赋值运算符(<code>=</code>),赋值运算符是把等号右边的值赋给左边的变量。
|
||||
# --description--
|
||||
|
||||
在 JavaScript 中,有很多<dfn>相互比较的操作</dfn>。所有这些操作符都返回一个`true`或`false`值。
|
||||
|
||||
最基本的运算符是相等运算符:`==`。相等运算符比较两个值,如果它们是同等,返回`true`,如果它们不等,返回`false`。值得注意的是相等运算符不同于赋值运算符(`=`),赋值运算符是把等号右边的值赋给左边的变量。
|
||||
|
||||
```js
|
||||
function equalityTest(myVal) {
|
||||
@ -20,8 +21,7 @@ function equalityTest(myVal) {
|
||||
}
|
||||
```
|
||||
|
||||
如果<code>myVal</code>等于<code>10</code>,相等运算符会返回<code>true</code>,因此大括号里面的代码会被执行,函数将返回<code>"Equal"</code>。否则,函数返回<code>"Not Equal"</code>。
|
||||
在 JavaScript 中,为了让两个不同的<code>数据类型</code>(例如<code>数字</code>和<code>字符串</code>)的值可以作比较,它必须把一种类型转换为另一种类型。然而一旦这样做,它可以像下面这样来比较:
|
||||
如果`myVal`等于`10`,相等运算符会返回`true`,因此大括号里面的代码会被执行,函数将返回`"Equal"`。否则,函数返回`"Not Equal"`。 在 JavaScript 中,为了让两个不同的`数据类型`(例如`数字`和`字符串`)的值可以作比较,它必须把一种类型转换为另一种类型。然而一旦这样做,它可以像下面这样来比较:
|
||||
|
||||
```js
|
||||
1 == 1 // true
|
||||
@ -30,66 +30,35 @@ function equalityTest(myVal) {
|
||||
"3" == 3 // true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
把<code>相等运算符</code>添加到指定的行,这样当<code>val</code>的值为<code>12</code>的时候,函数会返回"Equal"。
|
||||
</section>
|
||||
把`相等运算符`添加到指定的行,这样当`val`的值为`12`的时候,函数会返回"Equal"。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testEqual(10)</code>应该返回 "Not Equal"。
|
||||
testString: assert(testEqual(10) === "Not Equal");
|
||||
- text: <code>testEqual(12)</code>应该返回 "Equal"。
|
||||
testString: assert(testEqual(12) === "Equal");
|
||||
- text: <code>testEqual("12")</code>应该返回 "Equal"。
|
||||
testString: assert(testEqual("12") === "Equal");
|
||||
- text: 你应该使用<code>==</code>运算符。
|
||||
testString: assert(code.match(/==/g) && !code.match(/===/g));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`testEqual(10)`应该返回 "Not Equal"。
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testEqual(10);
|
||||
assert(testEqual(10) === 'Not Equal');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`testEqual(12)`应该返回 "Equal"。
|
||||
|
||||
```js
|
||||
function testEqual(val) {
|
||||
if (val == 12) {
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
assert(testEqual(12) === 'Equal');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testEqual("12")`应该返回 "Equal"。
|
||||
|
||||
```js
|
||||
assert(testEqual('12') === 'Equal');
|
||||
```
|
||||
|
||||
你应该使用`==`运算符。
|
||||
|
||||
```js
|
||||
assert(code.match(/==/g) && !code.match(/===/g));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,16 +1,18 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d4
|
||||
title: 大于运算符
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cp6GbH4'
|
||||
forumTopicId: 16786
|
||||
title: 大于运算符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
使用大于运算符(<code>></code>)来比较两个数字。如果大于运算符左边的数字大于右边的数字,将会返回<code>true</code>。否则,它返回<code>false</code>。
|
||||
# --description--
|
||||
|
||||
使用大于运算符(`>`)来比较两个数字。如果大于运算符左边的数字大于右边的数字,将会返回`true`。否则,它返回`false`。
|
||||
|
||||
与相等运算符一样,大于运算符在比较的时候,会转换值的数据类型。
|
||||
<strong>例如</strong>
|
||||
|
||||
**例如**
|
||||
|
||||
```js
|
||||
5 > 3 // true
|
||||
@ -19,81 +21,59 @@ title: 大于运算符
|
||||
'1' > 9 // false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
添加<code>大于</code>运算符到指定的行,使得返回的语句是有意义的。
|
||||
</section>
|
||||
添加`大于`运算符到指定的行,使得返回的语句是有意义的。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testGreaterThan(0)</code>应该返回 "10 or Under"。
|
||||
testString: assert(testGreaterThan(0) === "10 or Under");
|
||||
- text: <code>testGreaterThan(10)</code>应该返回 "10 or Under"。
|
||||
testString: assert(testGreaterThan(10) === "10 or Under");
|
||||
- text: <code>testGreaterThan(11)</code>应该返回 "Over 10"。
|
||||
testString: assert(testGreaterThan(11) === "Over 10");
|
||||
- text: <code>testGreaterThan(99)</code>应该返回 "Over 10"。
|
||||
testString: assert(testGreaterThan(99) === "Over 10");
|
||||
- text: <code>testGreaterThan(100)</code>应该返回 "Over 10"。
|
||||
testString: assert(testGreaterThan(100) === "Over 10");
|
||||
- text: <code>testGreaterThan(101)</code>应该返回 "Over 100"。
|
||||
testString: assert(testGreaterThan(101) === "Over 100");
|
||||
- text: <code>testGreaterThan(150)</code>应该返回 "Over 100"。
|
||||
testString: assert(testGreaterThan(150) === "Over 100");
|
||||
- text: 你应该使用<code>></code>运算符至少两次。
|
||||
testString: assert(code.match(/val\s*>\s*('|")*\d+('|")*/g).length > 1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`testGreaterThan(0)`应该返回 "10 or Under"。
|
||||
|
||||
```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);
|
||||
assert(testGreaterThan(0) === '10 or Under');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`testGreaterThan(10)`应该返回 "10 or Under"。
|
||||
|
||||
```js
|
||||
function testGreaterThan(val) {
|
||||
if (val > 100) { // Change this line
|
||||
return "Over 100";
|
||||
}
|
||||
if (val > 10) { // Change this line
|
||||
return "Over 10";
|
||||
}
|
||||
return "10 or Under";
|
||||
}
|
||||
assert(testGreaterThan(10) === '10 or Under');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testGreaterThan(11)`应该返回 "Over 10"。
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(11) === 'Over 10');
|
||||
```
|
||||
|
||||
`testGreaterThan(99)`应该返回 "Over 10"。
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(99) === 'Over 10');
|
||||
```
|
||||
|
||||
`testGreaterThan(100)`应该返回 "Over 10"。
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(100) === 'Over 10');
|
||||
```
|
||||
|
||||
`testGreaterThan(101)`应该返回 "Over 100"。
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(101) === 'Over 100');
|
||||
```
|
||||
|
||||
`testGreaterThan(150)`应该返回 "Over 100"。
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(150) === 'Over 100');
|
||||
```
|
||||
|
||||
你应该使用`>`运算符至少两次。
|
||||
|
||||
```js
|
||||
assert(code.match(/val\s*>\s*('|")*\d+('|")*/g).length > 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,16 +1,18 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d5
|
||||
title: 大于或等于运算符
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c6KBqtV'
|
||||
forumTopicId: 16785
|
||||
title: 大于或等于运算符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
使用<code>大于等于</code>运算符(<code>>=</code>)来比较两个数字的大小。如果大于等于运算符左边的数字比右边的数字大或者相等,它会返回<code>true</code>。否则,它会返回<code>false</code>。
|
||||
与相等运算符相似,<code>大于等于</code>运算符在比较的时候会转换值的数据类型。
|
||||
<strong>例如</strong>
|
||||
# --description--
|
||||
|
||||
使用`大于等于`运算符(`>=`)来比较两个数字的大小。如果大于等于运算符左边的数字比右边的数字大或者相等,它会返回`true`。否则,它会返回`false`。
|
||||
|
||||
与相等运算符相似,`大于等于`运算符在比较的时候会转换值的数据类型。
|
||||
|
||||
**例如**
|
||||
|
||||
```js
|
||||
6 >= 6 // true
|
||||
@ -19,83 +21,59 @@ title: 大于或等于运算符
|
||||
'7' >= 9 // false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
添加<code>大于等于</code>运算符到指定行,使得函数的返回语句有意义。
|
||||
</section>
|
||||
添加`大于等于`运算符到指定行,使得函数的返回语句有意义。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testGreaterOrEqual(0)</code>应该返回 "Less than 10"。
|
||||
testString: assert(testGreaterOrEqual(0) === "Less than 10");
|
||||
- text: <code>testGreaterOrEqual(9)</code>应该返回 "Less than 10"。
|
||||
testString: assert(testGreaterOrEqual(9) === "Less than 10");
|
||||
- text: <code>testGreaterOrEqual(10)</code>应该返回 "10 or Over"。
|
||||
testString: assert(testGreaterOrEqual(10) === "10 or Over");
|
||||
- text: <code>testGreaterOrEqual(11)</code>应该返回 "10 or Over"。
|
||||
testString: assert(testGreaterOrEqual(11) === "10 or Over");
|
||||
- text: <code>testGreaterOrEqual(19)</code>应该返回 "10 or Over"。
|
||||
testString: assert(testGreaterOrEqual(19) === "10 or Over");
|
||||
- text: <code>testGreaterOrEqual(100)</code>应该返回 "20 or Over"。
|
||||
testString: assert(testGreaterOrEqual(100) === "20 or Over");
|
||||
- text: <code>testGreaterOrEqual(21)</code>应该返回 "20 or Over"。
|
||||
testString: assert(testGreaterOrEqual(21) === "20 or Over");
|
||||
- text: 你应该使用<code>>=</code>运算符至少两次。
|
||||
testString: assert(code.match(/val\s*>=\s*('|")*\d+('|")*/g).length > 1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`testGreaterOrEqual(0)`应该返回 "Less than 10"。
|
||||
|
||||
```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);
|
||||
assert(testGreaterOrEqual(0) === 'Less than 10');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`testGreaterOrEqual(9)`应该返回 "Less than 10"。
|
||||
|
||||
```js
|
||||
function testGreaterOrEqual(val) {
|
||||
if (val >= 20) { // Change this line
|
||||
return "20 or Over";
|
||||
}
|
||||
|
||||
if (val >= 10) { // Change this line
|
||||
return "10 or Over";
|
||||
}
|
||||
|
||||
return "Less than 10";
|
||||
}
|
||||
assert(testGreaterOrEqual(9) === 'Less than 10');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testGreaterOrEqual(10)`应该返回 "10 or Over"。
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(10) === '10 or Over');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(11)`应该返回 "10 or Over"。
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(11) === '10 or Over');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(19)`应该返回 "10 or Over"。
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(19) === '10 or Over');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(100)`应该返回 "20 or Over"。
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(100) === '20 or Over');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(21)`应该返回 "20 or Over"。
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(21) === '20 or Over');
|
||||
```
|
||||
|
||||
你应该使用`>=`运算符至少两次。
|
||||
|
||||
```js
|
||||
assert(code.match(/val\s*>=\s*('|")*\d+('|")*/g).length > 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,16 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d2
|
||||
title: 不等运算符
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cdBm9Sr'
|
||||
forumTopicId: 16787
|
||||
title: 不等运算符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
不相等运算符(<code>!=</code>)与相等运算符是相反的。这意味着不相等运算符中,如果“不为真”并且返回<code>false</code>的地方,在相等运算符中会返回<code>true</code>,<em>反之亦然</em>。与相等运算符类似,不相等运算符在比较的时候也会转换值的数据类型。
|
||||
<strong>例如</strong>
|
||||
# --description--
|
||||
|
||||
不相等运算符(`!=`)与相等运算符是相反的。这意味着不相等运算符中,如果“不为真”并且返回`false`的地方,在相等运算符中会返回`true`,*反之亦然*。与相等运算符类似,不相等运算符在比较的时候也会转换值的数据类型。
|
||||
|
||||
**例如**
|
||||
|
||||
```js
|
||||
1 != 2 // true
|
||||
@ -19,70 +20,47 @@ title: 不等运算符
|
||||
0 != false // false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
在<code>if</code>语句中,添加不相等运算符<code>!=</code>,这样函数在当<code>val</code>不等于 <code>99</code>的时候,会返回 "Not Equal"。
|
||||
</section>
|
||||
在`if`语句中,添加不相等运算符`!=`,这样函数在当`val`不等于 `99`的时候,会返回 "Not Equal"。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testNotEqual(99)</code>应该返回 "Equal"。
|
||||
testString: assert(testNotEqual(99) === "Equal");
|
||||
- text: <code>testNotEqual("99")</code>应该返回 "Equal"。
|
||||
testString: assert(testNotEqual("99") === "Equal");
|
||||
- text: <code>testNotEqual(12)</code>应该返回 "Not Equal"。
|
||||
testString: assert(testNotEqual(12) === "Not Equal");
|
||||
- text: <code>testNotEqual("12")</code>应该返回 "Not Equal"。
|
||||
testString: assert(testNotEqual("12") === "Not Equal");
|
||||
- text: <code>testNotEqual("bob")</code>应该返回 "Not Equal"。
|
||||
testString: assert(testNotEqual("bob") === "Not Equal");
|
||||
- text: 你应该使用<code>!=</code>运算符。
|
||||
testString: assert(code.match(/(?!!==)!=/));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`testNotEqual(99)`应该返回 "Equal"。
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testNotEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Not Equal";
|
||||
}
|
||||
return "Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testNotEqual(10);
|
||||
assert(testNotEqual(99) === 'Equal');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`testNotEqual("99")`应该返回 "Equal"。
|
||||
|
||||
```js
|
||||
function testNotEqual(val) {
|
||||
if (val != 99) {
|
||||
return "Not Equal";
|
||||
}
|
||||
return "Equal";
|
||||
}
|
||||
assert(testNotEqual('99') === 'Equal');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testNotEqual(12)`应该返回 "Not Equal"。
|
||||
|
||||
```js
|
||||
assert(testNotEqual(12) === 'Not Equal');
|
||||
```
|
||||
|
||||
`testNotEqual("12")`应该返回 "Not Equal"。
|
||||
|
||||
```js
|
||||
assert(testNotEqual('12') === 'Not Equal');
|
||||
```
|
||||
|
||||
`testNotEqual("bob")`应该返回 "Not Equal"。
|
||||
|
||||
```js
|
||||
assert(testNotEqual('bob') === 'Not Equal');
|
||||
```
|
||||
|
||||
你应该使用`!=`运算符。
|
||||
|
||||
```js
|
||||
assert(code.match(/(?!!==)!=/));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,16 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d6
|
||||
title: 小于运算符
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cNVRWtB'
|
||||
forumTopicId: 16789
|
||||
title: 小于运算符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
使用<dfn>小于</dfn>运算符(<code><</code>)比较两个数字的大小。如果小于运算符左边的数字比右边的数字小,它会返回<code>true</code>。否则会返回<code>false</code>。与相等运算符类似,<dfn>小于</dfn> 运算符在做比较的时候会转换值的数据类型。
|
||||
<strong>例如</strong>
|
||||
# --description--
|
||||
|
||||
使用<dfn>小于</dfn>运算符(`<`)比较两个数字的大小。如果小于运算符左边的数字比右边的数字小,它会返回`true`。否则会返回`false`。与相等运算符类似,<dfn>小于</dfn> 运算符在做比较的时候会转换值的数据类型。
|
||||
|
||||
**例如**
|
||||
|
||||
```js
|
||||
2 < 5 // true
|
||||
@ -19,81 +20,53 @@ title: 小于运算符
|
||||
'8' < 4 // false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
添加<code>小于</code>运算符到指定行,使得函数的返回语句有意义。
|
||||
</section>
|
||||
添加`小于`运算符到指定行,使得函数的返回语句有意义。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testLessThan(0)</code>应该返回 "Under 25"。
|
||||
testString: assert(testLessThan(0) === "Under 25");
|
||||
- text: <code>testLessThan(24)</code>应该返回 "Under 25"。
|
||||
testString: assert(testLessThan(24) === "Under 25");
|
||||
- text: <code>testLessThan(25)</code>应该返回 "Under 55"。
|
||||
testString: assert(testLessThan(25) === "Under 55");
|
||||
- text: <code>testLessThan(54)</code>应该返回 "Under 55"。
|
||||
testString: assert(testLessThan(54) === "Under 55");
|
||||
- text: <code>testLessThan(55)</code>应该返回 "55 or Over"。
|
||||
testString: assert(testLessThan(55) === "55 or Over");
|
||||
- text: <code>testLessThan(99)</code>应该返回 "55 or Over"。
|
||||
testString: assert(testLessThan(99) === "55 or Over");
|
||||
- text: 你应该使用<code><</code>运算符至少两次。
|
||||
testString: assert(code.match(/val\s*<\s*('|")*\d+('|")*/g).length > 1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`testLessThan(0)`应该返回 "Under 25"。
|
||||
|
||||
```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);
|
||||
assert(testLessThan(0) === 'Under 25');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`testLessThan(24)`应该返回 "Under 25"。
|
||||
|
||||
```js
|
||||
function testLessThan(val) {
|
||||
if (val < 25) { // Change this line
|
||||
return "Under 25";
|
||||
}
|
||||
|
||||
if (val < 55) { // Change this line
|
||||
return "Under 55";
|
||||
}
|
||||
|
||||
return "55 or Over";
|
||||
}
|
||||
assert(testLessThan(24) === 'Under 25');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testLessThan(25)`应该返回 "Under 55"。
|
||||
|
||||
```js
|
||||
assert(testLessThan(25) === 'Under 55');
|
||||
```
|
||||
|
||||
`testLessThan(54)`应该返回 "Under 55"。
|
||||
|
||||
```js
|
||||
assert(testLessThan(54) === 'Under 55');
|
||||
```
|
||||
|
||||
`testLessThan(55)`应该返回 "55 or Over"。
|
||||
|
||||
```js
|
||||
assert(testLessThan(55) === '55 or Over');
|
||||
```
|
||||
|
||||
`testLessThan(99)`应该返回 "55 or Over"。
|
||||
|
||||
```js
|
||||
assert(testLessThan(99) === '55 or Over');
|
||||
```
|
||||
|
||||
你应该使用`<`运算符至少两次。
|
||||
|
||||
```js
|
||||
assert(code.match(/val\s*<\s*('|")*\d+('|")*/g).length > 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,16 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d7
|
||||
title: 小于或等于运算符
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cNVR7Am'
|
||||
forumTopicId: 16788
|
||||
title: 小于或等于运算符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
使用<code>小于等于</code>运算符(<code><=</code>)比较两个数字的大小。如果在小于等于运算符左边的数字小于或者等于右边的数字,它会返回<code>true</code>。如果在小于等于运算符左边的数字大于右边的数字,它会返回<code>false</code>。与相等运算符类似,<code>小于等于</code>运算符会转换数据类型。
|
||||
<strong>例如</strong>
|
||||
# --description--
|
||||
|
||||
使用`小于等于`运算符(`<=`)比较两个数字的大小。如果在小于等于运算符左边的数字小于或者等于右边的数字,它会返回`true`。如果在小于等于运算符左边的数字大于右边的数字,它会返回`false`。与相等运算符类似,`小于等于`运算符会转换数据类型。
|
||||
|
||||
**例如**
|
||||
|
||||
```js
|
||||
4 <= 5 // true
|
||||
@ -19,84 +20,59 @@ title: 小于或等于运算符
|
||||
'8' <= 4 // false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
添加<code>小于等于</code>运算符到指定行,使得函数的返回语句有意义。
|
||||
</section>
|
||||
添加`小于等于`运算符到指定行,使得函数的返回语句有意义。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testLessOrEqual(0)</code>应该返回 "Smaller Than or Equal to 12"。
|
||||
testString: assert(testLessOrEqual(0) === "Smaller Than or Equal to 12");
|
||||
- text: <code>testLessOrEqual(11)</code>应该返回 "Smaller Than or Equal to 12"。
|
||||
testString: assert(testLessOrEqual(11) === "Smaller Than or Equal to 12");
|
||||
- text: <code>testLessOrEqual(12)</code>应该返回 "Smaller Than or Equal to 12"。
|
||||
testString: assert(testLessOrEqual(12) === "Smaller Than or Equal to 12");
|
||||
- text: <code>testLessOrEqual(23)</code>应该返回 "Smaller Than or Equal to 24"。
|
||||
testString: assert(testLessOrEqual(23) === "Smaller Than or Equal to 24");
|
||||
- text: <code>testLessOrEqual(24)</code>应该返回 "Smaller Than or Equal to 24"。
|
||||
testString: assert(testLessOrEqual(24) === "Smaller Than or Equal to 24");
|
||||
- text: <code>testLessOrEqual(25)</code>应该返回 "More Than 24"。
|
||||
testString: assert(testLessOrEqual(25) === "More Than 24");
|
||||
- text: <code>testLessOrEqual(55)</code>应该返回 "More Than 24"。
|
||||
testString: assert(testLessOrEqual(55) === "More Than 24");
|
||||
- text: 你应该使用<code><=</code>运算符至少两。
|
||||
testString: assert(code.match(/val\s*<=\s*('|")*\d+('|")*/g).length > 1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`testLessOrEqual(0)`应该返回 "Smaller Than or Equal to 12"。
|
||||
|
||||
```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);
|
||||
|
||||
assert(testLessOrEqual(0) === 'Smaller Than or Equal to 12');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`testLessOrEqual(11)`应该返回 "Smaller Than or Equal to 12"。
|
||||
|
||||
```js
|
||||
function testLessOrEqual(val) {
|
||||
if (val <= 12) { // Change this line
|
||||
return "Smaller Than or Equal to 12";
|
||||
}
|
||||
|
||||
if (val <= 24) { // Change this line
|
||||
return "Smaller Than or Equal to 24";
|
||||
}
|
||||
|
||||
return "More Than 24";
|
||||
}
|
||||
assert(testLessOrEqual(11) === 'Smaller Than or Equal to 12');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testLessOrEqual(12)`应该返回 "Smaller Than or Equal to 12"。
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(12) === 'Smaller Than or Equal to 12');
|
||||
```
|
||||
|
||||
`testLessOrEqual(23)`应该返回 "Smaller Than or Equal to 24"。
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(23) === 'Smaller Than or Equal to 24');
|
||||
```
|
||||
|
||||
`testLessOrEqual(24)`应该返回 "Smaller Than or Equal to 24"。
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(24) === 'Smaller Than or Equal to 24');
|
||||
```
|
||||
|
||||
`testLessOrEqual(25)`应该返回 "More Than 24"。
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(25) === 'More Than 24');
|
||||
```
|
||||
|
||||
`testLessOrEqual(55)`应该返回 "More Than 24"。
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(55) === 'More Than 24');
|
||||
```
|
||||
|
||||
你应该使用`<=`运算符至少两。
|
||||
|
||||
```js
|
||||
assert(code.match(/val\s*<=\s*('|")*\d+('|")*/g).length > 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,83 +1,55 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d1
|
||||
title: 严格相等运算符
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cy87atr'
|
||||
forumTopicId: 16790
|
||||
title: 严格相等运算符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
严格相等运算符(<code>===</code>)是相对相等操作符(<code>==</code>)的另一种比较操作符。与相等操作符不同的是,它会同时比较元素的值和<code>数据类型</code>。
|
||||
# --description--
|
||||
|
||||
严格相等运算符(`===`)是相对相等操作符(`==`)的另一种比较操作符。与相等操作符不同的是,它会同时比较元素的值和`数据类型`。
|
||||
|
||||
如果比较的值类型不同,那么在严格相等运算符比较下它们是不相等的,会返回 false 。
|
||||
<strong>示例</strong>
|
||||
|
||||
**示例**
|
||||
|
||||
```js
|
||||
3 === 3 // true
|
||||
3 === '3' // false
|
||||
```
|
||||
|
||||
<code>3</code>是一个<code>数字</code>类型的,而<code>'3'</code>是一个<code>字符串</code>类型的,所以 3 不全等于 '3'。
|
||||
</section>
|
||||
`3`是一个`数字`类型的,而`'3'`是一个`字符串`类型的,所以 3 不全等于 '3'。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
在<code>if</code>语句值使用严格相等运算符,这样当<code>val</code>严格等于7的时候,函数会返回"Equal"。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
在`if`语句值使用严格相等运算符,这样当`val`严格等于7的时候,函数会返回"Equal"。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testStrict(10)</code>应该返回 "Not Equal"。
|
||||
testString: assert(testStrict(10) === "Not Equal");
|
||||
- text: <code>testStrict(7)</code>应该返回 "Equal"。
|
||||
testString: assert(testStrict(7) === "Equal");
|
||||
- text: <code>testStrict("7")</code>应该返回 "Not Equal"。
|
||||
testString: assert(testStrict("7") === "Not Equal");
|
||||
- text: 你应该使用<code>===</code>运算符。
|
||||
testString: assert(code.match(/(val\s*===\s*\d+)|(\d+\s*===\s*val)/g).length > 0);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`testStrict(10)`应该返回 "Not Equal"。
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testStrict(val) {
|
||||
if (val) { // Change this line
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testStrict(10);
|
||||
assert(testStrict(10) === 'Not Equal');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`testStrict(7)`应该返回 "Equal"。
|
||||
|
||||
```js
|
||||
function testStrict(val) {
|
||||
if (val === 7) {
|
||||
return "Equal";
|
||||
}
|
||||
return "Not Equal";
|
||||
}
|
||||
assert(testStrict(7) === 'Equal');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testStrict("7")`应该返回 "Not Equal"。
|
||||
|
||||
```js
|
||||
assert(testStrict('7') === 'Not Equal');
|
||||
```
|
||||
|
||||
你应该使用`===`运算符。
|
||||
|
||||
```js
|
||||
assert(code.match(/(val\s*===\s*\d+)|(\d+\s*===\s*val)/g).length > 0);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,16 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d3
|
||||
title: 严格不等运算符
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cKekkUy'
|
||||
forumTopicId: 16791
|
||||
title: 严格不等运算符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
严格不相等运算符(<code>!==</code>)与全等运算符是相反的。这意味着严格不相等并返回<code>false</code>的地方,用严格相等运算符会返回<code>true</code>,<em>反之亦然</em>。严格不相等运算符不会转换值的数据类型。
|
||||
<strong>示例</strong>
|
||||
# --description--
|
||||
|
||||
严格不相等运算符(`!==`)与全等运算符是相反的。这意味着严格不相等并返回`false`的地方,用严格相等运算符会返回`true`,*反之亦然*。严格不相等运算符不会转换值的数据类型。
|
||||
|
||||
**示例**
|
||||
|
||||
```js
|
||||
3 !== 3 // false
|
||||
@ -17,68 +18,41 @@ title: 严格不等运算符
|
||||
4 !== 3 // true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
在<code>if</code>语句中,添加严格不相等运算符<code>!==</code>,这样如果<code>val</code>与<code>17</code>严格不相等的时候,函数会返回 "Not Equal"。
|
||||
</section>
|
||||
在`if`语句中,添加严格不相等运算符`!==`,这样如果`val`与`17`严格不相等的时候,函数会返回 "Not Equal"。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testStrictNotEqual(17)</code>应该返回 "Equal"。
|
||||
testString: assert(testStrictNotEqual(17) === "Equal");
|
||||
- text: <code>testStrictNotEqual("17")</code>应该返回 "Not Equal"。
|
||||
testString: assert(testStrictNotEqual("17") === "Not Equal");
|
||||
- text: <code>testStrictNotEqual(12)</code>应该返回 "Not Equal"。
|
||||
testString: assert(testStrictNotEqual(12) === "Not Equal");
|
||||
- text: <code>testStrictNotEqual("bob")</code>应该返回 "Not Equal"。
|
||||
testString: assert(testStrictNotEqual("bob") === "Not Equal");
|
||||
- text: 应该使用 <code>!==</code> 运算符。
|
||||
testString: assert(code.match(/(val\s*!==\s*\d+)|(\d+\s*!==\s*val)/g).length > 0);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`testStrictNotEqual(17)`应该返回 "Equal"。
|
||||
|
||||
```js
|
||||
// Setup
|
||||
function testStrictNotEqual(val) {
|
||||
if (val) { // Change this line
|
||||
return "Not Equal";
|
||||
}
|
||||
return "Equal";
|
||||
}
|
||||
|
||||
// Change this value to test
|
||||
testStrictNotEqual(10);
|
||||
assert(testStrictNotEqual(17) === 'Equal');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`testStrictNotEqual("17")`应该返回 "Not Equal"。
|
||||
|
||||
```js
|
||||
function testStrictNotEqual(val) {
|
||||
if (val !== 17) {
|
||||
return "Not Equal";
|
||||
}
|
||||
return "Equal";
|
||||
}
|
||||
assert(testStrictNotEqual('17') === 'Not Equal');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testStrictNotEqual(12)`应该返回 "Not Equal"。
|
||||
|
||||
```js
|
||||
assert(testStrictNotEqual(12) === 'Not Equal');
|
||||
```
|
||||
|
||||
`testStrictNotEqual("bob")`应该返回 "Not Equal"。
|
||||
|
||||
```js
|
||||
assert(testStrictNotEqual('bob') === 'Not Equal');
|
||||
```
|
||||
|
||||
应该使用 `!==` 运算符。
|
||||
|
||||
```js
|
||||
assert(code.match(/(val\s*!==\s*\d+)|(\d+\s*!==\s*val)/g).length > 0);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d8
|
||||
title: 逻辑与运算符
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cvbRVtr'
|
||||
forumTopicId: 16799
|
||||
title: 逻辑与运算符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
有时你需要在一次判断中做多个操作。当且仅当运算符的左边和右边都是<code>true</code>,<dfn>逻辑与</dfn> 运算符(<code>&&</code>)才会返回<code>true</code>。
|
||||
# --description--
|
||||
|
||||
有时你需要在一次判断中做多个操作。当且仅当运算符的左边和右边都是`true`,<dfn>逻辑与</dfn> 运算符(`&&`)才会返回`true`。
|
||||
|
||||
同样的效果可以通过 if 语句的嵌套来实现:
|
||||
|
||||
```js
|
||||
@ -20,7 +21,7 @@ if (num > 5) {
|
||||
return "No";
|
||||
```
|
||||
|
||||
只有当<code>num</code>的值在 6 和 9 之间(包括 6 和 9)才会返回 "Yes"。相同的逻辑可被写为:
|
||||
只有当`num`的值在 6 和 9 之间(包括 6 和 9)才会返回 "Yes"。相同的逻辑可被写为:
|
||||
|
||||
```js
|
||||
if (num > 5 && num < 10) {
|
||||
@ -29,83 +30,71 @@ if (num > 5 && num < 10) {
|
||||
return "No";
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
请使用逻辑与运算符把两个 if 语句合并为一个 if 语句,如果<code>val</code>小于或等于<code>50</code>并且大于或等于<code>25</code>,返回<code>"Yes"</code>。否则,将返回<code>"No"</code>。
|
||||
</section>
|
||||
请使用逻辑与运算符把两个 if 语句合并为一个 if 语句,如果`val`小于或等于`50`并且大于或等于`25`,返回`"Yes"`。否则,将返回`"No"`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>&&</code>运算符一次。
|
||||
testString: assert(code.match(/&&/g).length === 1,);
|
||||
- text: 你应该只有一个<code>if</code>表达式。
|
||||
testString: assert(code.match(/if/g).length === 1);
|
||||
- text: <code>testLogicalAnd(0)</code>应该返回 "No"。
|
||||
testString: assert(testLogicalAnd(0) === "No");
|
||||
- text: <code>testLogicalAnd(24)</code>应该返回 "No"。
|
||||
testString: assert(testLogicalAnd(24) === "No");
|
||||
- text: <code>testLogicalAnd(25)</code>应该返回 "Yes"。
|
||||
testString: assert(testLogicalAnd(25) === "Yes");
|
||||
- text: <code>testLogicalAnd(30)</code>应该返回 "Yes"。
|
||||
testString: assert(testLogicalAnd(30) === "Yes");
|
||||
- text: <code>testLogicalAnd(50)</code>应该返回 "Yes"。
|
||||
testString: assert(testLogicalAnd(50) === "Yes");
|
||||
- text: <code>testLogicalAnd(51)</code>应该返回 "No"。
|
||||
testString: assert(testLogicalAnd(51) === "No");
|
||||
- text: <code>testLogicalAnd(75)</code>应该返回 "No"。
|
||||
testString: assert(testLogicalAnd(75) === "No");
|
||||
- text: <code>testLogicalAnd(80)</code>应该返回 "No"。
|
||||
testString: assert(testLogicalAnd(80) === "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);
|
||||
assert(code.match(/&&/g).length === 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
你应该只有一个`if`表达式。
|
||||
|
||||
```js
|
||||
function testLogicalAnd(val) {
|
||||
if (val >= 25 && val <= 50) {
|
||||
return "Yes";
|
||||
}
|
||||
return "No";
|
||||
}
|
||||
assert(code.match(/if/g).length === 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
`testLogicalAnd(0)`应该返回 "No"。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(0) === 'No');
|
||||
```
|
||||
|
||||
`testLogicalAnd(24)`应该返回 "No"。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(24) === 'No');
|
||||
```
|
||||
|
||||
`testLogicalAnd(25)`应该返回 "Yes"。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(25) === 'Yes');
|
||||
```
|
||||
|
||||
`testLogicalAnd(30)`应该返回 "Yes"。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(30) === 'Yes');
|
||||
```
|
||||
|
||||
`testLogicalAnd(50)`应该返回 "Yes"。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(50) === 'Yes');
|
||||
```
|
||||
|
||||
`testLogicalAnd(51)`应该返回 "No"。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(51) === 'No');
|
||||
```
|
||||
|
||||
`testLogicalAnd(75)`应该返回 "No"。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(75) === 'No');
|
||||
```
|
||||
|
||||
`testLogicalAnd(80)`应该返回 "No"。
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(80) === 'No');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,17 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244d9
|
||||
title: 逻辑或运算符
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cEPrGTN'
|
||||
forumTopicId: 16800
|
||||
title: 逻辑或运算符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
只要<dfn>逻辑或</dfn>运算符<code>||</code>两边任何一个为<code>true</code>,那么它就返回<code>true</code>;否则返回<code>false</code>。
|
||||
# --description--
|
||||
|
||||
只要<dfn>逻辑或</dfn>运算符`||`两边任何一个为`true`,那么它就返回`true`;否则返回`false`。
|
||||
|
||||
<dfn>逻辑或</dfn>运算符由两个管道符号(|)组成。这个按键位于退格键和回车键之间。
|
||||
|
||||
下面这样的语句你应该很熟悉:
|
||||
|
||||
```js
|
||||
@ -22,7 +24,7 @@ if (num < 5) {
|
||||
return "Yes";
|
||||
```
|
||||
|
||||
只有当<code>num</code>大于等于 5 或小于等于 10 时,函数返回"Yes"。相同的逻辑可以简写成:
|
||||
只有当`num`大于等于 5 或小于等于 10 时,函数返回"Yes"。相同的逻辑可以简写成:
|
||||
|
||||
```js
|
||||
if (num > 10 || num < 5) {
|
||||
@ -31,85 +33,71 @@ if (num > 10 || num < 5) {
|
||||
return "Yes";
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
请使用逻辑或运算符把两个 if 语句合并为一个 if 语句,如果<code>val</code>不在 10 和 20 之间(包括 10 和 20),返回<code>"Outside"</code>。反之,返回<code>"Inside"</code>。
|
||||
</section>
|
||||
请使用逻辑或运算符把两个 if 语句合并为一个 if 语句,如果`val`不在 10 和 20 之间(包括 10 和 20),返回`"Outside"`。反之,返回`"Inside"`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用一次<code>||</code>操作符。
|
||||
testString: assert(code.match(/\|\|/g).length === 1);
|
||||
- text: 你应该只有一个<code>if</code>表达式。
|
||||
testString: assert(code.match(/if/g).length === 1);
|
||||
- text: <code>testLogicalOr(0)</code>应该返回 "Outside"。
|
||||
testString: assert(testLogicalOr(0) === "Outside");
|
||||
- text: <code>testLogicalOr(9)</code>应该返回 "Outside"。
|
||||
testString: assert(testLogicalOr(9) === "Outside");
|
||||
- text: <code>testLogicalOr(10)</code>应该返回 "Inside"。
|
||||
testString: assert(testLogicalOr(10) === "Inside");
|
||||
- text: <code>testLogicalOr(15)</code>应该返回 "Inside"。
|
||||
testString: assert(testLogicalOr(15) === "Inside");
|
||||
- text: <code>testLogicalOr(19)</code>应该返回 "Inside"。
|
||||
testString: assert(testLogicalOr(19) === "Inside");
|
||||
- text: <code>testLogicalOr(20)</code>应该返回 "Inside"。
|
||||
testString: assert(testLogicalOr(20) === "Inside");
|
||||
- text: <code>testLogicalOr(21)</code>应该返回 "Outside"。
|
||||
testString: assert(testLogicalOr(21) === "Outside");
|
||||
- text: <code>testLogicalOr(25)</code>应该返回 "Outside"。
|
||||
testString: assert(testLogicalOr(25) === "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);
|
||||
assert(code.match(/\|\|/g).length === 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
你应该只有一个`if`表达式。
|
||||
|
||||
```js
|
||||
function testLogicalOr(val) {
|
||||
if (val < 10 || val > 20) {
|
||||
return "Outside";
|
||||
}
|
||||
return "Inside";
|
||||
}
|
||||
assert(code.match(/if/g).length === 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
`testLogicalOr(0)`应该返回 "Outside"。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(0) === 'Outside');
|
||||
```
|
||||
|
||||
`testLogicalOr(9)`应该返回 "Outside"。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(9) === 'Outside');
|
||||
```
|
||||
|
||||
`testLogicalOr(10)`应该返回 "Inside"。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(10) === 'Inside');
|
||||
```
|
||||
|
||||
`testLogicalOr(15)`应该返回 "Inside"。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(15) === 'Inside');
|
||||
```
|
||||
|
||||
`testLogicalOr(19)`应该返回 "Inside"。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(19) === 'Inside');
|
||||
```
|
||||
|
||||
`testLogicalOr(20)`应该返回 "Inside"。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(20) === 'Inside');
|
||||
```
|
||||
|
||||
`testLogicalOr(21)`应该返回 "Outside"。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(21) === 'Outside');
|
||||
```
|
||||
|
||||
`testLogicalOr(25)`应该返回 "Outside"。
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(25) === 'Outside');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,17 +1,20 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244af
|
||||
title: 复合赋值之 +=
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cDR6LCb'
|
||||
forumTopicId: 16661
|
||||
title: 复合赋值之 +=
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
在编程当中,通常通过赋值来修改变量的内容。记住,赋值时 Javascript 会先计算<code>=</code>右边的内容,所以我们可以写这样的语句:
|
||||
<code>myVar = myVar + 5;</code>
|
||||
# --description--
|
||||
|
||||
在编程当中,通常通过赋值来修改变量的内容。记住,赋值时 Javascript 会先计算`=`右边的内容,所以我们可以写这样的语句:
|
||||
|
||||
`myVar = myVar + 5;`
|
||||
|
||||
以上是最常见的运算赋值语句,即先运算、再赋值。还有一类操作符是一步到位既做运算也赋值的。
|
||||
其中一种就是<code>+=</code>运算符。
|
||||
|
||||
其中一种就是`+=`运算符。
|
||||
|
||||
```js
|
||||
var myVar = 1;
|
||||
@ -19,77 +22,45 @@ myVar += 5;
|
||||
console.log(myVar); // Returns 6
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>+=</code>操作符实现同样的效果。
|
||||
</section>
|
||||
使用`+=`操作符实现同样的效果。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应该等于<code>15</code>。
|
||||
testString: assert(a === 15);
|
||||
- text: <code>b</code>应该等于<code>26</code>。
|
||||
testString: assert(b === 26);
|
||||
- text: <code>c</code>应该等于<code>19</code>。
|
||||
testString: assert(c === 19);
|
||||
- text: 你应该对每个变量使用<code>+=</code>操作符。
|
||||
testString: assert(code.match(/\+=/g).length === 3);
|
||||
- text: 不要修改注释上面的代码。
|
||||
testString: assert(/var a = 3;/.test(code) && /var b = 17;/.test(code) && /var c = 12;/.test(code));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`a`应该等于`15`。
|
||||
|
||||
```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;
|
||||
|
||||
assert(a === 15);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`b`应该等于`26`。
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
assert(b === 26);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`c`应该等于`19`。
|
||||
|
||||
```js
|
||||
var a = 3;
|
||||
var b = 17;
|
||||
var c = 12;
|
||||
|
||||
a += 12;
|
||||
b += 9;
|
||||
c += 7;
|
||||
assert(c === 19);
|
||||
```
|
||||
|
||||
</section>
|
||||
你应该对每个变量使用`+=`操作符。
|
||||
|
||||
```js
|
||||
assert(code.match(/\+=/g).length === 3);
|
||||
```
|
||||
|
||||
不要修改注释上面的代码。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/var a = 3;/.test(code) &&
|
||||
/var b = 17;/.test(code) &&
|
||||
/var c = 12;/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,88 +1,60 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b2
|
||||
title: 复合赋值之 /=
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c2QvKT2'
|
||||
forumTopicId: 16659
|
||||
title: 复合赋值之 /=
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<code>/=</code>操作符是让变量与另一个数相除并赋值。
|
||||
<code>myVar = myVar / 5;</code>
|
||||
变量<code>myVar</code>等于自身除以<code>5</code>的值。等价于:
|
||||
<code>myVar /= 5;</code>
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>/=</code>操作符实现同样的效果。
|
||||
</section>
|
||||
`/=`操作符是让变量与另一个数相除并赋值。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`myVar = myVar / 5;`
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应该等于<code>4</code>。
|
||||
testString: assert(a === 4);
|
||||
- text: <code>b</code>应该等于<code>27</code>。
|
||||
testString: assert(b === 27);
|
||||
- text: <code>c</code>应该等于<code>3</code>。
|
||||
testString: assert(c === 3);
|
||||
- text: 应该对每个变量使用<code>/=</code>操作符。
|
||||
testString: assert(code.match(/\/=/g).length === 3);
|
||||
- text: 不要修改注释上面的代码。
|
||||
testString: assert(/var a = 48;/.test(code) && /var b = 108;/.test(code) && /var c = 33;/.test(code));
|
||||
变量`myVar`等于自身除以`5`的值。等价于:
|
||||
|
||||
```
|
||||
`myVar /= 5;`
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
使用`/=`操作符实现同样的效果。
|
||||
|
||||
<div id='js-seed'>
|
||||
# --hints--
|
||||
|
||||
`a`应该等于`4`。
|
||||
|
||||
```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;
|
||||
|
||||
assert(a === 4);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`b`应该等于`27`。
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
assert(b === 27);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`c`应该等于`3`。
|
||||
|
||||
```js
|
||||
var a = 48;
|
||||
var b = 108;
|
||||
var c = 33;
|
||||
|
||||
a /= 12;
|
||||
b /= 4;
|
||||
c /= 11;
|
||||
assert(c === 3);
|
||||
```
|
||||
|
||||
</section>
|
||||
应该对每个变量使用`/=`操作符。
|
||||
|
||||
```js
|
||||
assert(code.match(/\/=/g).length === 3);
|
||||
```
|
||||
|
||||
不要修改注释上面的代码。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/var a = 48;/.test(code) &&
|
||||
/var b = 108;/.test(code) &&
|
||||
/var c = 33;/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,89 +1,60 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b1
|
||||
title: 复合赋值之 *=
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c83vrfa'
|
||||
forumTopicId: 16662
|
||||
title: 复合赋值之 *=
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<code>*=</code>操作符是让变量与一个数相乘并赋值。
|
||||
<code>myVar = myVar * 5;</code>
|
||||
变量<code>myVar</code>等于自身与数值<code>5</code>相乘的值。也可以写作这样的形式:
|
||||
<code>myVar *= 5;</code>
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>*=</code>操作符实现同样的效果。
|
||||
</section>
|
||||
`*=`操作符是让变量与一个数相乘并赋值。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`myVar = myVar * 5;`
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应该等于<code>25</code>。
|
||||
testString: assert(a === 25);
|
||||
- text: <code>b</code>应该等于<code>36</code>。
|
||||
testString: assert(b === 36);
|
||||
- text: <code>c</code>应该等于<code>46</code>。
|
||||
testString: assert(c === 46);
|
||||
- text: 应该对每个变量使用<code>*=</code>操作符。
|
||||
testString: assert(code.match(/\*=/g).length === 3);
|
||||
- text: 不要修改注释上面的代码。
|
||||
testString: assert(/var a = 5;/.test(code) && /var b = 12;/.test(code) && /var c = 4\.6;/.test(code));
|
||||
变量`myVar`等于自身与数值`5`相乘的值。也可以写作这样的形式:
|
||||
|
||||
```
|
||||
`myVar *= 5;`
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
使用`*=`操作符实现同样的效果。
|
||||
|
||||
<div id='js-seed'>
|
||||
# --hints--
|
||||
|
||||
`a`应该等于`25`。
|
||||
|
||||
```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;
|
||||
|
||||
|
||||
assert(a === 25);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`b`应该等于`36`。
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
assert(b === 36);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`c`应该等于`46`。
|
||||
|
||||
```js
|
||||
var a = 5;
|
||||
var b = 12;
|
||||
var c = 4.6;
|
||||
|
||||
a *= 5;
|
||||
b *= 3;
|
||||
c *= 10;
|
||||
assert(c === 46);
|
||||
```
|
||||
|
||||
</section>
|
||||
应该对每个变量使用`*=`操作符。
|
||||
|
||||
```js
|
||||
assert(code.match(/\*=/g).length === 3);
|
||||
```
|
||||
|
||||
不要修改注释上面的代码。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/var a = 5;/.test(code) &&
|
||||
/var b = 12;/.test(code) &&
|
||||
/var c = 4\.6;/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,91 +1,58 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b0
|
||||
title: 复合赋值之 -=
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c2Qv7AV'
|
||||
forumTopicId: 16660
|
||||
title: 复合赋值之 -=
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
与<code>+=</code>操作符类似,<code>-=</code>操作符用来对一个变量进行减法赋值操作。
|
||||
<code>myVar = myVar - 5;</code>
|
||||
变量<code>myVar</code>等于自身减去<code>5</code>的值。也可以写成这种形式:
|
||||
<code>myVar -= 5;</code>
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>-=</code>操作符实现同样的效果。
|
||||
</section>
|
||||
与`+=`操作符类似,`-=`操作符用来对一个变量进行减法赋值操作。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`myVar = myVar - 5;`
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code>应该等于<code>5</code>。
|
||||
testString: assert(a === 5);
|
||||
- text: <code>b</code>应该等于<code>-6</code>。
|
||||
testString: assert(b === -6);
|
||||
- text: <code>c</code>应该等于<code>2</code>。
|
||||
testString: assert(c === 2);
|
||||
- text: 应该对每个变量使用<code>-=</code>操作符。
|
||||
testString: assert(code.match(/-=/g).length === 3);
|
||||
- text: 不要修改注释上面的代码。
|
||||
testString: assert(/var a = 11;/.test(code) && /var b = 9;/.test(code) && /var c = 3;/.test(code));
|
||||
变量`myVar`等于自身减去`5`的值。也可以写成这种形式:
|
||||
|
||||
```
|
||||
`myVar -= 5;`
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
使用`-=`操作符实现同样的效果。
|
||||
|
||||
<div id='js-seed'>
|
||||
# --hints--
|
||||
|
||||
`a`应该等于`5`。
|
||||
|
||||
```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;
|
||||
|
||||
|
||||
assert(a === 5);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`b`应该等于`-6`。
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
assert(b === -6);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`c`应该等于`2`。
|
||||
|
||||
```js
|
||||
var a = 11;
|
||||
var b = 9;
|
||||
var c = 3;
|
||||
|
||||
a -= 6;
|
||||
b -= 15;
|
||||
c -= 1;
|
||||
|
||||
|
||||
assert(c === 2);
|
||||
```
|
||||
|
||||
</section>
|
||||
应该对每个变量使用`-=`操作符。
|
||||
|
||||
```js
|
||||
assert(code.match(/-=/g).length === 3);
|
||||
```
|
||||
|
||||
不要修改注释上面的代码。
|
||||
|
||||
```js
|
||||
assert(
|
||||
/var a = 11;/.test(code) && /var b = 9;/.test(code) && /var c = 3;/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,89 +1,53 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b7
|
||||
title: 用加号运算符连接字符串
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cNpM8AN'
|
||||
forumTopicId: 16802
|
||||
title: 用加号运算符连接字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
在 JavaScript 中,当对一个<code>String</code>类型的值使用<code>+</code>操作符的时候,它被称作 <dfn>拼接操作符</dfn>。你可以通过<dfn>拼接</dfn>其他字符串来创建一个新的字符串。
|
||||
<strong>示例</strong>
|
||||
# --description--
|
||||
|
||||
在 JavaScript 中,当对一个`String`类型的值使用`+`操作符的时候,它被称作 <dfn>拼接操作符</dfn>。你可以通过<dfn>拼接</dfn>其他字符串来创建一个新的字符串。
|
||||
|
||||
**示例**
|
||||
|
||||
```js
|
||||
'My name is Alan,' + ' I concatenate.'
|
||||
```
|
||||
|
||||
<strong>提示</strong><br>注意空格。拼接操作不会在两个字符串之间添加空格,所以想加上空格的话,你需要自己在字符串里面添加。
|
||||
</section>
|
||||
**提示**
|
||||
注意空格。拼接操作不会在两个字符串之间添加空格,所以想加上空格的话,你需要自己在字符串里面添加。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>+</code>操作符,把字符串<code>"This is the start. "</code>和<code>"This is the end."</code>连接起来并赋值给变量<code>myStr</code>。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
使用`+`操作符,把字符串`"This is the start. "`和`"This is the end."`连接起来并赋值给变量`myStr`。
|
||||
|
||||
```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.");
|
||||
- text: 使用<code>+</code>操作符构建<code>myStr</code>。
|
||||
testString: assert(code.match(/(["']).*(["'])\s*\+\s*(["']).*(["'])/g).length > 1);
|
||||
- text: <code>myStr</code>应该被<code>var</code>关键字声明。
|
||||
testString: assert(/var\s+myStr/.test(code));
|
||||
- text: 确保有给<code>myStr</code>赋值。
|
||||
testString: assert(/myStr\s*=/.test(code));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myStr`的值应该是`This is the start. This is the end.`。
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourStr = "I come first. " + "I come second.";
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var myStr;
|
||||
|
||||
|
||||
assert(myStr === 'This is the start. This is the end.');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
使用`+`操作符构建`myStr`。
|
||||
|
||||
```js
|
||||
(function(){
|
||||
if(typeof myStr === 'string') {
|
||||
return 'myStr = "' + myStr + '"';
|
||||
} else {
|
||||
return 'myStr is not a string';
|
||||
}
|
||||
})();
|
||||
assert(code.match(/(["']).*(["'])\s*\+\s*(["']).*(["'])/g).length > 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`myStr`应该被`var`关键字声明。
|
||||
|
||||
```js
|
||||
var ourStr = "I come first. " + "I come second.";
|
||||
var myStr = "This is the start. " + "This is the end.";
|
||||
assert(/var\s+myStr/.test(code));
|
||||
```
|
||||
|
||||
</section>
|
||||
确保有给`myStr`赋值。
|
||||
|
||||
```js
|
||||
assert(/myStr\s*=/.test(code));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,83 +1,39 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b8
|
||||
title: 用 += 运算符连接字符串
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cbQmmC4'
|
||||
forumTopicId: 16803
|
||||
title: 用 += 运算符连接字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
我们还可以使用<code>+=</code>运算符来<dfn>concatenate</dfn>(拼接)字符串到现有字符串的结尾。对于那些被分割成几段的长的字符串来说,这一操作是非常有用的。
|
||||
<strong>提示</strong><br>注意空格。拼接操作不会在两个字符串之间添加空格,所以如果想要加上空格的话,你需要自己在字符串里面添加。
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
通过使用<code>+=</code>操作符来连接这两个字符串:<br><code>"This is the first sentence. "</code>和<code>"This is the second sentence."</code>并赋给变量<code>myStr</code>。
|
||||
</section>
|
||||
我们还可以使用`+=`运算符来<dfn>concatenate</dfn>(拼接)字符串到现有字符串的结尾。对于那些被分割成几段的长的字符串来说,这一操作是非常有用的。
|
||||
|
||||
## 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.");
|
||||
- text: 使用<code>+=</code>操作符创建<code>myStr</code>变量。
|
||||
testString: assert(code.match(/\w\s*\+=\s*["']/g).length > 1 && code.match(/\w\s*\=\s*["']/g).length > 1);
|
||||
# --instructions--
|
||||
|
||||
```
|
||||
通过使用`+=`操作符来连接这两个字符串:
|
||||
`"This is the first sentence. "`和`"This is the second sentence."`并赋给变量`myStr`。
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myStr`的值应该是`This is the first sentence. This is the second sentence.`。
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourStr = "I come first. ";
|
||||
ourStr += "I come second.";
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var myStr;
|
||||
|
||||
|
||||
assert(myStr === 'This is the first sentence. This is the second sentence.');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
使用`+=`操作符创建`myStr`变量。
|
||||
|
||||
```js
|
||||
(function(){
|
||||
if(typeof myStr === 'string') {
|
||||
return 'myStr = "' + myStr + '"';
|
||||
} else {
|
||||
return 'myStr is not a string';
|
||||
}
|
||||
})();
|
||||
assert(
|
||||
code.match(/\w\s*\+=\s*["']/g).length > 1 &&
|
||||
code.match(/\w\s*\=\s*["']/g).length > 1
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var ourStr = "I come first. ";
|
||||
ourStr += "I come second.";
|
||||
|
||||
var myStr = "This is the first sentence. ";
|
||||
myStr += "This is the second sentence.";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,86 +1,32 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b9
|
||||
title: 用变量构造字符串
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cqk8rf4'
|
||||
forumTopicId: 16805
|
||||
title: 用变量构造字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
有时候你需要创建一个类似<a href="https://en.wikipedia.org/wiki/Mad_Libs" target="_blank">Mad Libs</a>(填词游戏)风格的字符串。通过使用连接运算符<code> + </code>,你可以插入一个或多个变量来组成一个字符串。
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
把你的名字赋值给变量<code>myName</code>,然后把变量<code>myName</code>插入到字符串<code>"My name is "</code>和<code>" and I am well!"</code>之间,并把连接后的结果赋值给变量<code>myStr</code>。
|
||||
</section>
|
||||
有时候你需要创建一个类似[Mad Libs](https://en.wikipedia.org/wiki/Mad_Libs)(填词游戏)风格的字符串。通过使用连接运算符`+`,你可以插入一个或多个变量来组成一个字符串。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myName</code>至少要包含三个字符。
|
||||
testString: assert(typeof myName !== 'undefined' && myName.length > 2);
|
||||
- text: 使用两个<code>+</code>操作符创建包含<code>myName</code>的<code>myStr</code>变量。
|
||||
testString: assert(code.match(/["']\s*\+\s*myName\s*\+\s*["']/g).length > 0);
|
||||
把你的名字赋值给变量`myName`,然后把变量`myName`插入到字符串`"My name is "`和`" and I am well!"`之间,并把连接后的结果赋值给变量`myStr`。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myName`至少要包含三个字符。
|
||||
|
||||
```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;
|
||||
|
||||
|
||||
assert(typeof myName !== 'undefined' && myName.length > 2);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
使用两个`+`操作符创建包含`myName`的`myStr`变量。
|
||||
|
||||
```js
|
||||
(function(){
|
||||
var output = [];
|
||||
if(typeof myName === 'string') {
|
||||
output.push('myName = "' + myName + '"');
|
||||
} else {
|
||||
output.push('myName is not a string');
|
||||
}
|
||||
if(typeof myStr === 'string') {
|
||||
output.push('myStr = "' + myStr + '"');
|
||||
} else {
|
||||
output.push('myStr is not a string');
|
||||
}
|
||||
return output.join('\n');
|
||||
})();
|
||||
assert(code.match(/["']\s*\+\s*myName\s*\+\s*["']/g).length > 0);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var myName = "Bob";
|
||||
var myStr = "My name is " + myName + " and I am well!";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,16 +1,18 @@
|
||||
---
|
||||
id: 56105e7b514f539506016a5e
|
||||
title: 使用 For 循环反向遍历数组
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c2R6BHa'
|
||||
forumTopicId: 16808
|
||||
title: 使用 For 循环反向遍历数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
for循环也可以逆向迭代,只要我们定义好合适的条件。
|
||||
为了让每次倒数递减 2,我们需要改变我们的<code>初始化</code>,<code>条件判断</code>和<code>计数器</code>。
|
||||
我们让<code>i = 10</code>,并且当<code>i > 0</code>的时候才继续循环。我们使用<code>i -= 2</code>来让<code>i</code>每次循环递减 2。
|
||||
|
||||
为了让每次倒数递减 2,我们需要改变我们的`初始化`,`条件判断`和`计数器`。
|
||||
|
||||
我们让`i = 10`,并且当`i > 0`的时候才继续循环。我们使用`i -= 2`来让`i`每次循环递减 2。
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@ -19,79 +21,31 @@ for (var i=10; i > 0; i-=2) {
|
||||
}
|
||||
```
|
||||
|
||||
循环结束后,<code>ourArray</code>的值为<code>[10,8,6,4,2]</code>。
|
||||
让我们改变<code>初始化</code>和<code>计数器</code>,这样我们就可以按照奇数从后往前两两倒着数。
|
||||
</section>
|
||||
循环结束后,`ourArray`的值为`[10,8,6,4,2]`。 让我们改变`初始化`和`计数器`,这样我们就可以按照奇数从后往前两两倒着数。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用一个<code>for</code>循环,把 9 到 1 的奇数添加进<code>myArray</code>。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
使用一个`for`循环,把 9 到 1 的奇数添加进`myArray`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>for</code>循环。
|
||||
testString: assert(code.match(/for\s*\(/g).length > 1);
|
||||
- text: 你应该使用数组方法<code>push</code>。
|
||||
testString: assert(code.match(/myArray.push/));
|
||||
- text: <code>myArray</code>应该等于<code>[9,7,5,3,1]</code>。
|
||||
testString: assert.deepEqual(myArray, [9,7,5,3,1]);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该使用`for`循环。
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [];
|
||||
|
||||
for (var i = 10; i > 0; i -= 2) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
|
||||
assert(code.match(/for\s*\(/g).length > 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
你应该使用数组方法`push`。
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
assert(code.match(/myArray.push/));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`myArray`应该等于`[9,7,5,3,1]`。
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
for (var i = 10; i > 0; i -= 2) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
var myArray = [];
|
||||
for (var i = 9; i > 0; i -= 2) {
|
||||
myArray.push(i);
|
||||
}
|
||||
assert.deepEqual(myArray, [9, 7, 5, 3, 1]);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,107 +1,156 @@
|
||||
---
|
||||
id: 565bbe00e9cc8ac0725390f4
|
||||
title: 21点游戏
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c6KE7ty'
|
||||
forumTopicId: 16809
|
||||
title: 21点游戏
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
在赌场 21 点游戏中,玩家可以通过计算牌桌上已经发放的卡牌的高低值来让自己在游戏中保持优势,这就叫<a href='https://www.douban.com/note/273781969/' target='_blank'> 21 点算法</a>。
|
||||
根据下面的表格,每张卡牌都分配了一个值。如果卡牌的值<count>大于 0,那么玩家应该追加赌注。反之,追加少许赌注甚至不追加赌注。
|
||||
<table class="table table-striped"><thead><tr><th>Count Change</th><th>Cards</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>
|
||||
你需要写一个函数实现 21 点算法,它根据参数<code>card</code>的值来递增或递减变量<code>count</code>,函数返回一个由当前<code>count</code>和<code>Bet</code>(<code>count>0</code>)或<code>Hold</code>(<code>count<=0</code>)拼接的字符串。注意<code>count</code>和<code>"Bet"</code>或<code>Hold</code>应该用空格分开。
|
||||
# --description--
|
||||
|
||||
<strong>例如:</strong><br><code>-3 Hold<br>5 Bet</code>
|
||||
在赌场 21 点游戏中,玩家可以通过计算牌桌上已经发放的卡牌的高低值来让自己在游戏中保持优势,这就叫[21 点算法](https://www.douban.com/note/273781969/)。
|
||||
|
||||
<strong>提示</strong><br>既然 card 的值为 7、8、9 时,count 值不变,那我们就可以忽略这种情况。
|
||||
</section>
|
||||
根据下面的表格,每张卡牌都分配了一个值。如果卡牌的值大于 0,那么玩家应该追加赌注。反之,追加少许赌注甚至不追加赌注。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
<table class='table table-striped'><thead><tr><th>Count Change</th><th>Cards</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>
|
||||
你需要写一个函数实现 21 点算法,它根据参数<code>card</code>的值来递增或递减变量<code>count</code>,函数返回一个由当前<code>count</code>和<code>Bet</code>(<code>count>0</code>)或<code>Hold</code>(<code>count<=0</code>)拼接的字符串。注意<code>count</code>和<code>"Bet"</code>或<code>Hold</code>应该用空格分开。</count>
|
||||
|
||||
</section>
|
||||
**例如:**
|
||||
<code>-3 Hold<br>5 Bet</code>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**提示**
|
||||
既然 card 的值为 7、8、9 时,count 值不变,那我们就可以忽略这种情况。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Cards Sequence 2, 3, 4, 5, 6 应该返回<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; })());
|
||||
- text: Cards Sequence 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; })());
|
||||
- text: Cards Sequence 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; })());
|
||||
- text: Cards Sequence 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; })());
|
||||
- text: Cards Sequence 2, J, 9, 2, 7 应该返回 <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; })());
|
||||
- text: Cards Sequence 2, 2, 10 应该返回 <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; })());
|
||||
- text: Cards Sequence 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; })());
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
Cards Sequence 2, 3, 4, 5, 6 应该返回`5 Bet`。
|
||||
|
||||
```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');
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(2);
|
||||
cc(3);
|
||||
cc(4);
|
||||
cc(5);
|
||||
var out = cc(6);
|
||||
if (out === '5 Bet') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
Cards Sequence 7, 8, 9 应该返回 `0 Hold`。
|
||||
|
||||
```js
|
||||
var count = 0;
|
||||
function cc(card) {
|
||||
switch(card) {
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
count++;
|
||||
break;
|
||||
case 10:
|
||||
case 'J':
|
||||
case 'Q':
|
||||
case 'K':
|
||||
case 'A':
|
||||
count--;
|
||||
}
|
||||
if(count > 0) {
|
||||
return count + " Bet";
|
||||
} else {
|
||||
return count + " Hold";
|
||||
}
|
||||
}
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(7);
|
||||
cc(8);
|
||||
var out = cc(9);
|
||||
if (out === '0 Hold') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
Cards Sequence 10, J, Q, K, A 应该返回 `-5 Hold`。
|
||||
|
||||
```js
|
||||
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 3, 7, Q, 8, A 应该返回 `-1 Hold`。
|
||||
|
||||
```js
|
||||
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 2, J, 9, 2, 7 应该返回 `1 Bet`。
|
||||
|
||||
```js
|
||||
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, 2, 10 应该返回 `1 Bet`。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(2);
|
||||
cc(2);
|
||||
var out = cc(10);
|
||||
if (out === '1 Bet') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
Cards Sequence 3, 2, A, 10, K 应该返回 `-1 Hold`。
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(3);
|
||||
cc(2);
|
||||
cc('A');
|
||||
cc(10);
|
||||
var out = cc('K');
|
||||
if (out === '-1 Hold') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,69 +1,35 @@
|
||||
---
|
||||
id: cf1391c1c11feddfaeb4bdef
|
||||
title: 创建一个小数
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ca8GEuW'
|
||||
forumTopicId: 16826
|
||||
title: 创建一个小数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
我们也可以把小数存储到变量中。小数也被称作<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.21</code>)。
|
||||
</section>
|
||||
**提示**
|
||||
不是所有的实数都可以用 <dfn>浮点数</dfn> 来表示。因为可能存在四舍五入的错误,[详情查看](https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems)。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myDecimal</code>应该是一个数字。
|
||||
testString: assert(typeof myDecimal === "number");
|
||||
- text: <code>myDecimal</code>应该包含小数点。
|
||||
testString: assert(myDecimal % 1 != 0);
|
||||
创建一个变量`myDecimal`并给它赋值一个浮点数。(例如`5.21`)。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myDecimal`应该是一个数字。
|
||||
|
||||
```js
|
||||
var ourDecimal = 5.7;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
assert(typeof myDecimal === 'number');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`myDecimal`应该包含小数点。
|
||||
|
||||
```js
|
||||
(function(){if(typeof myDecimal !== "undefined"){return myDecimal;}})();
|
||||
assert(myDecimal % 1 != 0);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var myDecimal = 9.9;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,78 +1,46 @@
|
||||
---
|
||||
id: bd7123c9c443eddfaeb5bdef
|
||||
title: 声明变量
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cNanrHq'
|
||||
forumTopicId: 17556
|
||||
title: 声明变量
|
||||
---
|
||||
|
||||
## 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>"12"</code>,<code>"dog"</code>或<code>"123 cats"</code>。计算机可以对数字执行数学运算,但不能对字符串执行数学运算。
|
||||
# --description--
|
||||
|
||||
在计算机科学中,<dfn>数据</dfn>就是一切,它对于计算机意义重大。JavaScript 提供七种不同的<dfn>数据类型</dfn>,它们是`undefined`(未定义), `null`(空),`boolean`(布尔型),`string`(字符串),`symbol`(符号),`number`(数字),和`object`(对象)。
|
||||
|
||||
例如,计算机区分数字和字符集合的字符串,例如数字`12`和字符串`"12"`,`"dog"`或`"123 cats"`。计算机可以对数字执行数学运算,但不能对字符串执行数学运算。
|
||||
|
||||
<dfn>变量</dfn>允许计算机以一种动态的形式来存储和操作数据,通过操作指向数据的指针而不是数据本身来避免了内存泄露,以上的七种数据类型都可以存储到一个变量中。
|
||||
<code>变量</code>非常类似于你在数学中使用的 x,y 变量,都是以一个简单命名的名称来代替我们赋值给它的数据。计算机中的<code>变量</code>与数学中的变量不同的是,计算机可以在不同的时间存储不同类型的变量。
|
||||
通过在变量的前面使用关键字<code>var</code>,声明一个变量,例如:
|
||||
|
||||
`变量`非常类似于你在数学中使用的 x,y 变量,都是以一个简单命名的名称来代替我们赋值给它的数据。计算机中的`变量`与数学中的变量不同的是,计算机可以在不同的时间存储不同类型的变量。
|
||||
|
||||
通过在变量的前面使用关键字`var`,声明一个变量,例如:
|
||||
|
||||
```js
|
||||
var ourName;
|
||||
```
|
||||
|
||||
上面代码的意思是创建一个名为<code>ourName</code>的<code>变量</code>,在 JavaScript 中我们以分号结束语句。
|
||||
<code>变量</code>名称可以由数字、字母、美元符号<code>$</code> 或者 下划线<code>_</code>组成,但是不能包含空格或者以数字为开头。
|
||||
</section>
|
||||
上面代码的意思是创建一个名为`ourName`的`变量`,在 JavaScript 中我们以分号结束语句。 `变量`名称可以由数字、字母、美元符号`$` 或者 下划线`_`组成,但是不能包含空格或者以数字为开头。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>var</code> 关键字来创建一个名为<code>myName</code>的变量。
|
||||
<strong>提示</strong><br>如果遇到困难了,请看下<code>ourName</code>的例子是怎么写的。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
使用`var` 关键字来创建一个名为`myName`的变量。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你需要使用<code>var</code>关键字定义一个变量<code>myName</code>,并使用分号结尾。
|
||||
testString: assert(/var\s+myName\s*;/.test(code), '你需要使用<code>var</code>关键字定义一个变量<code>myName</code>。并使用分号结尾。');
|
||||
**提示**
|
||||
如果遇到困难了,请看下`ourName`的例子是怎么写的。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你需要使用`var`关键字定义一个变量`myName`,并使用分号结尾。
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourName;
|
||||
|
||||
// Declare myName below this line
|
||||
|
||||
assert(
|
||||
/var\s+myName\s*;/.test(code),
|
||||
'你需要使用<code>var</code>关键字定义一个变量<code>myName</code>。并使用分号结尾。'
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
if(typeof myName !== "undefined"){(function(v){return v;})(myName);}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var myName;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,73 +1,60 @@
|
||||
---
|
||||
id: bd7123c9c444eddfaeb5bdef
|
||||
title: 声明字符串变量
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c2QvWU6'
|
||||
forumTopicId: 17557
|
||||
title: 声明字符串变量
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
之前我们写过这样的代码:
|
||||
<code>var myName = "your name";</code>
|
||||
<code>"your name"</code>被称作<dfn>字符串变量</dfn>。字符串是用单引号或双引号包裹起来的一连串的零个或多个字符。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
创建两个新的<code>字符串变量</code>:<code>myFirstName</code>和<code>myLastName</code>,并用你的姓和名分别为它们赋值。
|
||||
</section>
|
||||
`var myName = "your name";`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`"your name"`被称作<dfn>字符串变量</dfn>。字符串是用单引号或双引号包裹起来的一连串的零个或多个字符。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myFirstName</code>应该是一个字符串,并且至少包含一个字符。
|
||||
testString: assert((function(){if(typeof myFirstName !== "undefined" && typeof myFirstName === "string" && myFirstName.length > 0){return true;}else{return false;}})());
|
||||
- text: <code>myLastName</code>应该是一个字符串,并且至少包含一个字符。
|
||||
testString: assert((function(){if(typeof myLastName !== "undefined" && typeof myLastName === "string" && myLastName.length > 0){return true;}else{return false;}})());
|
||||
# --instructions--
|
||||
|
||||
```
|
||||
创建两个新的`字符串变量`:`myFirstName`和`myLastName`,并用你的姓和名分别为它们赋值。
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myFirstName`应该是一个字符串,并且至少包含一个字符。
|
||||
|
||||
```js
|
||||
// Example
|
||||
var firstName = "Alan";
|
||||
var lastName = "Turing";
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
typeof myFirstName !== 'undefined' &&
|
||||
typeof myFirstName === 'string' &&
|
||||
myFirstName.length > 0
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`myLastName`应该是一个字符串,并且至少包含一个字符。
|
||||
|
||||
```js
|
||||
if(typeof myFirstName !== "undefined" && typeof myLastName !== "undefined"){(function(){return myFirstName + ', ' + myLastName;})();}
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
typeof myLastName !== 'undefined' &&
|
||||
typeof myLastName === 'string' &&
|
||||
myLastName.length > 0
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var myFirstName = "Alan";
|
||||
var myLastName = "Turing";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,77 +1,55 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ad
|
||||
title: 数字递减
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cM2KeS2'
|
||||
forumTopicId: 17558
|
||||
title: 数字递减
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
使用自减符号<code>--</code>,你可以很方便地对一个变量执行<dfn>自减</dfn>或者<code>-1</code>运算。
|
||||
<code>i--;</code>
|
||||
# --description--
|
||||
|
||||
使用自减符号`--`,你可以很方便地对一个变量执行<dfn>自减</dfn>或者`-1`运算。
|
||||
|
||||
`i--;`
|
||||
|
||||
等效于
|
||||
<code>i = i - 1;</code>
|
||||
<strong>提示</strong><br><code>i--;</code>这种写法,省去了书写<code>=</code>符号的必要。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
重写代码,使用<code>--</code>符号对<code>myVar</code>执行自减操作。
|
||||
</section>
|
||||
`i = i - 1;`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**提示**
|
||||
`i--;`这种写法,省去了书写`=`符号的必要。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myVar</code>应该等于<code>10</code>。
|
||||
testString: assert(myVar === 10);
|
||||
- text: <code>myVar = myVar - 1;</code>语句应该被修改。
|
||||
testString: assert(/var\s*myVar\s*=\s*11;\s*\/*.*\s*([-]{2}\s*myVar|myVar\s*[-]{2});/.test(code));
|
||||
- text: 对<code>myVar</code>使用<code>--</code>运算符。
|
||||
testString: assert(/[-]{2}\s*myVar|myVar\s*[-]{2}/.test(code));
|
||||
- text: 不要修改注释上面的代码。
|
||||
testString: assert(/var myVar = 11;/.test(code));
|
||||
# --instructions--
|
||||
|
||||
```
|
||||
重写代码,使用`--`符号对`myVar`执行自减操作。
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myVar`应该等于`10`。
|
||||
|
||||
```js
|
||||
var myVar = 11;
|
||||
|
||||
// Only change code below this line
|
||||
myVar = myVar - 1;
|
||||
|
||||
assert(myVar === 10);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`myVar = myVar - 1;`语句应该被修改。
|
||||
|
||||
```js
|
||||
(function(z){return 'myVar = ' + z;})(myVar);
|
||||
assert(
|
||||
/var\s*myVar\s*=\s*11;\s*\/*.*\s*([-]{2}\s*myVar|myVar\s*[-]{2});/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
对`myVar`使用`--`运算符。
|
||||
|
||||
```js
|
||||
var myVar = 11;
|
||||
myVar--;
|
||||
assert(/[-]{2}\s*myVar|myVar\s*[-]{2}/.test(code));
|
||||
```
|
||||
|
||||
</section>
|
||||
不要修改注释上面的代码。
|
||||
|
||||
```js
|
||||
assert(/var myVar = 11;/.test(code));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,101 +1,34 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392d3
|
||||
title: 删除对象的属性
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cDqKdTv'
|
||||
forumTopicId: 17560
|
||||
title: 删除对象的属性
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
我们同样可以删除对象的属性,例如:
|
||||
<code>delete ourDog.bark;</code>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
删除<code>myDog</code>对象的<code>"tails"</code>属性。
|
||||
</section>
|
||||
`delete ourDog.bark;`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 从<code>myDog</code>中删除<code>"tails"</code>属性。
|
||||
testString: assert(typeof myDog === "object" && myDog.tails === undefined);
|
||||
- text: 不要修改<code>myDog</code>的初始化。
|
||||
testString: 'assert(code.match(/"tails": 1/g).length > 1);'
|
||||
删除`myDog`对象的`"tails"`属性。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
从`myDog`中删除`"tails"`属性。
|
||||
|
||||
```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.
|
||||
|
||||
|
||||
assert(typeof myDog === 'object' && myDog.tails === undefined);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
不要修改`myDog`的初始化。
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
assert(code.match(/"tails": 1/g).length > 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var ourDog = {
|
||||
"name": "Camper",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["everything!"],
|
||||
"bark": "bow-wow"
|
||||
};
|
||||
var myDog = {
|
||||
"name": "Happy Coder",
|
||||
"legs": 4,
|
||||
"tails": 1,
|
||||
"friends": ["freeCodeCamp Campers"],
|
||||
"bark": "woof"
|
||||
};
|
||||
delete myDog.tails;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,65 +1,38 @@
|
||||
---
|
||||
id: bd7993c9ca9feddfaeb7bdef
|
||||
title: 两个小数相除
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cBZe9AW'
|
||||
forumTopicId: 18255
|
||||
title: 两个小数相除
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
现在让我们将一个小数除以另一个小数。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
改变数值<code>0.0</code>的值让变量<code>quotient</code>的值等于<code>2.2</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
改变数值`0.0`的值让变量`quotient`的值等于`2.2`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>quotient</code>的值应该等于<code>2.2</code>。
|
||||
testString: assert(quotient === 2.2);
|
||||
- text: 使用<code>/</code>运算符将 4.4 除以 2。
|
||||
testString: assert(/4\.40*\s*\/\s*2\.*0*/.test(code));
|
||||
- text: quotient 变量应该只被赋值一次。
|
||||
testString: assert(code.match(/quotient/g).length === 1);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`quotient`的值应该等于`2.2`。
|
||||
|
||||
```js
|
||||
var quotient = 0.0 / 2.0; // Fix this line
|
||||
assert(quotient === 2.2);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
使用`/`运算符将 4.4 除以 2。
|
||||
|
||||
```js
|
||||
(function(y){return 'quotient = '+y;})(quotient);
|
||||
assert(/4\.40*\s*\/\s*2\.*0*/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
quotient 变量应该只被赋值一次。
|
||||
|
||||
```js
|
||||
var quotient = 4.4 / 2.0;
|
||||
assert(code.match(/quotient/g).length === 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,75 +1,40 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb6bdef
|
||||
title: 除法运算
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cqkbdAr'
|
||||
forumTopicId: 17566
|
||||
title: 除法运算
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
我们可以在 JavaScript 中做除法运算。
|
||||
JavaScript 中使用<code>/</code>符号做除法运算。
|
||||
# --description--
|
||||
|
||||
<strong>示例</strong>
|
||||
我们可以在 JavaScript 中做除法运算。
|
||||
|
||||
JavaScript 中使用`/`符号做除法运算。
|
||||
|
||||
**示例**
|
||||
|
||||
```js
|
||||
myVar = 16 / 2; // assigned 8
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
</section>
|
||||
改变数值`0`来让变量`quotient`的值等于`2`。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
改变数值<code>0</code>来让变量<code>quotient</code>的值等于<code>2</code>。
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 要使<code>quotient</code>的值等于 2。
|
||||
testString: assert(quotient === 2);
|
||||
- text: 使用<code>/</code>运算符。
|
||||
testString: assert(/\d+\s*\/\s*\d+/.test(code));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
要使`quotient`的值等于 2。
|
||||
|
||||
```js
|
||||
var quotient = 66 / 0;
|
||||
|
||||
|
||||
assert(quotient === 2);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
使用`/`运算符。
|
||||
|
||||
```js
|
||||
(function(z){return 'quotient = '+z;})(quotient);
|
||||
assert(/\d+\s*\/\s*\d+/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var quotient = 66 / 33;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,84 +1,76 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b6
|
||||
title: 字符串中的转义序列
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cvmqRh6'
|
||||
forumTopicId: 17567
|
||||
title: 字符串中的转义序列
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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>FirstLine<br/> \SecondLine<br/>ThirdLine</blockquote>
|
||||
<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>
|
||||
|
||||
*请注意,必须对反斜杠本身进行转义才能显示为反斜杠。*
|
||||
|
||||
# --instructions--
|
||||
|
||||
使用转义字符将下面三行文本字符串赋给变量`myStr`。
|
||||
|
||||
<blockquote>FirstLine<br> \SecondLine<br>ThirdLine</blockquote>
|
||||
|
||||
你需要使用转义字符正确地插入特殊字符,确保间距与上面文本一致并且单词或转义字符之间没有空格。
|
||||
|
||||
像这样用转义字符写出来:
|
||||
<q>FirstLine<code>换行符</code><code>制表符</code><code>反斜杠</code>SecondLine<code>换行符</code>ThirdLine</q>
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
"FirstLine```换行符``制表符``反斜杠```SecondLine`换行符`ThirdLine"
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myStr</code>不能包含空格。
|
||||
testString: assert(!/ /.test(myStr));
|
||||
- text: <code>myStr</code>应该包含字符串<code>FirstLine</code>, <code>SecondLine</code> and <code>ThirdLine</code> (记得区分大小写)。
|
||||
testString: assert(/FirstLine/.test(myStr) && /SecondLine/.test(myStr) && /ThirdLine/.test(myStr));
|
||||
- text: <code>FirstLine</code>后面应该是一个新行<code>\n</code>。
|
||||
testString: assert(/FirstLine\n/.test(myStr));
|
||||
- text: <code>myStr</code>应该包含制表符<code>\t</code>并且制表符要在换行符后面。
|
||||
testString: assert(/\n\t/.test(myStr));
|
||||
- text: <code>SecondLine</code>前面应该是反斜杠<code>\\</code>。
|
||||
testString: assert(/\SecondLine/.test(myStr));
|
||||
- text: <code>SecondLine</code>和<code>ThirdLine</code>之间应该是换行符。
|
||||
testString: assert(/SecondLine\nThirdLine/.test(myStr));
|
||||
- text: <code>myStr</code> 应该只包含介绍里面展示的字符串。
|
||||
testString: assert(myStr === 'FirstLine\n\t\\SecondLine\nThirdLine');
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myStr`不能包含空格。
|
||||
|
||||
```js
|
||||
var myStr; // Change this line
|
||||
|
||||
|
||||
assert(!/ /.test(myStr));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`myStr`应该包含字符串`FirstLine`, `SecondLine` and `ThirdLine` (记得区分大小写)。
|
||||
|
||||
```js
|
||||
(function(){
|
||||
if (myStr !== undefined){
|
||||
console.log('myStr:\n' + myStr);}})();
|
||||
assert(
|
||||
/FirstLine/.test(myStr) && /SecondLine/.test(myStr) && /ThirdLine/.test(myStr)
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`FirstLine`后面应该是一个新行`\n`。
|
||||
|
||||
```js
|
||||
var myStr = "FirstLine\n\t\\SecondLine\nThirdLine";
|
||||
assert(/FirstLine\n/.test(myStr));
|
||||
```
|
||||
|
||||
</section>
|
||||
`myStr`应该包含制表符`\t`并且制表符要在换行符后面。
|
||||
|
||||
```js
|
||||
assert(/\n\t/.test(myStr));
|
||||
```
|
||||
|
||||
`SecondLine`前面应该是反斜杠`\\`。
|
||||
|
||||
```js
|
||||
assert(/\SecondLine/.test(myStr));
|
||||
```
|
||||
|
||||
`SecondLine`和`ThirdLine`之间应该是换行符。
|
||||
|
||||
```js
|
||||
assert(/SecondLine\nThirdLine/.test(myStr));
|
||||
```
|
||||
|
||||
`myStr` 应该只包含介绍里面展示的字符串。
|
||||
|
||||
```js
|
||||
assert(myStr === 'FirstLine\n\t\\SecondLine\nThirdLine');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,77 +1,42 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244b5
|
||||
title: 转义字符串中的引号
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c2QvgSr'
|
||||
forumTopicId: 17568
|
||||
title: 转义字符串中的引号
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
定义一个字符串必须要用单引号或双引号来包裹它。那么当你的字符串里面包含:<code>"</code>或者<code>'</code>时该怎么办呢?
|
||||
在 JavaScript 中,你可以通过在引号前面使用<dfn>反斜杠</dfn>(<code>\</code>)来转义引号。
|
||||
<code>var sampleStr = "Alan said, \"Peter is learning JavaScript\".";</code>
|
||||
# --description--
|
||||
|
||||
定义一个字符串必须要用单引号或双引号来包裹它。那么当你的字符串里面包含:`"`或者`'`时该怎么办呢?
|
||||
|
||||
在 JavaScript 中,你可以通过在引号前面使用<dfn>反斜杠</dfn>(`\`)来转义引号。
|
||||
|
||||
`var sampleStr = "Alan said, \"Peter is learning JavaScript\".";`
|
||||
|
||||
有了转义符号,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>
|
||||
`Alan said, "Peter is learning JavaScript".`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的代码中应该包含两个双引号 (<code>"</code>) 以及四个转义的双引 (<code>\"</code>)。
|
||||
testString: assert(code.match(/\\"/g).length === 4 && code.match(/[^\\]"/g).length === 2);
|
||||
- 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\".");
|
||||
使用<dfn>反斜杠</dfn>将一个字符串赋值给变量`myStr`,打印到控制台,输出为:
|
||||
|
||||
```
|
||||
`I am a "double quoted" string inside "double quotes".`
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的代码中应该包含两个双引号 (`"`) 以及四个转义的双引 (`\"`)。
|
||||
|
||||
```js
|
||||
var myStr = ""; // Change this line
|
||||
|
||||
|
||||
assert(code.match(/\\"/g).length === 4 && code.match(/[^\\]"/g).length === 2);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
变量 myStr 应该包含字符串`I am a "double quoted" string inside "double quotes".`。
|
||||
|
||||
```js
|
||||
(function(){
|
||||
if(typeof myStr === 'string') {
|
||||
console.log("myStr = \"" + myStr + "\"");
|
||||
} else {
|
||||
console.log("myStr is undefined");
|
||||
}
|
||||
})();
|
||||
assert(myStr === 'I am a "double quoted" string inside "double quotes".');
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var myStr = "I am a \"double quoted\" string inside \"double quotes\".";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,78 +1,45 @@
|
||||
---
|
||||
id: bd7123c9c448eddfaeb5bdef
|
||||
title: 查找字符串的长度
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cvmqEAd'
|
||||
forumTopicId: 18182
|
||||
title: 查找字符串的长度
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
你可以通过在字符串变量或字符串后面写上<code>.length</code>来获得字符串变量值的长度。
|
||||
<code>"Alan Peter".length; // 10</code>
|
||||
例如,我们创建了一个变量<code>var firstName = "Charles"</code>,我们就可以通过使用<code>firstName.length</code>来获得<code>"Charles"</code>字符串的长度。
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>.length</code>属性来获得变量<code>lastName</code>的长度,并把它赋值给变量<code>lastNameLength</code>。
|
||||
</section>
|
||||
你可以通过在字符串变量或字符串后面写上`.length`来获得字符串变量值的长度。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`"Alan Peter".length; // 10`
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 不能改变 <code>// Setup</code> 部分声明的变量。
|
||||
testString: assert(code.match(/var lastNameLength = 0;/) && code.match(/var lastName = "Lovelace";/));
|
||||
- text: <code>lastNameLength</code>应该等于 8。
|
||||
testString: assert(typeof lastNameLength !== 'undefined' && lastNameLength === 8);
|
||||
- text: 你应该使用 <code>.length</code> 获取 <code>lastName</code> 的长度,像这样 <code>lastName.length</code>。
|
||||
testString: assert(code.match(/=\s*lastName\.length/g) && !code.match(/lastName\s*=\s*8/));
|
||||
例如,我们创建了一个变量`var firstName = "Charles"`,我们就可以通过使用`firstName.length`来获得`"Charles"`字符串的长度。
|
||||
|
||||
```
|
||||
# --instructions--
|
||||
|
||||
</section>
|
||||
使用`.length`属性来获得变量`lastName`的长度,并把它赋值给变量`lastNameLength`。
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
# --hints--
|
||||
|
||||
<div id='js-seed'>
|
||||
不能改变 `// Setup` 部分声明的变量。
|
||||
|
||||
```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;
|
||||
|
||||
|
||||
assert(
|
||||
code.match(/var lastNameLength = 0;/) &&
|
||||
code.match(/var lastName = "Lovelace";/)
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`lastNameLength`应该等于 8。
|
||||
|
||||
```js
|
||||
var firstNameLength = 0;
|
||||
var firstName = "Ada";
|
||||
firstNameLength = firstName.length;
|
||||
|
||||
var lastNameLength = 0;
|
||||
var lastName = "Lovelace";
|
||||
lastNameLength = lastName.length;
|
||||
assert(typeof lastNameLength !== 'undefined' && lastNameLength === 8);
|
||||
```
|
||||
|
||||
</section>
|
||||
你应该使用 `.length` 获取 `lastName` 的长度,像这样 `lastName.length`。
|
||||
|
||||
```js
|
||||
assert(code.match(/=\s*lastName\.length/g) && !code.match(/lastName\s*=\s*8/));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,74 +1,50 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ae
|
||||
title: 求余运算
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cWP24Ub'
|
||||
forumTopicId: 18184
|
||||
title: 求余运算
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<dfn>remainder</dfn>求余运算符<code>%</code>返回两个数相除得到的余数
|
||||
<strong>示例</strong>
|
||||
# --description--
|
||||
|
||||
<dfn>remainder</dfn>求余运算符`%`返回两个数相除得到的余数
|
||||
|
||||
**示例**
|
||||
|
||||
<blockquote>5 % 2 = 1 因为<br>Math.floor(5 / 2) = 2 (商)<br>2 * 2 = 4<br>5 - 4 = 1 (余数)</blockquote>
|
||||
<strong>用法</strong><br>在数学中,判断一个数是奇数还是偶数,只需要判断这个数除以 2 得到的余数是 0 还是 1。
|
||||
|
||||
**用法**
|
||||
在数学中,判断一个数是奇数还是偶数,只需要判断这个数除以 2 得到的余数是 0 还是 1。
|
||||
|
||||
<blockquote>17 % 2 = 1(17 是奇数)<br>48 % 2 = 0(48 是偶数)</blockquote>
|
||||
<strong>提示<strong><br>余数运算符(<dfn>remainder</dfn>)有时被错误地称为“模数”运算符。它与模数非常相似,但不能用于负数的运算。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>%</code>运算符,计算 11 除以 3 的余数,并把余数赋给变量 remainder。
|
||||
</section>
|
||||
**提示**
|
||||
余数运算符(<dfn>remainder</dfn>)有时被错误地称为“模数”运算符。它与模数非常相似,但不能用于负数的运算。\*\*\*\*
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 变量<code>remainder</code>应该被初始化。
|
||||
testString: assert(/var\s+?remainder/.test(code));
|
||||
- text: <code>remainder</code>的值应该等于<code>2</code>。
|
||||
testString: assert(remainder === 2);
|
||||
- text: 你应该使用<code>%</code>运算符。
|
||||
testString: assert(/\s+?remainder\s*?=\s*?.*%.*;/.test(code));
|
||||
使用`%`运算符,计算 11 除以 3 的余数,并把余数赋给变量 remainder。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
变量`remainder`应该被初始化。
|
||||
|
||||
```js
|
||||
// Only change code below this line
|
||||
|
||||
var remainder;
|
||||
|
||||
assert(/var\s+?remainder/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`remainder`的值应该等于`2`。
|
||||
|
||||
```js
|
||||
(function(y){return 'remainder = '+y;})(remainder);
|
||||
assert(remainder === 2);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
你应该使用`%`运算符。
|
||||
|
||||
```js
|
||||
var remainder = 11 % 3;
|
||||
assert(/\s+?remainder\s*?=\s*?.*%.*;/.test(code));
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,77 +1,43 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb9bdef
|
||||
title: 使用 JavaScript 生成随机分数
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cyWJJs3'
|
||||
forumTopicId: 18185
|
||||
title: 使用 JavaScript 生成随机分数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
随机数非常适合用来创建随机行为。
|
||||
<code>Math.random()</code>用来生成一个在<code>0</code>(包括 0)到<code>1</code>(不包括 1)之间的随机小数,因此<code>Math.random()</code>可能返回 0 但绝不会返回 1。
|
||||
<strong>提示</strong><br><a href='storing-values-with-the-assignment-operator' target='_blank'>使用赋值运算符存储值</a>这一节讲过,所有函数调用将在<code>return</code>执行之前解析,因此我们可以返回<code>Math.random()</code>函数的值。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
更改<code>randomFraction</code>使其返回一个随机数而不是<code>0</code>。
|
||||
</section>
|
||||
`Math.random()`用来生成一个在`0`(包括 0)到`1`(不包括 1)之间的随机小数,因此`Math.random()`可能返回 0 但绝不会返回 1。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**提示**
|
||||
[使用赋值运算符存储值](storing-values-with-the-assignment-operator)这一节讲过,所有函数调用将在`return`执行之前解析,因此我们可以返回`Math.random()`函数的值。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>randomFraction</code>应该返回一个随机数。
|
||||
testString: assert(typeof randomFraction() === "number");
|
||||
- text: <code>randomFraction</code>应该返回一个小数。
|
||||
testString: assert((randomFraction()+''). match(/\./g));
|
||||
- text: 需要使用<code>Math.random</code>生成随机的小数。
|
||||
testString: assert(code.match(/Math\.random/g).length >= 0);
|
||||
# --instructions--
|
||||
|
||||
```
|
||||
更改`randomFraction`使其返回一个随机数而不是`0`。
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`randomFraction`应该返回一个随机数。
|
||||
|
||||
```js
|
||||
function randomFraction() {
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
return 0;
|
||||
|
||||
// Only change code above this line.
|
||||
}
|
||||
assert(typeof randomFraction() === 'number');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`randomFraction`应该返回一个小数。
|
||||
|
||||
```js
|
||||
(function(){return randomFraction();})();
|
||||
assert((randomFraction() + '').match(/\./g));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
需要使用`Math.random`生成随机的小数。
|
||||
|
||||
```js
|
||||
function randomFraction() {
|
||||
return Math.random();
|
||||
}
|
||||
assert(code.match(/Math\.random/g).length >= 0);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,83 +1,63 @@
|
||||
---
|
||||
id: cf1111c1c12feddfaeb1bdef
|
||||
title: 使用 JavaScript 生成随机整数
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cRn6bfr'
|
||||
forumTopicId: 18186
|
||||
title: 使用 JavaScript 生成随机整数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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>Math.floor()</code>向下取整,所以最终我们获得的结果不可能有<code>20</code>。这确保了我们获得了一个在0到19之间的整数。
|
||||
|
||||
记住`Math.random()`永远不会返回`1`。同时因为我们是在用`Math.floor()`向下取整,所以最终我们获得的结果不可能有`20`。这确保了我们获得了一个在0到19之间的整数。
|
||||
|
||||
把操作连缀起来,代码类似于下面:
|
||||
<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>
|
||||
`Math.floor(Math.random() * 20);`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
我们先调用`Math.random()`,把它的结果乘以20,然后把上一步的结果传给`Math.floor()`,最终通过向下取整获得最近的整数。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myFunction</code>的结果应该是一个整数。
|
||||
testString: assert(typeof randomWholeNum() === "number" && (function(){var r = randomWholeNum();return Math.floor(r) === r;})());
|
||||
- text: 需要使用<code>Math.random</code>生成随机数字。
|
||||
testString: assert(code.match(/Math.random/g).length > 1);
|
||||
- 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));
|
||||
- text: 你需要使用<code>Math.floor</code>移除数字中的小数部分。
|
||||
testString: assert(code.match(/Math.floor/g).length > 1);
|
||||
# --instructions--
|
||||
|
||||
```
|
||||
生成一个`0`到`9`之间的随机整数。
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myFunction`的结果应该是一个整数。
|
||||
|
||||
```js
|
||||
var randomNumberBetween0and19 = Math.floor(Math.random() * 20);
|
||||
|
||||
function randomWholeNum() {
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
return Math.random();
|
||||
}
|
||||
assert(
|
||||
typeof randomWholeNum() === 'number' &&
|
||||
(function () {
|
||||
var r = randomWholeNum();
|
||||
return Math.floor(r) === r;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
需要使用`Math.random`生成随机数字。
|
||||
|
||||
```js
|
||||
(function(){return randomWholeNum();})();
|
||||
assert(code.match(/Math.random/g).length > 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
你应该将`Math.random`的结果乘以 10 来生成 0 到 9 之间的随机数。
|
||||
|
||||
```js
|
||||
var randomNumberBetween0and19 = Math.floor(Math.random() * 20);
|
||||
function randomWholeNum() {
|
||||
return Math.floor(Math.random() * 10);
|
||||
}
|
||||
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)
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
你需要使用`Math.floor`移除数字中的小数部分。
|
||||
|
||||
```js
|
||||
assert(code.match(/Math.floor/g).length > 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,103 +1,63 @@
|
||||
---
|
||||
id: cf1111c1c12feddfaeb2bdef
|
||||
title: 生成某个范围内的随机整数
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cm83yu6'
|
||||
forumTopicId: 18187
|
||||
title: 生成某个范围内的随机整数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
我们之前生成的随机数是在0到某个数之间,现在我们要生成的随机数是在两个指定的数之间。
|
||||
|
||||
我们需要定义一个最小值和一个最大值。
|
||||
|
||||
下面是我们将要使用的方法,仔细看看并尝试理解这行代码到底在干嘛:
|
||||
<code>Math.floor(Math.random() * (max - min + 1)) + min</code>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
创建一个叫<code>randomRange</code>的函数,参数为 myMin 和 myMax,返回一个在<code>myMin</code>(包括 myMin)和<code>myMax</code>(包括 myMax)之间的随机数。
|
||||
</section>
|
||||
`Math.floor(Math.random() * (max - min + 1)) + min`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>randomRange</code>返回的随机数应该大于或等于<code>myMin</code>。
|
||||
testString: assert(calcMin === 5);
|
||||
- text: <code>randomRange</code>返回的随机数应该小于或等于<code>myMax</code>。
|
||||
testString: assert(calcMax === 15);
|
||||
- text: <code>randomRange</code>应该返回一个随机整数, 而不是小数。
|
||||
testString: assert(randomRange(0,1) % 1 === 0 );
|
||||
- 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;}})());
|
||||
创建一个叫`randomRange`的函数,参数为 myMin 和 myMax,返回一个在`myMin`(包括 myMin)和`myMax`(包括 myMax)之间的随机数。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`randomRange`返回的随机数应该大于或等于`myMin`。
|
||||
|
||||
```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);
|
||||
assert(calcMin === 5);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`randomRange`返回的随机数应该小于或等于`myMax`。
|
||||
|
||||
```js
|
||||
var calcMin = 100;
|
||||
var calcMax = -100;
|
||||
for(var i = 0; i < 100; i++) {
|
||||
var result = randomRange(5,15);
|
||||
calcMin = Math.min(calcMin, result);
|
||||
calcMax = Math.max(calcMax, result);
|
||||
}
|
||||
(function(){
|
||||
if(typeof myRandom === 'number') {
|
||||
return "myRandom = " + myRandom;
|
||||
} else {
|
||||
return "myRandom undefined";
|
||||
}
|
||||
})()
|
||||
assert(calcMax === 15);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`randomRange`应该返回一个随机整数, 而不是小数。
|
||||
|
||||
```js
|
||||
function randomRange(myMin, myMax) {
|
||||
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
|
||||
}
|
||||
assert(randomRange(0, 1) % 1 === 0);
|
||||
```
|
||||
|
||||
</section>
|
||||
`randomRange`应该使用`myMax`和`myMin`, 并且返回两者之间的随机数。
|
||||
|
||||
```js
|
||||
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;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,136 +1,48 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244be
|
||||
title: 全局作用域和函数
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cQM7mCN'
|
||||
forumTopicId: 18193
|
||||
title: 全局作用域和函数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
在 JavaScript 中,<dfn>作用域</dfn>涉及到变量的作用范围。在函数外定义的变量具有 <dfn>全局</dfn> 作用域。这意味着,具有全局作用域的变量可以在代码的任何地方被调用。
|
||||
这些没有使用<code>var</code>关键字定义的变量,会被自动创建在全局作用域中,形成全局变量。当在代码其他地方无意间定义了一个变量,刚好变量名与全局变量相同,这时会产生意想不到的后果。因此你应该总是使用var关键字来声明你的变量。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
在函数外声明一个<code>全局</code>变量<code>myGlobal</code>,并给它一个初始值<code>10</code>
|
||||
在函数<code>fun1</code>的内部,<strong>不</strong>使用<code>var</code>关键字来声明<code>oopsGlobal</code>,并赋值为<code>5</code>。
|
||||
</section>
|
||||
这些没有使用`var`关键字定义的变量,会被自动创建在全局作用域中,形成全局变量。当在代码其他地方无意间定义了一个变量,刚好变量名与全局变量相同,这时会产生意想不到的后果。因此你应该总是使用var关键字来声明你的变量。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 应定义<code>myGlobal</code>。
|
||||
testString: assert(typeof myGlobal != "undefined");
|
||||
- text: <code>myGlobal</code>的值应为<code>10</code>。
|
||||
testString: assert(myGlobal === 10);
|
||||
- text: 应使用<code>var</code>关键字定义<code>myGlobal</code>。
|
||||
testString: assert(/var\s+myGlobal/.test(code));
|
||||
- text: <code>oopsGlobal</code>应为全局变量且值为<code>5</code>。
|
||||
testString: assert(typeof oopsGlobal != "undefined" && oopsGlobal === 5);
|
||||
在函数外声明一个`全局`变量`myGlobal`,并给它一个初始值`10`
|
||||
|
||||
```
|
||||
在函数`fun1`的内部,**不**使用`var`关键字来声明`oopsGlobal`,并赋值为`5`。
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
应定义`myGlobal`。
|
||||
|
||||
```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);
|
||||
}
|
||||
assert(typeof myGlobal != 'undefined');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
`myGlobal`的值应为`10`。
|
||||
|
||||
```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();
|
||||
assert(myGlobal === 10);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
应使用`var`关键字定义`myGlobal`。
|
||||
|
||||
```js
|
||||
fun1();
|
||||
fun2();
|
||||
uncapture();
|
||||
(function() { return logOutput || "console.log never called"; })();
|
||||
assert(/var\s+myGlobal/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`oopsGlobal`应为全局变量且值为`5`。
|
||||
|
||||
```js
|
||||
// Declare your variable here
|
||||
var myGlobal = 10;
|
||||
|
||||
function fun1() {
|
||||
// Assign 5 to oopsGlobal Here
|
||||
oopsGlobal = 5;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
assert(typeof oopsGlobal != 'undefined' && oopsGlobal === 5);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244c0
|
||||
title: 函数中的全局作用域和局部作用域
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c2QwKH2'
|
||||
forumTopicId: 18194
|
||||
title: 函数中的全局作用域和局部作用域
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
一个程序中有可能具有相同名称的<dfn>局部</dfn>变量 和<dfn>全局</dfn>变量。在这种情况下,<code>局部</code>变量将会优先于<code>全局</code>变量。
|
||||
# --description--
|
||||
|
||||
一个程序中有可能具有相同名称的<dfn>局部</dfn>变量 和<dfn>全局</dfn>变量。在这种情况下,`局部`变量将会优先于`全局`变量。
|
||||
|
||||
下面为例:
|
||||
|
||||
```js
|
||||
@ -19,67 +20,31 @@ function myFun() {
|
||||
}
|
||||
```
|
||||
|
||||
函数<code>myFun</code>将会返回<code>"Head"</code>,因为<code>局部变量</code>优先级更高。
|
||||
</section>
|
||||
函数`myFun`将会返回`"Head"`,因为`局部变量`优先级更高。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
给<code>myOutfit</code>添加一个局部变量来覆盖<code>outerWear</code>的值为<code>"sweater"</code>。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
给`myOutfit`添加一个局部变量来覆盖`outerWear`的值为`"sweater"`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 不要修改全局变量<code>outerWear</code>的值。
|
||||
testString: assert(outerWear === "T-Shirt");
|
||||
- text: <code>myOutfit</code>应该返回<code>"sweater"</code>。
|
||||
testString: assert(myOutfit() === "sweater");
|
||||
- text: 不要修改<code>return</code>语句。
|
||||
testString: assert(/return outerWear/.test(code));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
不要修改全局变量`outerWear`的值。
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var outerWear = "T-Shirt";
|
||||
|
||||
function myOutfit() {
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
return outerWear;
|
||||
}
|
||||
|
||||
myOutfit();
|
||||
assert(outerWear === 'T-Shirt');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`myOutfit`应该返回`"sweater"`。
|
||||
|
||||
```js
|
||||
var outerWear = "T-Shirt";
|
||||
function myOutfit() {
|
||||
var outerWear = "sweater";
|
||||
return outerWear;
|
||||
}
|
||||
assert(myOutfit() === 'sweater');
|
||||
```
|
||||
|
||||
</section>
|
||||
不要修改`return`语句。
|
||||
|
||||
```js
|
||||
assert(/return outerWear/.test(code));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,113 +1,88 @@
|
||||
---
|
||||
id: 5664820f61c48e80c9fa476c
|
||||
title: 高尔夫代码
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c9ykNUR'
|
||||
forumTopicId: 18195
|
||||
title: 高尔夫代码
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
在高尔夫<a href="https://en.wikipedia.org/wiki/Golf" target="_blank">golf</a>游戏中,每个洞都有自己的标准杆数<code>par</code>,代表着距离。根据你把球打进洞所挥杆的次数<code>strokes</code>,可以计算出你的高尔夫水平。
|
||||
函数将会传送 2 个参数,分别是标准杆数<code>par</code>和挥杆次数<code>strokes</code>,根据下面的表格返回正确的水平段位。
|
||||
<table class="table table-striped"><thead><tr><th>Strokes</th><th>Return</th></tr></thead><tbody><tr><td>1</td><td>"Hole-in-one!"</td></tr><tr><td><= par - 2</td><td>"Eagle"</td></tr><tr><td>par - 1</td><td>"Birdie"</td></tr><tr><td>par</td><td>"Par"</td></tr><tr><td>par + 1</td><td>"Bogey"</td></tr><tr><td>par + 2</td><td>"Double Bogey"</td></tr><tr><td>>= par + 3</td><td>"Go Home!"</td></tr></tbody></table>
|
||||
<code>par</code>和<code>strokes</code>必须是数字而且是正数。
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
在高尔夫[golf](https://en.wikipedia.org/wiki/Golf)游戏中,每个洞都有自己的标准杆数`par`,代表着距离。根据你把球打进洞所挥杆的次数`strokes`,可以计算出你的高尔夫水平。
|
||||
|
||||
</section>
|
||||
函数将会传送 2 个参数,分别是标准杆数`par`和挥杆次数`strokes`,根据下面的表格返回正确的水平段位。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
<table class='table table-striped'><thead><tr><th>Strokes</th><th>Return</th></tr></thead><tbody><tr><td>1</td><td>"Hole-in-one!"</td></tr><tr><td><= par - 2</td><td>"Eagle"</td></tr><tr><td>par - 1</td><td>"Birdie"</td></tr><tr><td>par</td><td>"Par"</td></tr><tr><td>par + 1</td><td>"Bogey"</td></tr><tr><td>par + 2</td><td>"Double Bogey"</td></tr><tr><td>>= par + 3</td><td>"Go Home!"</td></tr></tbody></table>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>golfScore(4, 1)</code>应该返回 "Hole-in-one!"。
|
||||
testString: assert(golfScore(4, 1) === "Hole-in-one!");
|
||||
- text: <code>golfScore(4, 2)</code>应该返回 "Eagle"。
|
||||
testString: assert(golfScore(4, 2) === "Eagle");
|
||||
- text: <code>golfScore(5, 2)</code>应该返回 "Eagle"。
|
||||
testString: assert(golfScore(5, 2) === "Eagle");
|
||||
- text: <code>golfScore(4, 3)</code>应该返回 "Birdie"。
|
||||
testString: assert(golfScore(4, 3) === "Birdie");
|
||||
- text: <code>golfScore(4, 4)</code>应该返回 "Par"。
|
||||
testString: assert(golfScore(4, 4) === "Par");
|
||||
- text: <code>golfScore(1, 1)</code>应该返回 "Hole-in-one!"。
|
||||
testString: assert(golfScore(1, 1) === "Hole-in-one!");
|
||||
- text: <code>golfScore(5, 5)</code>应该返回 "Par"。
|
||||
testString: assert(golfScore(5, 5) === "Par");
|
||||
- text: <code>golfScore(4, 5)</code>应该返回 "Bogey"。
|
||||
testString: assert(golfScore(4, 5) === "Bogey");
|
||||
- text: <code>golfScore(4, 6)</code>应该返回 "Double Bogey"。
|
||||
testString: assert(golfScore(4, 6) === "Double Bogey");
|
||||
- text: <code>golfScore(4, 7)</code>应该返回 "Go Home!"。
|
||||
testString: assert(golfScore(4, 7) === "Go Home!");
|
||||
- text: <code>golfScore(5, 9)</code>应该返回 "Go Home!"。
|
||||
testString: assert(golfScore(5, 9) === "Go Home!");
|
||||
`par`和`strokes`必须是数字而且是正数。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`golfScore(4, 1)`应该返回 "Hole-in-one!"。
|
||||
|
||||
```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);
|
||||
assert(golfScore(4, 1) === 'Hole-in-one!');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`golfScore(4, 2)`应该返回 "Eagle"。
|
||||
|
||||
```js
|
||||
function golfScore(par, strokes) {
|
||||
if (strokes === 1) {
|
||||
return "Hole-in-one!";
|
||||
}
|
||||
|
||||
if (strokes <= par - 2) {
|
||||
return "Eagle";
|
||||
}
|
||||
|
||||
if (strokes === par - 1) {
|
||||
return "Birdie";
|
||||
}
|
||||
|
||||
if (strokes === par) {
|
||||
return "Par";
|
||||
}
|
||||
|
||||
if (strokes === par + 1) {
|
||||
return "Bogey";
|
||||
}
|
||||
|
||||
if(strokes === par + 2) {
|
||||
return "Double Bogey";
|
||||
}
|
||||
|
||||
return "Go Home!";
|
||||
}
|
||||
assert(golfScore(4, 2) === 'Eagle');
|
||||
```
|
||||
|
||||
</section>
|
||||
`golfScore(5, 2)`应该返回 "Eagle"。
|
||||
|
||||
```js
|
||||
assert(golfScore(5, 2) === 'Eagle');
|
||||
```
|
||||
|
||||
`golfScore(4, 3)`应该返回 "Birdie"。
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 3) === 'Birdie');
|
||||
```
|
||||
|
||||
`golfScore(4, 4)`应该返回 "Par"。
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 4) === 'Par');
|
||||
```
|
||||
|
||||
`golfScore(1, 1)`应该返回 "Hole-in-one!"。
|
||||
|
||||
```js
|
||||
assert(golfScore(1, 1) === 'Hole-in-one!');
|
||||
```
|
||||
|
||||
`golfScore(5, 5)`应该返回 "Par"。
|
||||
|
||||
```js
|
||||
assert(golfScore(5, 5) === 'Par');
|
||||
```
|
||||
|
||||
`golfScore(4, 5)`应该返回 "Bogey"。
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 5) === 'Bogey');
|
||||
```
|
||||
|
||||
`golfScore(4, 6)`应该返回 "Double Bogey"。
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 6) === 'Double Bogey');
|
||||
```
|
||||
|
||||
`golfScore(4, 7)`应该返回 "Go Home!"。
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 7) === 'Go Home!');
|
||||
```
|
||||
|
||||
`golfScore(5, 9)`应该返回 "Go Home!"。
|
||||
|
||||
```js
|
||||
assert(golfScore(5, 9) === 'Go Home!');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,78 +1,56 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244ac
|
||||
title: 数字递增
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/ca8GLT9'
|
||||
forumTopicId: 18201
|
||||
title: 数字递增
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
使用<code>++</code>,我们可以很容易地对变量进行自增或者<code>+1</code>运算。
|
||||
<code>i++;</code>
|
||||
# --description--
|
||||
|
||||
使用`++`,我们可以很容易地对变量进行自增或者`+1`运算。
|
||||
|
||||
`i++;`
|
||||
|
||||
等效于
|
||||
<code>i = i + 1;</code>
|
||||
<strong>注意</strong><br><code>i++;</code>这种写法,省去了书写<code>=</code>符号的必要。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
重写代码,使用<code>++</code>来对变量<code>myVar</code>进行自增操作。
|
||||
<strong>提示</strong><br>了解更多关于<code>++</code>运算符<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#%E9%80%92%E5%A2%9E_()" target="_blank">Arithmetic operators - Increment (++)</a>.
|
||||
</section>
|
||||
`i = i + 1;`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**注意**
|
||||
`i++;`这种写法,省去了书写`=`符号的必要。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myVar</code>应该等于<code>88</code>。
|
||||
testString: assert(myVar === 88);
|
||||
- text: <code>myVar = myVar + 1;</code>语句应该被修改。
|
||||
testString: assert(/var\s*myVar\s*=\s*87;\s*\/*.*\s*myVar\+\+;/.test(code));
|
||||
- text: 使用<code>++</code>运算符。
|
||||
testString: assert(/[+]{2}\s*myVar|myVar\s*[+]{2}/.test(code));
|
||||
- text: 不要修改注释上方的代码。
|
||||
testString: assert(/var myVar = 87;/.test(code));
|
||||
# --instructions--
|
||||
|
||||
```
|
||||
重写代码,使用`++`来对变量`myVar`进行自增操作。
|
||||
|
||||
</section>
|
||||
**提示**
|
||||
了解更多关于`++`运算符[Arithmetic operators - Increment (++)](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#%E9%80%92%E5%A2%9E_()).
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
# --hints--
|
||||
|
||||
<div id='js-seed'>
|
||||
`myVar`应该等于`88`。
|
||||
|
||||
```js
|
||||
var myVar = 87;
|
||||
|
||||
// Only change code below this line
|
||||
myVar = myVar + 1;
|
||||
|
||||
assert(myVar === 88);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`myVar = myVar + 1;`语句应该被修改。
|
||||
|
||||
```js
|
||||
(function(z){return 'myVar = ' + z;})(myVar);
|
||||
assert(/var\s*myVar\s*=\s*87;\s*\/*.*\s*myVar\+\+;/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
使用`++`运算符。
|
||||
|
||||
```js
|
||||
var myVar = 87;
|
||||
myVar++;
|
||||
assert(/[+]{2}\s*myVar|myVar\s*[+]{2}/.test(code));
|
||||
```
|
||||
|
||||
</section>
|
||||
不要修改注释上方的代码。
|
||||
|
||||
```js
|
||||
assert(/var myVar = 87;/.test(code));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,68 +1,30 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244a9
|
||||
title: 使用赋值运算符初始化变量
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cWJ4Bfb'
|
||||
forumTopicId: 301171
|
||||
title: 使用赋值运算符初始化变量
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --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>
|
||||
`var myVar = 0;`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
创建一个名为`myVar`的变量并指定一个初始值`0`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你需要初始化<code>a</code>的值为<code>9</code>。
|
||||
testString: assert(/var\s+a\s*=\s*9\s*/.test(code));
|
||||
# --instructions--
|
||||
|
||||
```
|
||||
通过关键字`var`定义一个变量`a`并且给它一个初始值`9`。
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你需要初始化`a`的值为`9`。
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourVar = 19;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
assert(/var\s+a\s*=\s*9\s*/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
if(typeof a !== 'undefined') {(function(a){return "a = " + a;})(a);} else { (function() {return 'a is undefined';})(); }
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var a = 9;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,14 +1,14 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244db
|
||||
title: 介绍 else if 语句
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/caeJ2hm'
|
||||
forumTopicId: 18206
|
||||
title: 介绍 else if 语句
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
如果你有多个条件语句,你可以通过<code>else if</code>语句把<code>if</code>语句链起来。
|
||||
# --description--
|
||||
|
||||
如果你有多个条件语句,你可以通过`else if`语句把`if`语句链起来。
|
||||
|
||||
```js
|
||||
if (num > 15) {
|
||||
@ -20,82 +20,59 @@ if (num > 15) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>if else</code>实现同样的效果。
|
||||
</section>
|
||||
使用`if else`实现同样的效果。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该至少有两个<code>else</code>表达式。
|
||||
testString: assert(code.match(/else/g).length > 1);
|
||||
- text: 你应该至少有两个<code>if</code>表达式。
|
||||
testString: assert(code.match(/if/g).length > 1);
|
||||
- text: <code>testElseIf(0)</code>应该返回 "Smaller than 5"。
|
||||
testString: assert(testElseIf(0) === "Smaller than 5");
|
||||
- text: <code>testElseIf(5)</code>应该返回 "Between 5 and 10"。
|
||||
testString: assert(testElseIf(5) === "Between 5 and 10");
|
||||
- text: <code>testElseIf(7)</code>应该返回 "Between 5 and 10"。
|
||||
testString: assert(testElseIf(7) === "Between 5 and 10");
|
||||
- text: <code>testElseIf(10)</code>应该返回 "Between 5 and 10"。
|
||||
testString: assert(testElseIf(10) === "Between 5 and 10");
|
||||
- text: <code>testElseIf(12)</code>应该返回 "Greater than 10"。
|
||||
testString: assert(testElseIf(12) === "Greater than 10");
|
||||
- text: <code>testElseIf(12)</code> 应该返回 "Greater than 10"。
|
||||
testString: assert(testElseIf(12) === "Greater than 10");
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该至少有两个`else`表达式。
|
||||
|
||||
```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);
|
||||
|
||||
assert(code.match(/else/g).length > 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
你应该至少有两个`if`表达式。
|
||||
|
||||
```js
|
||||
function testElseIf(val) {
|
||||
if(val > 10) {
|
||||
return "Greater than 10";
|
||||
} else if(val < 5) {
|
||||
return "Smaller than 5";
|
||||
} else {
|
||||
return "Between 5 and 10";
|
||||
}
|
||||
}
|
||||
assert(code.match(/if/g).length > 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
`testElseIf(0)`应该返回 "Smaller than 5"。
|
||||
|
||||
```js
|
||||
assert(testElseIf(0) === 'Smaller than 5');
|
||||
```
|
||||
|
||||
`testElseIf(5)`应该返回 "Between 5 and 10"。
|
||||
|
||||
```js
|
||||
assert(testElseIf(5) === 'Between 5 and 10');
|
||||
```
|
||||
|
||||
`testElseIf(7)`应该返回 "Between 5 and 10"。
|
||||
|
||||
```js
|
||||
assert(testElseIf(7) === 'Between 5 and 10');
|
||||
```
|
||||
|
||||
`testElseIf(10)`应该返回 "Between 5 and 10"。
|
||||
|
||||
```js
|
||||
assert(testElseIf(10) === 'Between 5 and 10');
|
||||
```
|
||||
|
||||
`testElseIf(12)`应该返回 "Greater than 10"。
|
||||
|
||||
```js
|
||||
assert(testElseIf(12) === 'Greater than 10');
|
||||
```
|
||||
|
||||
`testElseIf(12)` 应该返回 "Greater than 10"。
|
||||
|
||||
```js
|
||||
assert(testElseIf(12) === 'Greater than 10');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,14 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244da
|
||||
title: 介绍 else 语句
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cek4Efq'
|
||||
forumTopicId: 18207
|
||||
title: 介绍 else 语句
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
当<code>if</code>语句的条件为真,大括号里的代码执行,那如果条件为假呢?正常情况下什么也不会发生。使用else语句,可以执行当条件为假时相应的代码。
|
||||
# --description--
|
||||
|
||||
当`if`语句的条件为真,大括号里的代码执行,那如果条件为假呢?正常情况下什么也不会发生。使用else语句,可以执行当条件为假时相应的代码。
|
||||
|
||||
```js
|
||||
if (num > 10) {
|
||||
@ -18,84 +18,53 @@ if (num > 10) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
请把多个<code>if</code>语句合并为一个<code>if/else</code>语句。
|
||||
</section>
|
||||
请把多个`if`语句合并为一个`if/else`语句。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该只有一个<code>if</code>表达式。
|
||||
testString: assert(code.match(/if/g).length === 1);
|
||||
- text: 你应该使用一个<code>else</code>表达式。
|
||||
testString: assert(/else/g.test(code));
|
||||
- text: <code>testElse(4)</code>应该返回 "5 or Smaller"。
|
||||
testString: assert(testElse(4) === "5 or Smaller");
|
||||
- text: <code>testElse(5)</code>应该返回 "5 or Smaller"。
|
||||
testString: assert(testElse(5) === "5 or Smaller");
|
||||
- text: <code>testElse(6)</code>应该返回 "Bigger than 5"。
|
||||
testString: assert(testElse(6) === "Bigger than 5");
|
||||
- text: <code>testElse(10)</code>应该返回 "Bigger than 5"。
|
||||
testString: assert(testElse(10) === "Bigger than 5");
|
||||
- text: 不要修改上面和下面的代码。
|
||||
testString: assert(/var result = "";/.test(code) && /return result;/.test(code));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该只有一个`if`表达式。
|
||||
|
||||
```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);
|
||||
|
||||
assert(code.match(/if/g).length === 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
你应该使用一个`else`表达式。
|
||||
|
||||
```js
|
||||
function testElse(val) {
|
||||
var result = "";
|
||||
if(val > 5) {
|
||||
result = "Bigger than 5";
|
||||
} else {
|
||||
result = "5 or Smaller";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
assert(/else/g.test(code));
|
||||
```
|
||||
|
||||
</section>
|
||||
`testElse(4)`应该返回 "5 or Smaller"。
|
||||
|
||||
```js
|
||||
assert(testElse(4) === '5 or Smaller');
|
||||
```
|
||||
|
||||
`testElse(5)`应该返回 "5 or Smaller"。
|
||||
|
||||
```js
|
||||
assert(testElse(5) === '5 or Smaller');
|
||||
```
|
||||
|
||||
`testElse(6)`应该返回 "Bigger than 5"。
|
||||
|
||||
```js
|
||||
assert(testElse(6) === 'Bigger than 5');
|
||||
```
|
||||
|
||||
`testElse(10)`应该返回 "Bigger than 5"。
|
||||
|
||||
```js
|
||||
assert(testElse(10) === 'Bigger than 5');
|
||||
```
|
||||
|
||||
不要修改上面和下面的代码。
|
||||
|
||||
```js
|
||||
assert(/var result = "";/.test(code) && /return result;/.test(code));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,16 +1,18 @@
|
||||
---
|
||||
id: 56104e9e514f539506016a5c
|
||||
title: 使用 For 循环遍历数组的奇数
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cm8n7T9'
|
||||
forumTopicId: 18212
|
||||
title: 使用 For 循环遍历数组的奇数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
for循环可以按照我们指定的顺序来迭代,通过更改我们的<code>计数器</code>,我们可以按照偶数顺序来迭代。
|
||||
初始化<code>i = 0</code>,当<code>i < 10</code>的时候继续循环。
|
||||
<code>i += 2</code>让<code>i</code>每次循环之后增加2。
|
||||
# --description--
|
||||
|
||||
for循环可以按照我们指定的顺序来迭代,通过更改我们的`计数器`,我们可以按照偶数顺序来迭代。
|
||||
|
||||
初始化`i = 0`,当`i < 10`的时候继续循环。
|
||||
|
||||
`i += 2`让`i`每次循环之后增加2。
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@ -19,77 +21,25 @@ for (var i = 0; i < 10; i += 2) {
|
||||
}
|
||||
```
|
||||
|
||||
循环结束后,<code>ourArray</code>的值为<code>[0,2,4,6,8]</code>。
|
||||
改变<code>计数器</code>,这样我们可以用奇数来数。
|
||||
</section>
|
||||
循环结束后,`ourArray`的值为`[0,2,4,6,8]`。 改变`计数器`,这样我们可以用奇数来数。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
写一个<code>for</code>循环,把从 1 到 9 的奇数添加到<code>myArray</code>。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
写一个`for`循环,把从 1 到 9 的奇数添加到`myArray`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>for</code>循环。
|
||||
testString: assert(code.match(/for\s*\(/g).length > 1);
|
||||
- text: <code>myArray</code>应该等于<code>[1,3,5,7,9]</code>。
|
||||
testString: assert.deepEqual(myArray, [1,3,5,7,9]);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该使用`for`循环。
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [];
|
||||
|
||||
for (var i = 0; i < 10; i += 2) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
|
||||
assert(code.match(/for\s*\(/g).length > 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`myArray`应该等于`[1,3,5,7,9]`。
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
assert.deepEqual(myArray, [1, 3, 5, 7, 9]);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
for (var i = 0; i < 10; i += 2) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
var myArray = [];
|
||||
for (var i = 1; i < 10; i += 2) {
|
||||
myArray.push(i);
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,14 +1,14 @@
|
||||
---
|
||||
id: 5675e877dbd60be8ad28edc6
|
||||
title: 使用 For 循环遍历数组
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/caeR3HB'
|
||||
forumTopicId: 18216
|
||||
title: 使用 For 循环遍历数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
迭代输出一个数组的每个元素是 JavaScript 中的常见需求,<code>for</code>循环可以做到这一点。下面的代码将输出数组 <code>arr</code>的每个元素到控制台:
|
||||
# --description--
|
||||
|
||||
迭代输出一个数组的每个元素是 JavaScript 中的常见需求,`for`循环可以做到这一点。下面的代码将输出数组 `arr`的每个元素到控制台:
|
||||
|
||||
```js
|
||||
var arr = [10, 9, 8, 7, 6];
|
||||
@ -17,82 +17,37 @@ for (var i = 0; i < arr.length; i++) {
|
||||
}
|
||||
```
|
||||
|
||||
记住数组的索引从零开始的,这意味着数组的最后一个元素的下标是:数组的长度 -1。我们这个循环的 <dfn>条件</dfn>是<code>i < arr.length</code>,当<code>i</code>的值为 长度 -1 的时候循环就停止了。在这个例子中,最后一个循环是 i === 4,也就是说,当i的值等于arr.length时,结果输出 6。
|
||||
</section>
|
||||
记住数组的索引从零开始的,这意味着数组的最后一个元素的下标是:数组的长度 -1。我们这个循环的 <dfn>条件</dfn>是`i < arr.length`,当`i`的值为 长度 -1 的时候循环就停止了。在这个例子中,最后一个循环是 i === 4,也就是说,当i的值等于arr.length时,结果输出 6。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
声明并初始化一个变量<code>total</code>为<code>0</code>。使用<code>for</code>循环,使得<code>total</code>的值为<code>myArr</code>的数组中的每个元素的值的总和。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
声明并初始化一个变量`total`为`0`。使用`for`循环,使得`total`的值为`myArr`的数组中的每个元素的值的总和。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>total</code>应该被声明, 并且初始化值为 0。
|
||||
testString: assert(code.match(/var.*?total\s*=\s*0.*?;/));
|
||||
- text: <code>total</code>应该等于 20。
|
||||
testString: assert(total === 20);
|
||||
- text: 你应该使用<code>for</code>循环在<code>myArr</code>中遍历。
|
||||
testString: assert(code.match(/for\s*\(/g).length > 1 && code.match(/myArr\s*\[/));
|
||||
- text: 不能直接把<code>total</code>设置成 20。
|
||||
testString: assert(!code.match(/total[\s\+\-]*=\s*(\d(?!\s*[;,])|[1-9])/g));
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
`total`应该被声明, 并且初始化值为 0。
|
||||
|
||||
```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
|
||||
|
||||
assert(code.match(/var.*?total\s*=\s*0.*?;/));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`total`应该等于 20。
|
||||
|
||||
```js
|
||||
(function(){if(typeof total !== 'undefined') { return "total = " + total; } else { return "total is undefined";}})()
|
||||
|
||||
assert(total === 20);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你应该使用`for`循环在`myArr`中遍历。
|
||||
|
||||
```js
|
||||
var ourArr = [ 9, 10, 11, 12];
|
||||
var ourTotal = 0;
|
||||
|
||||
for (var i = 0; i < ourArr.length; i++) {
|
||||
ourTotal += ourArr[i];
|
||||
}
|
||||
|
||||
var myArr = [ 2, 3, 4, 5, 6];
|
||||
var total = 0;
|
||||
|
||||
for (var i = 0; i < myArr.length; i++) {
|
||||
total += myArr[i];
|
||||
}
|
||||
assert(code.match(/for\s*\(/g).length > 1 && code.match(/myArr\s*\[/));
|
||||
```
|
||||
|
||||
</section>
|
||||
不能直接把`total`设置成 20。
|
||||
|
||||
```js
|
||||
assert(!code.match(/total[\s\+\-]*=\s*(\d(?!\s*[;,])|[1-9])/g));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,14 @@
|
||||
---
|
||||
id: 5a2efd662fb457916e1fe604
|
||||
title: do...while 循环
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cDqWGcp'
|
||||
forumTopicId: 301172
|
||||
title: do...while 循环
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
这一节我们将要学习的是<code>do...while</code>循环,它会先执行<code>do</code>里面的代码,如果<code>while</code>表达式为真则重复执行,反之则停止执行。我们来看一个例子。
|
||||
# --description--
|
||||
|
||||
这一节我们将要学习的是`do...while`循环,它会先执行`do`里面的代码,如果`while`表达式为真则重复执行,反之则停止执行。我们来看一个例子。
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@ -19,8 +19,7 @@ do {
|
||||
} while (i < 5);
|
||||
```
|
||||
|
||||
这看起来和其他循环语句差不多,返回的结果是<code>[0, 1, 2, 3, 4]</code>,<code>do...while</code>与其他循环不同点在于,初始条件为假时的表现,让我们通过实际的例子来看看。
|
||||
这是一个普通的 while 循环,只要<code>i < 5</code>,它就会在循环中运行代码。
|
||||
这看起来和其他循环语句差不多,返回的结果是`[0, 1, 2, 3, 4]`,`do...while`与其他循环不同点在于,初始条件为假时的表现,让我们通过实际的例子来看看。 这是一个普通的 while 循环,只要`i < 5`,它就会在循环中运行代码。
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@ -31,8 +30,7 @@ while (i < 5) {
|
||||
}
|
||||
```
|
||||
|
||||
注意,我们首先将<code>i</code>的值初始化为 5。执行下一行时,注意到<code>i</code>不小于 5,循环内的代码将不会执行。所以<code>ourArray</code>最终没有添加任何内容,因此示例中的所有代码执行完时,<code>ourArray</code>仍然是<code>[]</code>。
|
||||
现在,看一下<code>do...while</code>循环。
|
||||
注意,我们首先将`i`的值初始化为 5。执行下一行时,注意到`i`不小于 5,循环内的代码将不会执行。所以`ourArray`最终没有添加任何内容,因此示例中的所有代码执行完时,`ourArray`仍然是`[]`。 现在,看一下`do...while`循环。
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@ -43,73 +41,31 @@ do {
|
||||
} while (i < 5);
|
||||
```
|
||||
|
||||
在这里,和使用 while 循环时一样,我们将<code>i</code>的值初始化为 5。执行下一行时,没有检查<code>i</code>的值,直接执行花括号内的代码。数组会添加一个元素,并在进行条件检查之前递增<code>i</code>。然后,在条件检查时因为<code>i</code>等于 6 不符合条件<code>i < 5</code>,所以退出循环。最终<code>ourArray</code>的值是<code>[5]</code>。
|
||||
本质上,<code>do...while</code>循环确保循环内的代码至少运行一次。
|
||||
让我们通过<code>do...while</code>循环将值添加到数组中。
|
||||
</section>
|
||||
在这里,和使用 while 循环时一样,我们将`i`的值初始化为 5。执行下一行时,没有检查`i`的值,直接执行花括号内的代码。数组会添加一个元素,并在进行条件检查之前递增`i`。然后,在条件检查时因为`i`等于 6 不符合条件`i < 5`,所以退出循环。最终`ourArray`的值是`[5]`。 本质上,`do...while`循环确保循环内的代码至少运行一次。 让我们通过`do...while`循环将值添加到数组中。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
将代码中的<code>while</code>循环更改为<code>do...while</code>循环,实现数字 10 添加到<code>myArray</code>中,代码执行完时,<code>i</code>等于<code>11</code>。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
将代码中的`while`循环更改为`do...while`循环,实现数字 10 添加到`myArray`中,代码执行完时,`i`等于`11`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>do...while</code>循环。
|
||||
testString: assert(code.match(/do/g));
|
||||
- text: <code>myArray</code>应该等于<code>[10]</code>。
|
||||
testString: assert.deepEqual(myArray, [10]);
|
||||
- text: <code>i</code>应该等于<code>11</code>。
|
||||
testString: assert.deepEqual(i, 11);
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该使用`do...while`循环。
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [];
|
||||
var i = 10;
|
||||
|
||||
// Only change code below this line
|
||||
while (i < 5) {
|
||||
myArray.push(i);
|
||||
i++;
|
||||
}
|
||||
assert(code.match(/do/g));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`myArray`应该等于`[10]`。
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
assert.deepEqual(myArray, [10]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`i`应该等于`11`。
|
||||
|
||||
```js
|
||||
var myArray = [];
|
||||
var i = 10;
|
||||
do {
|
||||
myArray.push(i);
|
||||
i++;
|
||||
} while (i < 5)
|
||||
assert.deepEqual(i, 11);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,21 +1,28 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb5bdef
|
||||
title: for 循环
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c9yNVCe'
|
||||
forumTopicId: 18219
|
||||
title: for 循环
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
你可以使用循环多次执行相同的代码。
|
||||
JavaScript 中最常见的循环就是 “<code>for循环</code>”。
|
||||
|
||||
JavaScript 中最常见的循环就是 “`for循环`”。
|
||||
|
||||
for循环中的三个表达式用分号隔开:
|
||||
<code>for ([初始化]; [条件判断]; [计数器])</code>
|
||||
<code>初始化</code>语句只会在执行循环开始之前执行一次。它通常用于定义和设置你的循环变量。
|
||||
<code>条件判断</code>语句会在每一轮循环的开始执行,只要条件判断为<code>true</code>就会继续执行循环。当条件为<code>false</code>的时候,循环将停止执行。这意味着,如果条件在一开始就为<code>false</code>,这个循环将不会执行。
|
||||
<code>计数器</code>是在每一轮循环结束时执行,通常用于递增或递减。
|
||||
在下面的例子中,先初始化<code>i = 0</code>,条件<code>i < 5</code>为真,进入第一次循环,执行大括号里的代码,第一次循环结束。递增<code>i</code>的值,条件判断,就这样依次执行下去,直到条件判断为假,整个循环结束。
|
||||
|
||||
`for ([初始化]; [条件判断]; [计数器])`
|
||||
|
||||
`初始化`语句只会在执行循环开始之前执行一次。它通常用于定义和设置你的循环变量。
|
||||
|
||||
`条件判断`语句会在每一轮循环的开始执行,只要条件判断为`true`就会继续执行循环。当条件为`false`的时候,循环将停止执行。这意味着,如果条件在一开始就为`false`,这个循环将不会执行。
|
||||
|
||||
`计数器`是在每一轮循环结束时执行,通常用于递增或递减。
|
||||
|
||||
在下面的例子中,先初始化`i = 0`,条件`i < 5`为真,进入第一次循环,执行大括号里的代码,第一次循环结束。递增`i`的值,条件判断,就这样依次执行下去,直到条件判断为假,整个循环结束。
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@ -24,76 +31,25 @@ for (var i = 0; i < 5; i++) {
|
||||
}
|
||||
```
|
||||
|
||||
最终<code>ourArray</code>的值为<code>[0,1,2,3,4]</code>.
|
||||
</section>
|
||||
最终`ourArray`的值为`[0,1,2,3,4]`.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>for</code>循环把从 1 到 5 添加进<code>myArray</code>中。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
使用`for`循环把从 1 到 5 添加进`myArray`中。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>for</code>循环。
|
||||
testString: assert(code.match(/for\s*\(/g).length > 1);
|
||||
- text: <code>myArray</code>应该等于<code>[1,2,3,4,5]</code>。
|
||||
testString: assert.deepEqual(myArray, [1,2,3,4,5]);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该使用`for`循环。
|
||||
|
||||
```js
|
||||
// Example
|
||||
var ourArray = [];
|
||||
|
||||
for (var i = 0; i < 5; i++) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
|
||||
assert(code.match(/for\s*\(/g).length > 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`myArray`应该等于`[1,2,3,4,5]`。
|
||||
|
||||
```js
|
||||
if (typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
assert.deepEqual(myArray, [1, 2, 3, 4, 5]);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
for (var i = 0; i < 5; i++) {
|
||||
ourArray.push(i);
|
||||
}
|
||||
var myArray = [];
|
||||
for (var i = 1; i < 6; i++) {
|
||||
myArray.push(i);
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,15 +1,16 @@
|
||||
---
|
||||
id: cf1111c1c11feddfaeb1bdef
|
||||
title: while 循环
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/c8QbnCM'
|
||||
forumTopicId: 18220
|
||||
title: while 循环
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
你可以使用循环多次执行相同的代码。
|
||||
我们将学习的第一种类型的循环称为 "<code>while</code>" 循环,因为它规定,当 "while" 条件为真,循环才会执行,反之不执行。
|
||||
|
||||
我们将学习的第一种类型的循环称为 "`while`" 循环,因为它规定,当 "while" 条件为真,循环才会执行,反之不执行。
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@ -20,69 +21,27 @@ while(i < 5) {
|
||||
}
|
||||
```
|
||||
|
||||
在上面的代码里,<code>while</code> 循环执行 5 次把 0 到 4 的数字添加到 <code>ourArray</code> 数组里。
|
||||
在上面的代码里,`while` 循环执行 5 次把 0 到 4 的数字添加到 `ourArray` 数组里。
|
||||
|
||||
让我们通过 while 循环将值添加到数组中。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
通过一个<code>while</code>循环,把从 0 到 4 的值添加到<code>myArray</code>中。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
通过一个`while`循环,把从 0 到 4 的值添加到`myArray`中。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>while</code>循环。
|
||||
testString: assert(code.match(/while/g));
|
||||
- text: <code>myArray</code>应该等于<code>[0,1,2,3,4]</code>。
|
||||
testString: assert.deepEqual(myArray, [0,1,2,3,4]);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该使用`while`循环。
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line.
|
||||
|
||||
|
||||
assert(code.match(/while/g));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`myArray`应该等于`[0,1,2,3,4]`。
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
assert.deepEqual(myArray, [0, 1, 2, 3, 4]);
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var myArray = [];
|
||||
var i = 5;
|
||||
while(i >= 0) {
|
||||
myArray.push(i);
|
||||
i--;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,15 +1,16 @@
|
||||
---
|
||||
id: 56533eb9ac21ba0edf2244bf
|
||||
title: 局部作用域和函数
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cd62NhM'
|
||||
forumTopicId: 18227
|
||||
title: 局部作用域和函数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
在一个函数内声明的变量,以及该函数的参数都是局部变量,意味着它们只在该函数内可见。
|
||||
这是在函数<code>myTest</code>内声明局部变量<code>loc</code>的例子:
|
||||
|
||||
这是在函数`myTest`内声明局部变量`loc`的例子:
|
||||
|
||||
```js
|
||||
function myTest() {
|
||||
@ -20,104 +21,28 @@ myTest(); // logs "foo"
|
||||
console.log(loc); // loc is not defined
|
||||
```
|
||||
|
||||
在函数外,<code>loc</code>是未定义的。
|
||||
</section>
|
||||
在函数外,`loc`是未定义的。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
在函数<code>myFunction</code>内部声明一个局部变量<code>myVar</code>,并删除外部的 console.log。
|
||||
<strong>提示:</strong><br>如果你遇到了问题,可以先尝试刷新页面。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
在函数`myFunction`内部声明一个局部变量`myVar`,并删除外部的 console.log。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 未找到全局的<code>myVar</code>变量。
|
||||
testString: assert(typeof myVar === 'undefined');
|
||||
- text: 需要定义局部的<code>myVar</code>变量。
|
||||
testString: assert(/var\s+myVar/.test(code));
|
||||
**提示:**
|
||||
如果你遇到了问题,可以先尝试刷新页面。
|
||||
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
未找到全局的`myVar`变量。
|
||||
|
||||
```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
|
||||
|
||||
assert(typeof myVar === 'undefined');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
需要定义局部的`myVar`变量。
|
||||
|
||||
```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;
|
||||
}
|
||||
|
||||
assert(/var\s+myVar/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
typeof myLocalScope === 'function' && (capture(), myLocalScope(), uncapture());
|
||||
(function() { return logOutput || "console.log never called"; })();
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
function myLocalScope() {
|
||||
'use strict';
|
||||
|
||||
var myVar;
|
||||
console.log(myVar);
|
||||
}
|
||||
myLocalScope();
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,16 +1,19 @@
|
||||
---
|
||||
id: 5690307fddb111c6084545d7
|
||||
title: if else 语句中的逻辑顺序
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cwNvMUV'
|
||||
forumTopicId: 18228
|
||||
title: if else 语句中的逻辑顺序
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<code>if</code>、<code>else if</code>语句中代码的执行顺序是很重要的。
|
||||
# --description--
|
||||
|
||||
`if`、`else if`语句中代码的执行顺序是很重要的。
|
||||
|
||||
在条件判断语句中,代码的执行顺序是从上到下,所以你需要考虑清楚先执行哪一句,后执行哪一句。
|
||||
|
||||
这有两个例子。
|
||||
|
||||
第一个例子:
|
||||
|
||||
```js
|
||||
@ -46,69 +49,29 @@ foo(0) // "Less than one"
|
||||
bar(0) // "Less than two"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
更改函数的逻辑顺序以便通过所有的测试用例。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>orderMyLogic(4)</code>应该返回 "Less than 5"。
|
||||
testString: assert(orderMyLogic(4) === "Less than 5");
|
||||
- text: <code>orderMyLogic(6)</code>应该返回 "Less than 10"。
|
||||
testString: assert(orderMyLogic(6) === "Less than 10");
|
||||
- text: <code>orderMyLogic(11)</code>应该返回 "Greater than or equal to 10"。
|
||||
testString: assert(orderMyLogic(11) === "Greater than or equal to 10");
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`orderMyLogic(4)`应该返回 "Less than 5"。
|
||||
|
||||
```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);
|
||||
assert(orderMyLogic(4) === 'Less than 5');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`orderMyLogic(6)`应该返回 "Less than 10"。
|
||||
|
||||
```js
|
||||
function orderMyLogic(val) {
|
||||
if(val < 5) {
|
||||
return "Less than 5";
|
||||
} else if (val < 10) {
|
||||
return "Less than 10";
|
||||
} else {
|
||||
return "Greater than or equal to 10";
|
||||
}
|
||||
}
|
||||
assert(orderMyLogic(6) === 'Less than 10');
|
||||
```
|
||||
|
||||
</section>
|
||||
`orderMyLogic(11)`应该返回 "Greater than or equal to 10"。
|
||||
|
||||
```js
|
||||
assert(orderMyLogic(11) === 'Greater than or equal to 10');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,17 @@
|
||||
---
|
||||
id: 56bbb991ad1ed5201cd392cc
|
||||
title: 使用 pop() 操作数组
|
||||
challengeType: 1
|
||||
videoUrl: 'https://scrimba.com/c/cRbVZAB'
|
||||
forumTopicId: 18236
|
||||
title: 使用 pop() 操作数组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
改变数组中数据的另一种方法是用<code>.pop()</code>函数。
|
||||
<code>.pop()</code>函数用来“抛出”一个数组末尾的值。我们可以把这个“抛出”的值赋给一个变量存储起来。换句话说就是<code>.pop()</code>函数移除数组末尾的元素并返回这个元素。
|
||||
# --description--
|
||||
|
||||
改变数组中数据的另一种方法是用`.pop()`函数。
|
||||
|
||||
`.pop()`函数用来“抛出”一个数组末尾的值。我们可以把这个“抛出”的值赋给一个变量存储起来。换句话说就是`.pop()`函数移除数组末尾的元素并返回这个元素。
|
||||
|
||||
数组中任何类型的元素(数值,字符串,甚至是数组)可以被“抛出来” 。
|
||||
|
||||
```js
|
||||
@ -19,70 +21,45 @@ console.log(oneDown); // Returns 6
|
||||
console.log(threeArr); // Returns [1, 4]
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>.pop()</code>函数移除<code>myArray</code>中的最后一个元素,并且把“抛出”的值赋给<code>removedFromMyArray</code>。
|
||||
</section>
|
||||
使用`.pop()`函数移除`myArray`中的最后一个元素,并且把“抛出”的值赋给`removedFromMyArray`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```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));
|
||||
- text: 对<code>myArray</code>使用<code>pop()</code>函数。
|
||||
testString: assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(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));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myArray`应该只包含`[["John", 23]]`。
|
||||
|
||||
```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;
|
||||
|
||||
|
||||
assert(
|
||||
(function (d) {
|
||||
if (d[0][0] == 'John' && d[0][1] === 23 && d[1] == undefined) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myArray)
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
对`myArray`使用`pop()`函数。
|
||||
|
||||
```js
|
||||
(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
|
||||
assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
`removedFromMyArray`应该只包含`["cat", 2]`。
|
||||
|
||||
```js
|
||||
var myArray = [["John", 23], ["cat", 2]];
|
||||
var removedFromMyArray = myArray.pop();
|
||||
assert(
|
||||
(function (d) {
|
||||
if (d[0] == 'cat' && d[1] === 2 && d[2] == undefined) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(removedFromMyArray)
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user