chore(learn): Applied MDX format to Chinese curriculum files (#40462)
This commit is contained in:
@ -1,68 +1,31 @@
|
||||
---
|
||||
id: 587d7b85367417b2b2512b3a
|
||||
title: 调用函数时,捕获以错误顺序传递的参数
|
||||
challengeType: 1
|
||||
forumTopicId: 301184
|
||||
title: 调用函数时,捕获以错误顺序传递的参数
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
继续讨论调用函数,需要注意的下一个 bug 是函数的参数传递顺序错误。 如果参数分别是不同的类型,例如接受数组和整数两个参数的函数,参数顺序传错就可能会引发运行时错误。对于接受相同类型参数的函数,传错参数也会导致逻辑错误或运行结果错误。确保以正确的顺序提供所有必需的参数以避免这些问题。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
函数<code>raiseToPower</code>返回基数 (base) 的指数 (exponent) 次幂。不幸的是,它没有被正确调用 ———— 修改代码,使<code>power</code>的值为 8。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
函数`raiseToPower`返回基数 (base) 的指数 (exponent) 次幂。不幸的是,它没有被正确调用 ———— 修改代码,使`power`的值为 8。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应修复变量<code>power</code>,使其等于 2 的 3 次方,而不是 3 的 2 次方。
|
||||
testString: assert(power == 8);
|
||||
- text: 你调用<code>raiseToPower</code>函数时,传递参数的顺序应正确。
|
||||
testString: assert(code.match(/raiseToPower\(\s*?base\s*?,\s*?exp\s*?\);/g));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应修复变量`power`,使其等于 2 的 3 次方,而不是 3 的 2 次方。
|
||||
|
||||
```js
|
||||
function raiseToPower(b, e) {
|
||||
return Math.pow(b, e);
|
||||
}
|
||||
|
||||
let base = 2;
|
||||
let exp = 3;
|
||||
let power = raiseToPower(exp, base);
|
||||
console.log(power);
|
||||
assert(power == 8);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你调用`raiseToPower`函数时,传递参数的顺序应正确。
|
||||
|
||||
```js
|
||||
function raiseToPower(b, e) {
|
||||
return Math.pow(b, e);
|
||||
}
|
||||
|
||||
let base = 2;
|
||||
let exp = 3;
|
||||
let power = raiseToPower(base, exp);
|
||||
console.log(power);
|
||||
assert(code.match(/raiseToPower\(\s*?base\s*?,\s*?exp\s*?\);/g));
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
---
|
||||
id: 587d7b85367417b2b2512b39
|
||||
title: 捕捉函数调用后缺少的左括号和右括号
|
||||
challengeType: 1
|
||||
forumTopicId: 301185
|
||||
title: 捕捉函数调用后缺少的左括号和右括号
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
当函数或方法不接受任何参数时,你可能忘记在调用它时加上空的左括号和右括号。通常,函数调用的结果会保存在变量中,供其他代码使用。可以通过将变量值(或其类型)打印到控制台,查看输出究竟是一个函数引用还是函数调用的返回值来检测这类错误。
|
||||
|
||||
下面示例中的两个变量是不同的:
|
||||
|
||||
```js
|
||||
@ -18,62 +19,23 @@ let varOne = myFunction; // set to equal a function
|
||||
let varTwo = myFunction(); // set to equal the string "You rock!"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修复代码,把调用函数<code>getNine</code>的返回值赋给变量<code>result</code>。
|
||||
</section>
|
||||
修复代码,把调用函数`getNine`的返回值赋给变量`result`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该修复变量<code>result</code>使其为函数<code>getNine</code>的返回值。
|
||||
testString: assert(result == 9);
|
||||
- text: 你应该调用<code>getNine</code>函数。
|
||||
testString: assert(code.match(/getNine\(\)/g).length == 2);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该修复变量`result`使其为函数`getNine`的返回值。
|
||||
|
||||
```js
|
||||
function getNine() {
|
||||
let x = 6;
|
||||
let y = 3;
|
||||
return x + y;
|
||||
}
|
||||
|
||||
let result = getNine;
|
||||
console.log(result);
|
||||
assert(result == 9);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你应该调用`getNine`函数。
|
||||
|
||||
```js
|
||||
function getNine() {
|
||||
let x = 6;
|
||||
let y = 3;
|
||||
return x + y;
|
||||
}
|
||||
|
||||
let result = getNine();
|
||||
console.log(result);
|
||||
assert(code.match(/getNine\(\)/g).length == 2);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,67 +1,51 @@
|
||||
---
|
||||
id: 587d7b84367417b2b2512b35
|
||||
title: 捕获拼错的变量名和函数名
|
||||
challengeType: 1
|
||||
forumTopicId: 301186
|
||||
title: 捕获拼错的变量名和函数名
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<code>console.log()</code>和<code>typeof</code>方法是检查中间值和程序输出类型的两种主要方法。 现在是时候了解一下 bug 出现的常见的情形。一个语法级别的问题是打字太快带来的低级拼写错误。
|
||||
# --description--
|
||||
|
||||
`console.log()`和`typeof`方法是检查中间值和程序输出类型的两种主要方法。 现在是时候了解一下 bug 出现的常见的情形。一个语法级别的问题是打字太快带来的低级拼写错误。
|
||||
|
||||
变量或函数名的错写、漏写或大小写弄混都会让浏览器尝试查找并不存在的东西,并报出“引用错误”。JavaScript 变量和函数名称区分大小写。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修复代码中的两个拼写错误,以便<code>netWorkingCapital</code>计算有效。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
修复代码中的两个拼写错误,以便`netWorkingCapital`计算有效。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '检查计算 netWorkingCapital 值时使用的两个变量的拼写是否正确,控制台应该输出 "Net working capital is: 2"。'
|
||||
testString: 'assert(netWorkingCapital === 2);'
|
||||
- text: 代码中不应存在拼写错误的变量。
|
||||
testString: assert(!code.match(/recievables/g));
|
||||
- text: 应在代码中声明并正确使用<code>receivables</code>变量。
|
||||
testString: assert(code.match(/receivables/g).length == 2);
|
||||
- text: 代码中不应存在拼写错误的变量。
|
||||
testString: assert(!code.match(/payable;/g));
|
||||
- text: 应在代码中声明并正确使用<code>payables</code>变量。
|
||||
testString: assert(code.match(/payables/g).length == 2);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
检查计算 netWorkingCapital 值时使用的两个变量的拼写是否正确,控制台应该输出 "Net working capital is: 2"。
|
||||
|
||||
```js
|
||||
let receivables = 10;
|
||||
let payables = 8;
|
||||
let netWorkingCapital = recievables - payable;
|
||||
console.log(`Net working capital is: ${netWorkingCapital}`);
|
||||
assert(netWorkingCapital === 2);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
代码中不应存在拼写错误的变量。
|
||||
|
||||
```js
|
||||
let receivables = 10;
|
||||
let payables = 8;
|
||||
let netWorkingCapital = receivables - payables;
|
||||
console.log(`Net working capital is: ${netWorkingCapital}`);
|
||||
assert(!code.match(/recievables/g));
|
||||
```
|
||||
|
||||
</section>
|
||||
应在代码中声明并正确使用`receivables`变量。
|
||||
|
||||
```js
|
||||
assert(code.match(/receivables/g).length == 2);
|
||||
```
|
||||
|
||||
代码中不应存在拼写错误的变量。
|
||||
|
||||
```js
|
||||
assert(!code.match(/payable;/g));
|
||||
```
|
||||
|
||||
应在代码中声明并正确使用`payables`变量。
|
||||
|
||||
```js
|
||||
assert(code.match(/payables/g).length == 2);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,16 @@
|
||||
---
|
||||
id: 587d7b84367417b2b2512b37
|
||||
title: 捕捉单引号和双引号的混合用法
|
||||
challengeType: 1
|
||||
forumTopicId: 301188
|
||||
title: 捕捉单引号和双引号的混合用法
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
JavaScript允许使用单引号('')和双引号("")声明字符串。决定使用哪一个通常看个人偏好,但有一些例外。
|
||||
# --description--
|
||||
|
||||
JavaScript允许使用单引号('')和双引号("")声明字符串。决定使用哪一个通常看个人偏好,但有一些例外。
|
||||
|
||||
如果字符串中有缩写或存在一段带引号的文本,你就会明白为什么 JavaScript 允许两种引号了。请注意,不要提前用引号结束字符串,这会导致语法错误。
|
||||
|
||||
下面是混合使用引号的一些示例:
|
||||
|
||||
```js
|
||||
@ -19,56 +21,30 @@ const quoteInString = "Groucho Marx once said 'Quote me as saying I was mis-quot
|
||||
const uhOhGroucho = 'I've had a perfectly wonderful evening, but this wasn't it.';
|
||||
```
|
||||
|
||||
当然,只使用一种引号是可以的。你可以使用反斜杠 (<code>\\</code>) 转义字符来转义字符串中的引号:
|
||||
当然,只使用一种引号是可以的。你可以使用反斜杠 (`\`) 转义字符来转义字符串中的引号:
|
||||
|
||||
```js
|
||||
// 一种引号的正确使用方式
|
||||
const allSameQuotes = 'I\'ve had a perfectly wonderful evening, but this wasn\'t it.';
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修复字符串,对<code>href</code>属性的值使用不同的引号,或者转义现有的引号。注意,整个字符串外面的双引号要保留。
|
||||
</section>
|
||||
修复字符串,对`href`属性的值使用不同的引号,或者转义现有的引号。注意,整个字符串外面的双引号要保留。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: "你应通过更改或转义来修复<code>href</code>的值 '#Home' 周围的引号。"
|
||||
testString: assert(code.match(/<a href=\s*?('|\\")#Home\1\s*?>/g));
|
||||
- text: 你应该在整个字符串外围保留双引号。
|
||||
testString: assert(code.match(/"<p>.*?<\/p>";/g));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应通过更改或转义来修复`href`的值 '#Home' 周围的引号。
|
||||
|
||||
```js
|
||||
let innerHtml = "<p>Click here to <a href="#Home">return home</a></p>";
|
||||
console.log(innerHtml);
|
||||
assert(code.match(/<a href=\s*?('|\\")#Home\1\s*?>/g));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你应该在整个字符串外围保留双引号。
|
||||
|
||||
```js
|
||||
let innerHtml = "<p>Click here to <a href=\"#Home\">return home</a></p>";
|
||||
console.log(innerHtml);
|
||||
assert(code.match(/"<p>.*?<\/p>";/g));
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
---
|
||||
id: 587d7b86367417b2b2512b3b
|
||||
title: 捕获使用索引的时候出现的错误
|
||||
challengeType: 1
|
||||
forumTopicId: 301189
|
||||
title: 捕获使用索引的时候出现的错误
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
当试图访问字符串或数组的特定索引(分割或访问一个片段)或循环索引时,有时会出现<code>Off by one errors</code>错误(有时称为 OBOE)。JavaScript 索引从<code>0</code>开始,而不是<code>1</code>,这意味着最后一个索引总会比字符串或数组的长度少 1。如果尝试访问等于长度的索引,程序可能会抛出“索引超出范围”引用错误或打印出<code>undefined</code>。
|
||||
# --description--
|
||||
|
||||
当试图访问字符串或数组的特定索引(分割或访问一个片段)或循环索引时,有时会出现`Off by one errors`错误(有时称为 OBOE)。JavaScript 索引从`0`开始,而不是`1`,这意味着最后一个索引总会比字符串或数组的长度少 1。如果尝试访问等于长度的索引,程序可能会抛出“索引超出范围”引用错误或打印出`undefined`。
|
||||
|
||||
当使用将索引范围作为参数的字符串或数组方法时,阅读相关的文档并了解参数中的索引的包含性(即是否考虑进返回值中)很重要。以下是一些错误的示例:
|
||||
|
||||
```js
|
||||
@ -27,72 +28,35 @@ for (let k = 0; k < len; k++) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修复以下函数中的两个索引错误,以便将 1 到 5 之间(包含 1 和 5)的所有数字打印到控制台。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该设置循环的初始条件,使循环从第一个索引开始。
|
||||
testString: assert(code.match(/i\s*?=\s*?0\s*?;/g).length == 1);
|
||||
- text: 你应修复循环的初始条件,使循环从索引 0 开始。
|
||||
testString: assert(!code.match(/i\s?=\s*?1\s*?;/g));
|
||||
- text: 你应设置循环的终止条件,使循环在最后一个索引处停止。
|
||||
testString: assert(code.match(/i\s*?<\s*?len\s*?;/g).length == 1);
|
||||
- text: 你应修复循环的终止条件,使循环在索引为字符串长度减 1 时停止。
|
||||
testString: assert(!code.match(/i\s*?<=\s*?len;/g));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该设置循环的初始条件,使循环从第一个索引开始。
|
||||
|
||||
```js
|
||||
function countToFive() {
|
||||
let firstFive = "12345";
|
||||
let len = firstFive.length;
|
||||
// Fix the line below
|
||||
for (let i = 1; i <= len; i++) {
|
||||
// Do not alter code below this line
|
||||
console.log(firstFive[i]);
|
||||
}
|
||||
}
|
||||
|
||||
countToFive();
|
||||
assert(code.match(/i\s*?=\s*?0\s*?;/g).length == 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你应修复循环的初始条件,使循环从索引 0 开始。
|
||||
|
||||
```js
|
||||
function countToFive() {
|
||||
let firstFive = "12345";
|
||||
let len = firstFive.length;
|
||||
// Fix the line below
|
||||
for (let i = 0; i < len; i++) {
|
||||
// Do not alter code below this line
|
||||
console.log(firstFive[i]);
|
||||
}
|
||||
}
|
||||
|
||||
countToFive();
|
||||
assert(!code.match(/i\s?=\s*?1\s*?;/g));
|
||||
```
|
||||
|
||||
你应设置循环的终止条件,使循环在最后一个索引处停止。
|
||||
|
||||
```js
|
||||
assert(code.match(/i\s*?<\s*?len\s*?;/g).length == 1);
|
||||
```
|
||||
|
||||
你应修复循环的终止条件,使循环在索引为字符串长度减 1 时停止。
|
||||
|
||||
```js
|
||||
assert(!code.match(/i\s*?<=\s*?len;/g));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
</section>
|
||||
|
@ -1,59 +1,33 @@
|
||||
---
|
||||
id: 587d7b84367417b2b2512b36
|
||||
title: 捕获未闭合的括号、方括号、大括号和引号
|
||||
challengeType: 1
|
||||
forumTopicId: 301190
|
||||
title: 捕获未闭合的括号、方括号、大括号和引号
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
要注意的另一个语法错误是所有的小括号、方括号、花括号和引号都必须配对。当你编辑代码并插入新代码其中带有括号时,很容易忘记括号闭合。 此外,在将代码块嵌套到其他代码块时要小心,例如将回调函数作为参数添加到方法中。
|
||||
|
||||
避免这种错误的一种方法是,一次性输入完这些符号,然后将光标移回它们之间继续编写。好在,现在大部分编辑器都会帮你自动补全。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
# --instructions--
|
||||
|
||||
修复代码中的两个符号配对错误。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该修复数组缺少的部分。
|
||||
testString: assert(code.match(/myArray\s*?=\s*?\[\s*?1\s*?,\s*?2\s*?,\s*?3\s*?\];/g));
|
||||
- text: '你应该修复<code>.reduce()</code>方法缺少的部分。控制台应输出 "Sum of array values is: 6"。'
|
||||
testString: 'assert(arraySum === 6);'
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该修复数组缺少的部分。
|
||||
|
||||
```js
|
||||
let myArray = [1, 2, 3;
|
||||
let arraySum = myArray.reduce((previous, current => previous + current);
|
||||
console.log(`Sum of array values is: ${arraySum}`);
|
||||
assert(code.match(/myArray\s*?=\s*?\[\s*?1\s*?,\s*?2\s*?,\s*?3\s*?\];/g));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你应该修复`.reduce()`方法缺少的部分。控制台应输出 "Sum of array values is: 6"。
|
||||
|
||||
```js
|
||||
let myArray = [1, 2, 3];
|
||||
let arraySum = myArray.reduce((previous, current) => previous + current);
|
||||
console.log(`Sum of array values is: ${arraySum}`);
|
||||
assert(arraySum === 6);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,16 +1,19 @@
|
||||
---
|
||||
id: 587d7b85367417b2b2512b38
|
||||
title: 捕获使用赋值运算符而不是相等运算符
|
||||
challengeType: 1
|
||||
forumTopicId: 301191
|
||||
title: 捕获使用赋值运算符而不是相等运算符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
分支程序,即在满足某些条件时执行不同操作的程序,依赖于 JavaScript 中的<code>if</code>,<code>else if</code>、<code>else</code>语句。条件有时采取测试一个结果是否等于一个值的形式。
|
||||
这种逻辑可以表述为“如果 x 等于 y ,则......”,听起来像是可以使用<code>=</code>(即赋值运算符)。然而,这会导致程序中流程出问题。
|
||||
如前面的挑战所述,JavaScript 中的赋值运算符 (<code>=</code>) 是用来为变量名赋值的。并且<code>==</code>和<code>===</code>运算符检查相等性(三等号<code>===</code>是用来测试是否严格相等的,严格相等的意思是值和类型都必须相同)。
|
||||
下面的代码将<code>x</code>赋值为 2,表达式<code>x = y</code>会在执行后得到<code>true</code>。JavaScript 会把大部分的值都视为<code>true</code>,除了所谓的 "falsy" 值,即:<code>false</code>、<code>0</code>、<code>""</code>(空字符串)、<code>NaN</code>、<code>undefined</code> 和 <code>null</code>。
|
||||
# --description--
|
||||
|
||||
分支程序,即在满足某些条件时执行不同操作的程序,依赖于 JavaScript 中的`if`,`else if`、`else`语句。条件有时采取测试一个结果是否等于一个值的形式。
|
||||
|
||||
这种逻辑可以表述为“如果 x 等于 y ,则......”,听起来像是可以使用`=`(即赋值运算符)。然而,这会导致程序中流程出问题。
|
||||
|
||||
如前面的挑战所述,JavaScript 中的赋值运算符 (`=`) 是用来为变量名赋值的。并且`==`和`===`运算符检查相等性(三等号`===`是用来测试是否严格相等的,严格相等的意思是值和类型都必须相同)。
|
||||
|
||||
下面的代码将`x`赋值为 2,表达式`x = y`会在执行后得到`true`。JavaScript 会把大部分的值都视为`true`,除了所谓的 "falsy" 值,即:`false`、`0`、`""`(空字符串)、`NaN`、`undefined` 和 `null`。
|
||||
|
||||
```js
|
||||
let x = 1;
|
||||
@ -21,67 +24,24 @@ if (x = y) {
|
||||
// 按本例用意这个代码块应该执行(但其实不会)。
|
||||
}
|
||||
```
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修复条件语句,以便程序运行正确的分支,并给<code>result</code>赋上正确的值。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
修复条件语句,以便程序运行正确的分支,并给`result`赋上正确的值。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该修复条件语句,使其判断是否相等,而不是赋值。
|
||||
testString: assert(result == "Not equal!");
|
||||
- text: 条件语句可以使用<code>==</code>或<code>===</code>来测试是否相等。
|
||||
testString: assert(code.match(/x\s*?===?\s*?y/g));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该修复条件语句,使其判断是否相等,而不是赋值。
|
||||
|
||||
```js
|
||||
let x = 7;
|
||||
let y = 9;
|
||||
let result = "to come";
|
||||
|
||||
if(x = y) {
|
||||
result = "Equal!";
|
||||
} else {
|
||||
result = "Not equal!";
|
||||
}
|
||||
|
||||
console.log(result);
|
||||
assert(result == 'Not equal!');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
条件语句可以使用`==`或`===`来测试是否相等。
|
||||
|
||||
```js
|
||||
let x = 7;
|
||||
let y = 9;
|
||||
let result = "to come";
|
||||
|
||||
if(x === y) {
|
||||
result = "Equal!";
|
||||
} else {
|
||||
result = "Not equal!";
|
||||
}
|
||||
|
||||
console.log(result);
|
||||
assert(code.match(/x\s*?===?\s*?y/g));
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d7b86367417b2b2512b3d
|
||||
title: 使用有效的终止条件防止无限循环
|
||||
challengeType: 1
|
||||
forumTopicId: 301192
|
||||
title: 使用有效的终止条件防止无限循环
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
最后一个话题是可怕的无限循环。当需要程序运行代码块一定次数或满足条件时,循环是很好的工具,但是它们需要终止条件来结束循环。无限循环可能会使浏览器冻结或崩溃,并导致程序执行混乱,没有人想要这样的结果。
|
||||
在本节的介绍中有一个无限循环的例子——它没有终止条件来摆脱<code>loopy()</code>内的<code>while</code>循环。不要调用这个函数!
|
||||
|
||||
在本节的介绍中有一个无限循环的例子——它没有终止条件来摆脱`loopy()`内的`while`循环。不要调用这个函数!
|
||||
|
||||
```js
|
||||
function loopy() {
|
||||
@ -19,55 +20,24 @@ function loopy() {
|
||||
```
|
||||
|
||||
程序员的工作是确保最终达到终止条件,该条件告诉程序何时跳出循环。有一种错误是从终端条件向错误方向递增或递减计数器变量。另一种是在循环代码中意外重置计数器或索引变量,而不是递增或递减它。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
<code>myFunc()</code>函数包含一个无限循环,因为终止条件<code>i != 4</code>永远不会为<code>false</code>(并中断循环) -<code>i</code>将每次递增 2,然后跳过 4,因为<code>i</code>是从奇数开始递增。在终端条件中输入比较运算符,使循环仅在<code>i</code>小于或等于 4 的情况下运行。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`myFunc()`函数包含一个无限循环,因为终止条件`i != 4`永远不会为`false`(并中断循环) -`i`将每次递增 2,然后跳过 4,因为`i`是从奇数开始递增。在终端条件中输入比较运算符,使循环仅在`i`小于或等于 4 的情况下运行。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该在<code>for</code>循环的终止条件(中间部分)中更改比较运算符。
|
||||
testString: assert(code.match(/i\s*?<=\s*?4;/g).length == 1);
|
||||
- text: 你应该修改比较运算符来避免出现死循环。
|
||||
testString: assert(!code.match(/i\s*?!=\s*?4;/g));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该在`for`循环的终止条件(中间部分)中更改比较运算符。
|
||||
|
||||
```js
|
||||
function myFunc() {
|
||||
for (let i = 1; i != 4; i += 2) {
|
||||
console.log("Still going!");
|
||||
}
|
||||
}
|
||||
assert(code.match(/i\s*?<=\s*?4;/g).length == 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你应该修改比较运算符来避免出现死循环。
|
||||
|
||||
```js
|
||||
function myFunc() {
|
||||
for (let i = 1; i <= 4; i += 2) {
|
||||
console.log("Still going!");
|
||||
}
|
||||
}
|
||||
assert(!code.match(/i\s*?!=\s*?4;/g));
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,77 +1,44 @@
|
||||
---
|
||||
id: 587d7b83367417b2b2512b37
|
||||
title: 了解 freeCodeCamp 和浏览器控制台之间的差异
|
||||
challengeType: 1
|
||||
forumTopicId: 301193
|
||||
title: 了解 freeCodeCamp 和浏览器控制台之间的差异
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
你可能已经注意到一些 freeCodeCamp JavaScript 的挑战有自己的控制台。这些控制台的行为与上一次挑战中使用的浏览器控制台略有不同。
|
||||
|
||||
以下挑战旨在强调 freeCodeCamp 控制台与浏览器控制台之间的一些差异。
|
||||
对于浏览器控制台。当在浏览器中加载并运行 JavaScript 文件时,<code>console.log()</code>语句会在控制台中按照调用的次数准确地打印出要求的内容。然而,在 freeCodeCamp 的代码编辑器中使用<code>console.log()</code>会略有不同,一开始可能会让你感到困惑。
|
||||
在 freeCodeCamp 代码编辑器中,传给<code>console.log()</code>的值会在每组测试执行的时候输出。另外,如果在代码中还手动调用过挑战题目的函数,调用几次就会增加几次传入值的输出。
|
||||
|
||||
对于浏览器控制台。当在浏览器中加载并运行 JavaScript 文件时,`console.log()`语句会在控制台中按照调用的次数准确地打印出要求的内容。然而,在 freeCodeCamp 的代码编辑器中使用`console.log()`会略有不同,一开始可能会让你感到困惑。
|
||||
|
||||
在 freeCodeCamp 代码编辑器中,传给`console.log()`的值会在每组测试执行的时候输出。另外,如果在代码中还手动调用过挑战题目的函数,调用几次就会增加几次传入值的输出。
|
||||
|
||||
这就产生了一些有趣的行为,并可能在一开始就让你感到困惑,因为你觉得只会输出一次的值可能会输出多次,具体次数取决于挑战题目本身测试的数量以及这些测试调用挑战函数的方式。
|
||||
如果你不打算执行挑战的测试,而只想查看自己调用<code>console.log()</code>的输出,可以使用<code>console.clear()</code>。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
首先,使用 <code>console.clear()</code> 清空浏览器控制台。然后使用<code>console.log()</code>在代码中指定的位置打印 <code>output</code> 变量。
|
||||
</section>
|
||||
如果你不打算执行挑战的测试,而只想查看自己调用`console.log()`的输出,可以使用`console.clear()`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 应该使用 <code>console.clear()</code> 来清空浏览器控制台。
|
||||
testString: const removeJSComments = code.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); const noSpaces = removeJSComments.replace(/\s/g, ''); assert(noSpaces.match(/console.clear\(\)/));
|
||||
- text: 使用<code>console.log()</code>输出变量<code>output</code>的值。
|
||||
testString: const noSpaces = code.replace(/\s/g, ''); assert(noSpaces.match(/console\.log\(output\)/));
|
||||
首先,使用 `console.clear()` 清空浏览器控制台。然后使用`console.log()`在代码中指定的位置打印 `output` 变量。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
应该使用 `console.clear()` 来清空浏览器控制台。
|
||||
|
||||
```js
|
||||
// Open your browser console.
|
||||
let output = "Get this to log once in the browser console and twice in the freeCodeCamp console";
|
||||
// Use console.clear() on the next line to clear the browser console.
|
||||
|
||||
|
||||
// Use console.log() to print the output variable.
|
||||
|
||||
|
||||
// Check the two consoles to see the difference. The freeCodeCamp console should have printed the variable twice, once for each test of this challenge. The browser console should only print the variable once because you cleared it first.
|
||||
const removeJSComments = code.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, '');
|
||||
const noSpaces = removeJSComments.replace(/\s/g, '');
|
||||
assert(noSpaces.match(/console.clear\(\)/));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
使用`console.log()`输出变量`output`的值。
|
||||
|
||||
```js
|
||||
// Open your browser console.
|
||||
let output = "Get this to log once in the browser console and twice in the freeCodeCamp console";
|
||||
// Use console.clear() on the next line to clear the browser console.
|
||||
console.clear();
|
||||
|
||||
// Use console.log() to print the output variable.
|
||||
console.log(output);
|
||||
|
||||
// Check the two consoles to see the difference. The freeCodeCamp console should have printed the variable twice, one for each test of this challenge. The browser console should only print the variable once becuase you cleared it first.
|
||||
const noSpaces = code.replace(/\s/g, '');
|
||||
assert(noSpaces.match(/console\.log\(output\)/));
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,93 +1,41 @@
|
||||
---
|
||||
id: 587d7b86367417b2b2512b3c
|
||||
title: 重新初始化循环中的变量时要小心
|
||||
challengeType: 1
|
||||
forumTopicId: 301194
|
||||
title: 重新初始化循环中的变量时要小心
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
有时需要在循环中保存信息以增加计数器或重置变量。一个潜在的问题是变量什么时候该重新初始化,什么时候不该重新初始化,反之亦然。如果你不小心重置了用于终止条件的变量,导致无限循环,这将特别危险。
|
||||
使用<code>console.log()</code>在每个循环中打印变量值可以发现与重置相关的错误或者重置变量失败。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
以下函数应该创建一个具有<code>m</code>行和<code>n</code>列“零”的二维数组。不幸的是,它没有产生预期的输出,因为<code>row</code>变量没有在外部循环中重新初始化(设置回空数组)。修改代码,使其正确地返回包含 3 行 2 列“零”的二维数组,即<code>[[0, 0], [0, 0], [0, 0]]</code>。
|
||||
</section>
|
||||
使用`console.log()`在每个循环中打印变量值可以发现与重置相关的错误或者重置变量失败。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应将变量<code>matrix</code>设置为 3 行 2 列“零”的二维数组。
|
||||
testString: assert(JSON.stringify(matrix) == "[[0,0],[0,0],[0,0]]");
|
||||
- text: 变量<code>matrix</code>应有 3 行。
|
||||
testString: assert(matrix.length == 3);
|
||||
- text: 变量<code>matrix</code>每行应有 2 列。
|
||||
testString: assert(matrix[0].length == 2 && matrix[1].length === 2 && matrix[2].length === 2);
|
||||
以下函数应该创建一个具有`m`行和`n`列“零”的二维数组。不幸的是,它没有产生预期的输出,因为`row`变量没有在外部循环中重新初始化(设置回空数组)。修改代码,使其正确地返回包含 3 行 2 列“零”的二维数组,即`[[0, 0], [0, 0], [0, 0]]`。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应将变量`matrix`设置为 3 行 2 列“零”的二维数组。
|
||||
|
||||
```js
|
||||
function zeroArray(m, n) {
|
||||
// Creates a 2-D array with m rows and n columns of zeroes
|
||||
let newArray = [];
|
||||
let row = [];
|
||||
for (let i = 0; i < m; i++) {
|
||||
// Adds the m-th row into newArray
|
||||
|
||||
for (let j = 0; j < n; j++) {
|
||||
// Pushes n zeroes into the current row to create the columns
|
||||
row.push(0);
|
||||
}
|
||||
// Pushes the current row, which now has n zeroes in it, to the array
|
||||
newArray.push(row);
|
||||
}
|
||||
return newArray;
|
||||
}
|
||||
|
||||
let matrix = zeroArray(3, 2);
|
||||
console.log(matrix);
|
||||
assert(JSON.stringify(matrix) == '[[0,0],[0,0],[0,0]]');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
变量`matrix`应有 3 行。
|
||||
|
||||
```js
|
||||
function zeroArray(m, n) {
|
||||
// Creates a 2-D array with m rows and n columns of zeroes
|
||||
let newArray = [];
|
||||
for (let i = 0; i < m; i++) {
|
||||
let row = [];
|
||||
// Adds the m-th row into newArray
|
||||
|
||||
for (let j = 0; j < n; j++) {
|
||||
// Pushes n zeroes into the current row to create the columns
|
||||
row.push(0);
|
||||
}
|
||||
// Pushes the current row, which now has n zeroes in it, to the array
|
||||
newArray.push(row);
|
||||
}
|
||||
return newArray;
|
||||
}
|
||||
|
||||
let matrix = zeroArray(3, 2);
|
||||
console.log(matrix);
|
||||
assert(matrix.length == 3);
|
||||
```
|
||||
|
||||
</section>
|
||||
变量`matrix`每行应有 2 列。
|
||||
|
||||
```js
|
||||
assert(
|
||||
matrix[0].length == 2 && matrix[1].length === 2 && matrix[2].length === 2
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,65 +1,33 @@
|
||||
---
|
||||
id: 587d7b83367417b2b2512b33
|
||||
title: 使用控制台检查变量值
|
||||
challengeType: 1
|
||||
forumTopicId: 18372
|
||||
title: 使用控制台检查变量值
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Chrome 和 Firefox 都有出色的 JavaScript 控制台(也称为 DevTools),可以用来调试 JavaScript 代码
|
||||
|
||||
可以在 Chrome 的菜单中找到“开发者工具”或 FireFox 的菜单中的 “Web 控制台”。如果你使用其他浏览器或手机,我们强烈建议你切换到桌面版 Firefox 或 Chrome。
|
||||
<code>console.log()</code>方法可能是最有用的调试工具,它可以将括号中的内容输出到控制台,将它放在代码中的关键点可以显示变量在当时的值。在查看输出之前,最好先想清楚输出应该是什么。在代码的合适位置打点来查看变量状态有助于定位问题。
|
||||
|
||||
`console.log()`方法可能是最有用的调试工具,它可以将括号中的内容输出到控制台,将它放在代码中的关键点可以显示变量在当时的值。在查看输出之前,最好先想清楚输出应该是什么。在代码的合适位置打点来查看变量状态有助于定位问题。
|
||||
|
||||
下面是输出 'Hello world!' 到控制台的示例:
|
||||
<code>console.log('Hello world!');</code>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
请使用<code>console.log()</code>方法在代码中注明的地方输出变量<code>a</code>的值。
|
||||
</section>
|
||||
`console.log('Hello world!');`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应使用<code>console.log()</code>来检查变量<code>a</code>的值。
|
||||
testString: assert(code.match(/console\.log\(a\)/g));
|
||||
请使用`console.log()`方法在代码中注明的地方输出变量`a`的值。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应使用`console.log()`来检查变量`a`的值。
|
||||
|
||||
```js
|
||||
let a = 5;
|
||||
let b = 1;
|
||||
a++;
|
||||
// Add your code below this line
|
||||
|
||||
|
||||
let sumAB = a + b;
|
||||
console.log(sumAB);
|
||||
assert(code.match(/console\.log\(a\)/g));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
var a = 5; console.log(a);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d7b84367417b2b2512b34
|
||||
title: 使用 type of 检查变量的类型
|
||||
challengeType: 1
|
||||
forumTopicId: 18374
|
||||
title: 使用 type of 检查变量的类型
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
可以使用<code>typeof</code>检查变量的数据结构或类型。在处理多种数据类型时,<code>typeof</code>会对调试很有帮助。如果想计算两数之和,但实际传入了一个字符串参数,则结果可能是错误的。类型错误可能隐藏在计算或函数调用中。当你以 JavaScript 对象(JSON)的形式访问和使用外部数据时尤其要小心。
|
||||
下面是使用<code>typeof</code>的一些示例:
|
||||
# --description--
|
||||
|
||||
可以使用`typeof`检查变量的数据结构或类型。在处理多种数据类型时,`typeof`会对调试很有帮助。如果想计算两数之和,但实际传入了一个字符串参数,则结果可能是错误的。类型错误可能隐藏在计算或函数调用中。当你以 JavaScript 对象(JSON)的形式访问和使用外部数据时尤其要小心。
|
||||
|
||||
下面是使用`typeof`的一些示例:
|
||||
|
||||
```js
|
||||
console.log(typeof ""); // 输出 "string"
|
||||
@ -16,56 +17,32 @@ console.log(typeof 0); // 输出 "number"
|
||||
console.log(typeof []); // 输出 "object"
|
||||
console.log(typeof {}); // 输出 "object"
|
||||
```
|
||||
JavaScript 有六种原始(不可变)数据类型:<code>Boolean</code>,<code>Null</code>,<code>Undefined</code>,<code>Number</code>,<code>String</code>, 和<code>Symbol</code>(ES6 新增)和一种可变的数据类型:<code>Object</code>。注意,在 JavaScript 中,数组在本质上是一种对象
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
添加两个<code>console.log()</code>语句来检查代码中的两个变量<code>seven</code>和<code>three</code>的 <code>typeof</code>值。
|
||||
</section>
|
||||
JavaScript 有六种原始(不可变)数据类型:`Boolean`,`Null`,`Undefined`,`Number`,`String`, 和`Symbol`(ES6 新增)和一种可变的数据类型:`Object`。注意,在 JavaScript 中,数组在本质上是一种对象
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应在两个<code>console.log()</code>语句中使用<code>typeof</code>来检查变量的类型。
|
||||
testString: assert(code.match(/console\.log\(typeof[\( ].*\)?\)/g).length == 2);
|
||||
- text: 你应使用<code>typeof</code>来检查变量<code>seven</code>的类型。
|
||||
testString: assert(code.match(/typeof[\( ]seven\)?/g));
|
||||
- text: 你应使用<code>typeof</code>来检查变量<code>three</code>的类型。
|
||||
testString: assert(code.match(/typeof[\( ]three\)?/g));
|
||||
添加两个`console.log()`语句来检查代码中的两个变量`seven`和`three`的 `typeof`值。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应在两个`console.log()`语句中使用`typeof`来检查变量的类型。
|
||||
|
||||
```js
|
||||
let seven = 7;
|
||||
let three = "3";
|
||||
console.log(seven + three);
|
||||
// Add your code below this line
|
||||
|
||||
assert(code.match(/console\.log\(typeof[\( ].*\)?\)/g).length == 2);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
你应使用`typeof`来检查变量`seven`的类型。
|
||||
|
||||
```js
|
||||
let seven = 7;let three = "3";console.log(typeof seven);
|
||||
console.log(typeof three);
|
||||
assert(code.match(/typeof[\( ]seven\)?/g));
|
||||
```
|
||||
|
||||
</section>
|
||||
你应使用`typeof`来检查变量`three`的类型。
|
||||
|
||||
```js
|
||||
assert(code.match(/typeof[\( ]three\)?/g));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
Reference in New Issue
Block a user