feat(curriculum): introduce let and const earlier (#43133)
* fix: move "Explore Differences Between..." to basic JS, update seed and tests * fix: resequence "Declare String Variables" * fix: move "Declare a Read-Only Variable..." to basic JS, update seed and tests * fix: revert changes to non-English "Explore Differences Between..." test text * fix: revert test strings, solutions, and seeds for non-English challenges * fix: update "Declare String Variables" description * fix: sync quotation marks in description and seed * fix: modify note in "Declare a Read-Only..." challenge * fix: update operator and compound assignment challenges * fix: update string challenges * fix: update array and array method challenges * fix: update function and scope challenges, resequence slightly * fix: "Word Blanks" solution * fix: add spacing to seed * fix: concatenating += challenge spacing * fix: appending variables to strings spacing * fix: find the length of a string spacing * fix: removed instances of removedFromMyArray = 0 * fix: switch challenges * fix: function argument and param spacing * fix: update counting cards, object challenges, and record collection * fix: finish rest of Basic JS section * fix: introducing else statements solution * fix: update spacing and wording * fix: update wording for const challenge * fix: update functional programming challenges * fix: intermediate algorithms and cert challenges * fix: revert some spacing and remove comments for fp challenge solutions * feat: add notes with links to moved let and const challenges in first two es6 challenges * fix: update es6 intro text * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/declare-a-read-only-variable-with-the-const-keyword.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * fix: concatenating strings with plus operator seed * fix: add comments back to Declare a Read-Only Variable... seed * feat: add es6 to basic javascript redirect tests for let and const challenges * fix: revert "Concatenating Strings with Plus Operator" seed * fix: move test file to cypress/integration/learn/redirects, separate redirect tests Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
This commit is contained in:
committed by
GitHub
parent
71b555ab16
commit
bcc9beff1f
@ -1,89 +0,0 @@
|
||||
---
|
||||
id: 587d7b87367417b2b2512b41
|
||||
title: 用 const 關鍵字聲明只讀變量
|
||||
challengeType: 1
|
||||
forumTopicId: 301201
|
||||
dashedName: declare-a-read-only-variable-with-the-const-keyword
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
`let` 並不是唯一的新的聲明變量的方式。 在 ES6 裏面,你還可以使用 `const` 關鍵字來聲明變量。
|
||||
|
||||
`const` 擁有 `let` 的所有優點,不同的是,通過 `const` 聲明的變量是隻讀的。 這意味着通過 `const` 聲明的變量只能被賦值一次,而不能被再次賦值。
|
||||
|
||||
```js
|
||||
const FAV_PET = "Cats";
|
||||
FAV_PET = "Dogs";
|
||||
```
|
||||
|
||||
控制檯將由於給 `FAV_PET` 重新賦值而顯示錯誤。
|
||||
|
||||
可見,嘗試給用 `const` 聲明的變量重新賦值會報錯。 你應該使用 `const` 關鍵字來聲明所有不打算再次賦值的變量。 這有助於避免給一個常量進行額外的再次賦值。 一個最佳實踐是對所有常量的命名採用全大寫字母,並在單詞之間使用下劃線進行分隔。
|
||||
|
||||
**注意:**通常,開發者會用大寫字母作爲常量標識符,用小寫字母或者駝峯命名作爲變量(對象或數組)標識符。 後面的挑戰會涉及到在數組中使用小寫變量標識符。
|
||||
|
||||
# --instructions--
|
||||
|
||||
改變以下代碼,使得所有的變量都使用 `let` 或 `const` 關鍵詞來聲明。 當變量將會改變的時候使用 `let` 關鍵字,當變量要保持常量的時候使用 `const` 關鍵字。 同時,對使用 `const` 聲明的變量按照最佳實踐重命名,變量名中的字母應該都是大寫的。
|
||||
|
||||
# --hints--
|
||||
|
||||
代碼中不應有 `var`。
|
||||
|
||||
```js
|
||||
(getUserInput) => assert(!getUserInput('index').match(/var/g));
|
||||
```
|
||||
|
||||
`SENTENCE` 應該是使用 `const` 聲明的常量。
|
||||
|
||||
```js
|
||||
(getUserInput) => assert(getUserInput('index').match(/(const SENTENCE)/g));
|
||||
```
|
||||
|
||||
`i` 應該是使用 `let`聲明的。
|
||||
|
||||
```js
|
||||
(getUserInput) => assert(getUserInput('index').match(/(let i)/g));
|
||||
```
|
||||
|
||||
`console.log` 應該修改爲用於打印 `SENTENCE` 變量。
|
||||
|
||||
```js
|
||||
(getUserInput) =>
|
||||
assert(getUserInput('index').match(/console\.log\(\s*SENTENCE\s*\)\s*;?/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function printManyTimes(str) {
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var sentence = str + " is cool!";
|
||||
for (var i = 0; i < str.length; i+=2) {
|
||||
console.log(sentence);
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
}
|
||||
printManyTimes("freeCodeCamp");
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function printManyTimes(str) {
|
||||
|
||||
const SENTENCE = str + " is cool!";
|
||||
for (let i = 0; i < str.length; i+=2) {
|
||||
console.log(SENTENCE);
|
||||
}
|
||||
|
||||
}
|
||||
printManyTimes("freeCodeCamp");
|
||||
```
|
@ -1,91 +0,0 @@
|
||||
---
|
||||
id: 587d7b87367417b2b2512b3f
|
||||
title: 探索 var 和 let 關鍵字之間的差異
|
||||
challengeType: 1
|
||||
forumTopicId: 301202
|
||||
dashedName: explore-differences-between-the-var-and-let-keywords
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
使用 `var` 關鍵字來聲明變量,會出現重複聲明導致變量被覆蓋卻不會報錯的問題。
|
||||
|
||||
```js
|
||||
var camper = 'James';
|
||||
var camper = 'David';
|
||||
console.log(camper);
|
||||
```
|
||||
|
||||
這裏控制檯將顯示字符串 `David`。
|
||||
|
||||
在上面的代碼中,`camper` 變量的初始值爲 `James`,然後又被覆蓋成了 `David`。 在小型的應用中,你可能不會遇到這樣的問題。但是當你的代碼規模變得更加龐大的時候,就可能會在不經意間覆蓋了之前定義的變量。 因爲這樣的情況不會報錯,所以搜索和修復 bug 會變得非常困難。
|
||||
在 ES6 中引入了新的關鍵字 `let` 來解決 `var` 關鍵字帶來的潛在問題。 如果你在上面的代碼中使用 `let` 關鍵字來代替 `var` 關鍵字,結果會是一個報錯。
|
||||
|
||||
```js
|
||||
let camper = 'James';
|
||||
let camper = 'David';
|
||||
```
|
||||
|
||||
你可以在瀏覽器的控制檯裏看見這個錯誤。 與 `var` 不同的是,當使用 `let` 的時候,同一名字的變量只能被聲明一次。 請注意 `"use strict"`。 這代表着開啓了嚴格模式,用於檢測常見的代碼錯誤以及“不安全”的行爲, 例如:
|
||||
|
||||
```js
|
||||
"use strict";
|
||||
x = 3.14;
|
||||
```
|
||||
|
||||
這將顯示一個錯誤 `x is not defined`。
|
||||
|
||||
# --instructions--
|
||||
|
||||
請更新這段代碼,只使用 `let` 關鍵字。
|
||||
|
||||
# --hints--
|
||||
|
||||
代碼中不應有 `var`
|
||||
|
||||
```js
|
||||
(getUserInput) => assert(!getUserInput('index').match(/var/g));
|
||||
```
|
||||
|
||||
`catName` 變量的值應該爲 `Oliver`
|
||||
|
||||
```js
|
||||
assert(catName === 'Oliver');
|
||||
```
|
||||
|
||||
`quote` 變量的值應該爲 `Oliver says Meow!`
|
||||
|
||||
```js
|
||||
assert(quote === 'Oliver says Meow!');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var catName;
|
||||
var quote;
|
||||
function catTalk() {
|
||||
"use strict";
|
||||
|
||||
catName = "Oliver";
|
||||
quote = catName + " says Meow!";
|
||||
|
||||
}
|
||||
catTalk();
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
let catName;
|
||||
let quote;
|
||||
function catTalk() {
|
||||
'use strict';
|
||||
|
||||
catName = 'Oliver';
|
||||
quote = catName + ' says Meow!';
|
||||
}
|
||||
catTalk();
|
||||
```
|
Reference in New Issue
Block a user