chore: seed chinese traditional (#42005)
Seeds the chinese traditional files manually so we can deploy to staging.
This commit is contained in:
committed by
GitHub
parent
e46e80e08f
commit
3da4be21bb
@ -0,0 +1,57 @@
|
||||
---
|
||||
id: 587d7b85367417b2b2512b3a
|
||||
title: 調用函數時,捕獲以錯誤順序傳遞的參數
|
||||
challengeType: 1
|
||||
forumTopicId: 301184
|
||||
dashedName: catch-arguments-passed-in-the-wrong-order-when-calling-a-function
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
繼續討論調用函數,需要注意的下一個 bug 是函數的參數傳遞順序錯誤。 如果參數分別是不同的類型,例如接受數組和整數兩個參數的函數,參數順序傳錯就可能會引發運行時錯誤。 對於接受相同類型參數的函數,傳錯參數也會導致邏輯錯誤或運行結果錯誤。 確保以正確的順序提供所有必需的參數以避免這些問題。
|
||||
|
||||
# --instructions--
|
||||
|
||||
函數 `raiseToPower` 返回基數 (base) 的指數 (exponent) 次冪。 不幸的是,它沒有被正確調用 — 修改代碼,使 `power` 的值爲 8。
|
||||
|
||||
# --hints--
|
||||
|
||||
你應修復變量 `power`,使其等於 2 的 3 次方,而不是 3 的 2 次方。
|
||||
|
||||
```js
|
||||
assert(power == 8);
|
||||
```
|
||||
|
||||
你調用 `raiseToPower` 函數時,傳遞參數的順序應正確。
|
||||
|
||||
```js
|
||||
assert(code.match(/raiseToPower\(\s*?base\s*?,\s*?exp\s*?\);/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function raiseToPower(b, e) {
|
||||
return Math.pow(b, e);
|
||||
}
|
||||
|
||||
let base = 2;
|
||||
let exp = 3;
|
||||
let power = raiseToPower(exp, base);
|
||||
console.log(power);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function raiseToPower(b, e) {
|
||||
return Math.pow(b, e);
|
||||
}
|
||||
|
||||
let base = 2;
|
||||
let exp = 3;
|
||||
let power = raiseToPower(base, exp);
|
||||
console.log(power);
|
||||
```
|
@ -0,0 +1,69 @@
|
||||
---
|
||||
id: 587d7b85367417b2b2512b39
|
||||
title: 捕捉函數調用後缺少的左括號和右括號
|
||||
challengeType: 1
|
||||
forumTopicId: 301185
|
||||
dashedName: catch-missing-open-and-closing-parenthesis-after-a-function-call
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
當函數或方法不接受任何參數時,你可能忘記在調用它時加上空的左括號和右括號。 通常,函數調用的結果會保存在變量中,供其他代碼使用。 可以通過將變量值(或其類型)打印到控制檯,查看輸出究竟是一個函數引用還是函數調用的返回值來檢測這類錯誤。
|
||||
|
||||
下面示例中的兩個變量是不同的:
|
||||
|
||||
```js
|
||||
function myFunction() {
|
||||
return "You rock!";
|
||||
}
|
||||
let varOne = myFunction;
|
||||
let varTwo = myFunction();
|
||||
```
|
||||
|
||||
這裏 `varOne` 是函數 `myFunction` ,`varTwo` 是字符串 `You rock!`
|
||||
|
||||
# --instructions--
|
||||
|
||||
修復代碼,將變量 `result` 設置爲調用函數 `getNine` 返回的值。
|
||||
|
||||
# --hints--
|
||||
|
||||
你應該修復變量 `result` 使其爲函數 `getNine` 的返回的 number 值。
|
||||
|
||||
```js
|
||||
assert(result == 9);
|
||||
```
|
||||
|
||||
你應該調用 `getNine` 函數。
|
||||
|
||||
```js
|
||||
assert(code.match(/getNine\(\)/g).length == 2);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function getNine() {
|
||||
let x = 6;
|
||||
let y = 3;
|
||||
return x + y;
|
||||
}
|
||||
|
||||
let result = getNine;
|
||||
console.log(result);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function getNine() {
|
||||
let x = 6;
|
||||
let y = 3;
|
||||
return x + y;
|
||||
}
|
||||
|
||||
let result = getNine();
|
||||
console.log(result);
|
||||
```
|
@ -0,0 +1,69 @@
|
||||
---
|
||||
id: 587d7b84367417b2b2512b35
|
||||
title: 捕獲拼錯的變量名和函數名
|
||||
challengeType: 1
|
||||
forumTopicId: 301186
|
||||
dashedName: catch-misspelled-variable-and-function-names
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`console.log()` 和 `typeof` 方法是檢查中間值和程序輸出類型的兩種主要方法。 現在是時候瞭解一下 bug 出現的常見的情形。 一個語法級別的問題是打字太快帶來的低級拼寫錯誤。
|
||||
|
||||
變量或函數名的錯寫、漏寫或大小寫弄混都會讓瀏覽器嘗試查找並不存在的東西,並報出“引用錯誤”。 JavaScript 變量和函數名稱區分大小寫。
|
||||
|
||||
# --instructions--
|
||||
|
||||
修復代碼中的兩個拼寫錯誤,以便 `netWorkingCapital` 計算有效。
|
||||
|
||||
# --hints--
|
||||
|
||||
檢查計算 netWorkingCapital 值時使用的兩個變量的拼寫是否正確,控制檯應該輸出 "Net working capital is: 2"。
|
||||
|
||||
```js
|
||||
assert(netWorkingCapital === 2);
|
||||
```
|
||||
|
||||
代碼中不應存在拼寫錯誤的變量。
|
||||
|
||||
```js
|
||||
assert(!code.match(/recievables/g));
|
||||
```
|
||||
|
||||
應在代碼中聲明並正確使用 `receivables` 變量。
|
||||
|
||||
```js
|
||||
assert(code.match(/receivables/g).length == 2);
|
||||
```
|
||||
|
||||
代碼中不應存在拼寫錯誤的變量。
|
||||
|
||||
```js
|
||||
assert(!code.match(/payable;/g));
|
||||
```
|
||||
|
||||
應在代碼中聲明並正確使用 `payables` 變量。
|
||||
|
||||
```js
|
||||
assert(code.match(/payables/g).length == 2);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let receivables = 10;
|
||||
let payables = 8;
|
||||
let netWorkingCapital = recievables - payable;
|
||||
console.log(`Net working capital is: ${netWorkingCapital}`);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let receivables = 10;
|
||||
let payables = 8;
|
||||
let netWorkingCapital = receivables - payables;
|
||||
console.log(`Net working capital is: ${netWorkingCapital}`);
|
||||
```
|
@ -0,0 +1,63 @@
|
||||
---
|
||||
id: 587d7b84367417b2b2512b37
|
||||
title: 捕捉單引號和雙引號的混合用法
|
||||
challengeType: 1
|
||||
forumTopicId: 301188
|
||||
dashedName: catch-mixed-usage-of-single-and-double-quotes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript 允許使用單引號(`'`)和雙引號(`"`)聲明字符串。 決定使用哪一個通常看個人偏好,但有一些例外。
|
||||
|
||||
如果字符串中有縮寫或存在一段帶引號的文本,你就會明白爲什麼 JavaScript 允許兩種引號了。 請注意,不要提前用引號結束字符串,這會導致語法錯誤。
|
||||
|
||||
下面是混合使用引號的一些示例:
|
||||
|
||||
```js
|
||||
const grouchoContraction = "I've had a perfectly wonderful evening, but this wasn't it.";
|
||||
const quoteInString = "Groucho Marx once said 'Quote me as saying I was mis-quoted.'";
|
||||
const uhOhGroucho = 'I've had a perfectly wonderful evening, but this wasn't it.';
|
||||
```
|
||||
|
||||
前兩項是正確的,但第三項是不正確的。
|
||||
|
||||
當然,只使用一種引號也是可以的。 可以使用反斜槓(`\`)來轉義字符串內的引號:
|
||||
|
||||
```js
|
||||
const allSameQuotes = 'I\'ve had a perfectly wonderful evening, but this wasn\'t it.';
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
修復字符串,使其對 `href` 值使用不同的引號,或者轉義現有的引號。 在整個字符串周圍保留雙引號。
|
||||
|
||||
# --hints--
|
||||
|
||||
你應通過更改或轉義來修復 `href` 的值 `#Home` 周圍的引號。
|
||||
|
||||
```js
|
||||
assert(code.match(/<a href=\s*?('|\\")#Home\1\s*?>/g));
|
||||
```
|
||||
|
||||
你應該在整個字符串外圍保留雙引號。
|
||||
|
||||
```js
|
||||
assert(code.match(/"<p>.*?<\/p>";/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let innerHtml = "<p>Click here to <a href="#Home">return home</a></p>";
|
||||
console.log(innerHtml);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let innerHtml = "<p>Click here to <a href=\"#Home\">return home</a></p>";
|
||||
console.log(innerHtml);
|
||||
```
|
@ -0,0 +1,93 @@
|
||||
---
|
||||
id: 587d7b86367417b2b2512b3b
|
||||
title: 捕獲使用索引的時候出現的錯誤
|
||||
challengeType: 1
|
||||
forumTopicId: 301189
|
||||
dashedName: catch-off-by-one-errors-when-using-indexing
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
當試圖訪問字符串或數組的特定索引(分割或訪問一個片段)或循環索引時,有時會出現 <dfn>Off by one errors</dfn> 錯誤(有時稱爲 OBOE)。 JavaScript 索引從 0 開始,而不是 1,這意味着最後一個索引總會比字符串或數組的長度少 1。 如果嘗試訪問等於長度的索引,程序可能會拋出“索引超出範圍”引用錯誤或打印出 `undefined`。
|
||||
|
||||
當使用將索引範圍作爲參數的字符串或數組方法時,閱讀相關的文檔並瞭解參數中的索引的包含性(即是否考慮進返回值中)很重要。 以下是一些錯誤的示例:
|
||||
|
||||
```js
|
||||
let alphabet = "abcdefghijklmnopqrstuvwxyz";
|
||||
let len = alphabet.length;
|
||||
for (let i = 0; i <= len; i++) {
|
||||
console.log(alphabet[i]);
|
||||
}
|
||||
for (let j = 1; j < len; j++) {
|
||||
console.log(alphabet[j]);
|
||||
}
|
||||
for (let k = 0; k < len; k++) {
|
||||
console.log(alphabet[k]);
|
||||
}
|
||||
```
|
||||
|
||||
第一個例子多了一次循環,第二個例子少了一次循環(漏掉了索引 0 處的字符), 第三個例子是正確的。
|
||||
|
||||
# --instructions--
|
||||
|
||||
修復以下函數中的兩個索引錯誤,將 1 到 5 之間(包含 1 和 5)的所有數字打印到控制檯。
|
||||
|
||||
# --hints--
|
||||
|
||||
應該設置循環的初始條件,使循環從第一個索引開始。
|
||||
|
||||
```js
|
||||
assert(code.match(/i\s*?=\s*?0\s*?;/g).length == 1);
|
||||
```
|
||||
|
||||
應修復循環的初始條件,使循環從索引 0 開始。
|
||||
|
||||
```js
|
||||
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));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function countToFive() {
|
||||
let firstFive = "12345";
|
||||
let len = firstFive.length;
|
||||
// Only change code below this line
|
||||
for (let i = 1; i <= len; i++) {
|
||||
// Only change code above this line
|
||||
console.log(firstFive[i]);
|
||||
}
|
||||
}
|
||||
|
||||
countToFive();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function countToFive() {
|
||||
let firstFive = "12345";
|
||||
let len = firstFive.length;
|
||||
// Only change code below this line
|
||||
for (let i = 0; i < len; i++) {
|
||||
// Only change code above this line
|
||||
console.log(firstFive[i]);
|
||||
}
|
||||
}
|
||||
|
||||
countToFive();
|
||||
```
|
@ -0,0 +1,49 @@
|
||||
---
|
||||
id: 587d7b84367417b2b2512b36
|
||||
title: '捕獲未閉合的括號、方括號、大括號和引號'
|
||||
challengeType: 1
|
||||
forumTopicId: 301190
|
||||
dashedName: catch-unclosed-parentheses-brackets-braces-and-quotes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
要注意的另一個語法錯誤是所有的小括號、方括號、花括號和引號都必須配對。 當你編輯代碼並插入新代碼其中帶有括號時,很容易忘記括號閉合。 此外,在將代碼塊嵌套到其他代碼塊時要小心,例如將回調函數作爲參數添加到方法中。
|
||||
|
||||
避免這種錯誤的一種方法是,一次性輸入完這些符號,然後將光標移回它們之間繼續編寫。 好在現在大部分編輯器都會幫你自動補全。
|
||||
|
||||
# --instructions--
|
||||
|
||||
修復代碼中的兩個符號配對錯誤。
|
||||
|
||||
# --hints--
|
||||
|
||||
應該修復數組缺少的部分。
|
||||
|
||||
```js
|
||||
assert(code.match(/myArray\s*?=\s*?\[\s*?1\s*?,\s*?2\s*?,\s*?3\s*?\];/g));
|
||||
```
|
||||
|
||||
應該修復 `.reduce()` 方法缺少的部分。 控制檯應該輸出 `Sum of array values is: 6`。
|
||||
|
||||
```js
|
||||
assert(arraySum === 6);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let myArray = [1, 2, 3;
|
||||
let arraySum = myArray.reduce((previous, current => previous + current);
|
||||
console.log(`Sum of array values is: ${arraySum}`);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let myArray = [1, 2, 3];
|
||||
let arraySum = myArray.reduce((previous, current) => previous + current);
|
||||
console.log(`Sum of array values is: ${arraySum}`);
|
||||
```
|
@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 587d7b85367417b2b2512b38
|
||||
title: 捕獲使用賦值運算符而不是相等運算符
|
||||
challengeType: 1
|
||||
forumTopicId: 301191
|
||||
dashedName: catch-use-of-assignment-operator-instead-of-equality-operator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
分支程序,即在滿足某些條件時執行不同操作的程序,依賴於 JavaScript 中的`if`,`else if`、`else`語句。 條件有時採取測試一個結果是否等於一個值的形式。
|
||||
|
||||
這種邏輯可以表述爲“如果 x 等於 y ,則......”,聽起來像是可以使用 `=`(即賦值運算符)。 然而,這會導致程序中流程出問題。
|
||||
|
||||
如前面的挑戰所述,JavaScript 中的賦值運算符 (`=`) 是用來爲變量名賦值的。 並且 `==` 和 `===` 運算符檢查相等性(三等號 `===` 是用來測試是否嚴格相等的,嚴格相等的意思是值和類型都必須相同)。
|
||||
|
||||
下面的代碼將 `x` 賦值爲 2,表達式會在執行後得到 `true`。 JavaScript 會把大部分的值都視爲 `true`,除了所謂的 “falsy”值,即:`false`、`0`、`""`(空字符串)、`NaN`、`undefined` 和 `null`。
|
||||
|
||||
```js
|
||||
let x = 1;
|
||||
let y = 2;
|
||||
if (x = y) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
在這個示例中,除非 `y` 值是假值,否則當 `y` 爲任何值時,`if` 語句中的代碼塊都會運行。 我們期望運行的 `else` 代碼塊實際上將不會運行。
|
||||
|
||||
# --instructions--
|
||||
|
||||
修復條件語句,以便程序運行正確的分支,並給 `result` 賦正確的值。
|
||||
|
||||
# --hints--
|
||||
|
||||
應該修復條件語句,使其判斷是否相等,而不是賦值。
|
||||
|
||||
```js
|
||||
assert(result == 'Not equal!');
|
||||
```
|
||||
|
||||
條件語句可以使用 `==` 或 `===` 來測試是否相等。
|
||||
|
||||
```js
|
||||
assert(code.match(/x\s*?===?\s*?y/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let x = 7;
|
||||
let y = 9;
|
||||
let result = "to come";
|
||||
|
||||
if(x = y) {
|
||||
result = "Equal!";
|
||||
} else {
|
||||
result = "Not equal!";
|
||||
}
|
||||
|
||||
console.log(result);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let x = 7;
|
||||
let y = 9;
|
||||
let result = "to come";
|
||||
|
||||
if(x === y) {
|
||||
result = "Equal!";
|
||||
} else {
|
||||
result = "Not equal!";
|
||||
}
|
||||
|
||||
console.log(result);
|
||||
```
|
@ -0,0 +1,63 @@
|
||||
---
|
||||
id: 587d7b86367417b2b2512b3d
|
||||
title: 使用有效的終止條件防止無限循環
|
||||
challengeType: 1
|
||||
forumTopicId: 301192
|
||||
dashedName: prevent-infinite-loops-with-a-valid-terminal-condition
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
最後一個話題是可怕的無限循環。 當需要程序運行代碼塊一定次數或滿足條件時,循環是很好的工具,但是它們需要終止條件來結束循環。 無限循環可能會使瀏覽器凍結或崩潰,並導致程序執行混亂,沒有人想要這樣的結果。
|
||||
|
||||
在本節的介紹中有一個無限循環的例子——它沒有終止條件來擺脫`loopy()`內的`while`循環。 不要調用這個函數!
|
||||
|
||||
```js
|
||||
function loopy() {
|
||||
while(true) {
|
||||
console.log("Hello, world!");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
程序員的工作是確保最終達到終止條件,該條件告訴程序何時跳出循環。 有一種錯誤是從終端條件向錯誤方向遞增或遞減計數器變量。 另一種是在循環代碼中意外重置計數器或索引變量,而不是遞增或遞減它。
|
||||
|
||||
# --instructions--
|
||||
|
||||
`myFunc()`函數包含一個無限循環,因爲終止條件`i != 4`永遠不會爲`false`(並中斷循環) -`i`將每次遞增 2,然後跳過 4,因爲`i`是從奇數開始遞增。 在終端條件中輸入比較運算符,使循環僅在`i`小於或等於 4 的情況下運行。
|
||||
|
||||
# --hints--
|
||||
|
||||
你應該在`for`循環的終止條件(中間部分)中更改比較運算符。
|
||||
|
||||
```js
|
||||
assert(code.match(/i\s*?<=\s*?4;/g).length == 1);
|
||||
```
|
||||
|
||||
你應該修改比較運算符來避免出現死循環。
|
||||
|
||||
```js
|
||||
assert(!code.match(/i\s*?!=\s*?4;/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function myFunc() {
|
||||
for (let i = 1; i != 4; i += 2) {
|
||||
console.log("Still going!");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function myFunc() {
|
||||
for (let i = 1; i <= 4; i += 2) {
|
||||
console.log("Still going!");
|
||||
}
|
||||
}
|
||||
```
|
@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 587d7b83367417b2b2512b37
|
||||
title: 瞭解 freeCodeCamp 和瀏覽器控制檯之間的差異
|
||||
challengeType: 1
|
||||
forumTopicId: 301193
|
||||
dashedName: understanding-the-differences-between-the-freecodecamp-and-browser-console
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
你可能已經注意到一些 freeCodeCamp JavaScript 的挑戰有自己的控制檯。 這些控制檯的行爲與上一次挑戰中使用的瀏覽器控制檯略有不同。
|
||||
|
||||
以下挑戰旨在強調 freeCodeCamp 控制檯與瀏覽器控制檯之間的一些差異。
|
||||
|
||||
當在瀏覽器中加載並運行 JavaScript 文件時,`console.log()` 語句會在控制檯中按照調用的次數準確地打印出要求的內容。
|
||||
|
||||
在編輯器檢測到腳本中的更改之後,以及測試期間,freeCodeCamp 控制檯將打印 `console.log()` 語句。
|
||||
|
||||
在運行測試之前,將清除 freeCodeCamp 控制檯,爲避免破壞,僅在第一次測試期間打印日誌(請參見下面的註釋)。
|
||||
|
||||
如果你想看到每次測試的日誌,運行測試,並打開瀏覽器控制檯。 如果你喜歡使用瀏覽器控制檯,想要它模仿 freeCodeCamp 控制檯,請在其他 `console` 調用前加上 `console.clear()`,以清除瀏覽器控制檯。
|
||||
|
||||
**注意:** 每次調用函數時,函數內的 `console.log` 都會被打印到 freeCodeCamp 控制檯。 這樣可以幫助在測試期間調試函數。
|
||||
|
||||
# --instructions--
|
||||
|
||||
首先,使用 `console.log` 打印 `output` 變量。 然後使用 `console.clear` 清除瀏覽器控制檯。
|
||||
|
||||
# --hints--
|
||||
|
||||
應該使用 `console.clear()` 清除瀏覽器控制檯。
|
||||
|
||||
```js
|
||||
assert(
|
||||
__helpers
|
||||
.removeWhiteSpace(code)
|
||||
.match(/console.clear\(\)/)
|
||||
);
|
||||
```
|
||||
|
||||
應該使用 `console.log()` 打印 `output` 變量。
|
||||
|
||||
```js
|
||||
assert(__helpers.removeWhiteSpace(code).match(/console\.log\(output\)/));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Open your browser console.
|
||||
let output = "Get this to log once in the freeCodeCamp console and twice in the browser console";
|
||||
// Use console.log() to print the output variable.
|
||||
|
||||
// Run the tests to see the difference between the two consoles.
|
||||
|
||||
// Now, add console.clear() before your console.log() to clear the browser console, and pass the tests.
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// Open your browser console.
|
||||
let output = "Get this to log once in the freeCodeCamp console and twice in the browser console";
|
||||
// Use console.log() to print the output variable.
|
||||
console.clear();
|
||||
console.log(output);
|
||||
|
||||
// Run the tests to see the difference between the two consoles.
|
||||
|
||||
// Now, add console.clear() before your console.log() to clear the browser console, and pass the tests.
|
||||
```
|
@ -0,0 +1,89 @@
|
||||
---
|
||||
id: 587d7b86367417b2b2512b3c
|
||||
title: 重新初始化循環中的變量時要小心
|
||||
challengeType: 1
|
||||
forumTopicId: 301194
|
||||
dashedName: use-caution-when-reinitializing-variables-inside-a-loop
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
有時需要在循環中保存信息以增加計數器或重置變量。 一個潛在的問題是變量什麼時候該重新初始化,什麼時候不該重新初始化,反之亦然。 如果你不小心重置了用於終止條件的變量,導致無限循環,這將特別危險。
|
||||
|
||||
使用`console.log()`在每個循環中打印變量值可以發現與重置相關的錯誤或者重置變量失敗。
|
||||
|
||||
# --instructions--
|
||||
|
||||
以下函數應該創建一個具有`m`行和`n`列“零”的二維數組。 不幸的是,它沒有產生預期的輸出,因爲`row`變量沒有在外部循環中重新初始化(設置回空數組)。 修改代碼,使其正確地返回包含 3 行 2 列“零”的二維數組,即`[[0, 0], [0, 0], [0, 0]]`。
|
||||
|
||||
# --hints--
|
||||
|
||||
應將變量 `matrix` 設置爲 3 行 2 列“零”的二維數組。
|
||||
|
||||
```js
|
||||
assert(JSON.stringify(matrix) == '[[0,0],[0,0],[0,0]]');
|
||||
```
|
||||
|
||||
變量 `matrix` 應有 3 行。
|
||||
|
||||
```js
|
||||
assert(matrix.length == 3);
|
||||
```
|
||||
|
||||
變量 `matrix` 每行應有 2 列。
|
||||
|
||||
```js
|
||||
assert(
|
||||
matrix[0].length == 2 && matrix[1].length === 2 && matrix[2].length === 2
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```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);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```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);
|
||||
```
|
@ -0,0 +1,54 @@
|
||||
---
|
||||
id: 587d7b83367417b2b2512b33
|
||||
title: 使用控制檯檢查變量值
|
||||
challengeType: 1
|
||||
forumTopicId: 18372
|
||||
dashedName: use-the-javascript-console-to-check-the-value-of-a-variable
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Chrome 和 Firefox 都有出色的 JavaScript 控制檯(也稱爲 DevTools),可以用來調試 JavaScript 代碼
|
||||
|
||||
可以在 Chrome 的菜單中找到“開發者工具”或 FireFox 的菜單中的 “Web 控制檯”。 如果你使用其他瀏覽器或手機,我們強烈建議你切換到桌面版 Firefox 或 Chrome。
|
||||
|
||||
`console.log()` 方法可能是最有用的調試工具,它可以將括號中的內容輸出到控制檯。 將它放在代碼中的關鍵點可以顯示變量在當時的值。 在查看輸出之前,最好先想清楚輸出應該是什麼。 在整個代碼中使用檢查點來查看計算狀態將有助於縮小問題的範圍。
|
||||
|
||||
下面是輸出 `Hello world!` 字符串到控制檯的示例:
|
||||
|
||||
```js
|
||||
console.log('Hello world!');
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
使用 `console.log()` 方法打印代碼中記錄的變量 `a` 的值。
|
||||
|
||||
# --hints--
|
||||
|
||||
應使用 `console.log()` 來檢查變量 `a` 的值。
|
||||
|
||||
```js
|
||||
assert(code.match(/console\.log\(a\)/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let a = 5;
|
||||
let b = 1;
|
||||
a++;
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
let sumAB = a + b;
|
||||
console.log(sumAB);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a = 5; console.log(a);
|
||||
```
|
@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 587d7b84367417b2b2512b34
|
||||
title: 使用 type of 檢查變量的類型
|
||||
challengeType: 1
|
||||
forumTopicId: 18374
|
||||
dashedName: use-typeof-to-check-the-type-of-a-variable
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
可以使用 `typeof` 檢查變量的數據結構或類型。 在處理多種數據類型時,這會對調試很有幫助。 如果想計算兩數之和,但實際傳入了一個字符串參數,則結果可能是錯誤的。 類型錯誤可能隱藏在計算或函數調用中。 當你以 JavaScript 對象(JSON)的形式訪問和使用外部數據時尤其要小心。
|
||||
|
||||
下面是使用 `typeof` 的一些示例:
|
||||
|
||||
```js
|
||||
console.log(typeof "");
|
||||
console.log(typeof 0);
|
||||
console.log(typeof []);
|
||||
console.log(typeof {});
|
||||
```
|
||||
|
||||
控制檯將按順序顯示字符串 `string`、`number`、`object` 和 `object`。
|
||||
|
||||
JavaScript 有六種原始(不可變)數據類型:`Boolean`、`Null`、`Undefined`、`Number`、`String` 和 `Symbol`(ES6 中新增的),和一種可變的數據類型:`Object`。 注意:在 JavaScript 中,數組在本質上是一種對象。
|
||||
|
||||
# --instructions--
|
||||
|
||||
添加兩個 `console.log()` 語句來檢查代碼中的兩個變量 `seven` 和 `three` 的 `typeof` 值。
|
||||
|
||||
# --hints--
|
||||
|
||||
應在兩個 `console.log()` 語句中使用 `typeof` 來檢查變量的類型。
|
||||
|
||||
```js
|
||||
assert(code.match(/console\.log\(typeof[\( ].*\)?\)/g).length == 2);
|
||||
```
|
||||
|
||||
應使用 `typeof` 來檢查變量 `seven` 的類型。
|
||||
|
||||
```js
|
||||
assert(code.match(/typeof[\( ]seven\)?/g));
|
||||
```
|
||||
|
||||
應使用 `typeof` 來檢查變量 `three` 的類型。
|
||||
|
||||
```js
|
||||
assert(code.match(/typeof[\( ]three\)?/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
let seven = 7;
|
||||
let three = "3";
|
||||
console.log(seven + three);
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let seven = 7;let three = "3";console.log(typeof seven);
|
||||
console.log(typeof three);
|
||||
```
|
Reference in New Issue
Block a user