* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.5 KiB
2.5 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7b87367417b2b2512b41 | 用 const 关键字声明只读变量 | 1 | 301201 | declare-a-read-only-variable-with-the-const-keyword |
--description--
let
并不是唯一的新的声明变量的方式。在 ES6里面,你还可以使用const
关键字来声明变量。
const
拥有let
的所有优点,所不同的是,通过const
声明的变量是只读的。这意味着通过const
声明的变量只能被赋值一次,而不能被再次赋值。
"use strict";
const FAV_PET = "Cats";
FAV_PET = "Dogs"; // returns error
可以看见,尝试给通过const
声明的变量再次赋值会报错。你应该使用const
关键字来对所有不打算再次赋值的变量进行声明。这有助于你避免给一个常量进行额外的再次赋值。一个最佳实践是对所有常量的命名采用全大写字母,并在单词之间使用下划线进行分隔。
注意: 一般开发者会以大写做为常量标识符,小写字母或者驼峰命名做为变量(对象或数组)标识符。接下来的挑战里会涉及到小写变量标识符的数组。
--instructions--
改变以下代码,使得所有的变量都使用let
或const
关键词来声明。当变量将会改变的时候使用let
关键字,当变量要保持常量的时候使用const
关键字。同时,对使用const
声明的变量按照最佳实践重命名,变量名中的字母应该都是大写的。
--hints--
var
在代码中不存在。
(getUserInput) => assert(!getUserInput('index').match(/var/g));
SENTENCE
应该是使用const
声明的常量。
(getUserInput) => assert(getUserInput('index').match(/(const SENTENCE)/g));
i
应该是使用let
声明的变量。
(getUserInput) => assert(getUserInput('index').match(/(let i)/g));
console.log
应该修改为用于打印SENTENCE
变量。
(getUserInput) =>
assert(getUserInput('index').match(/console\.log\(\s*SENTENCE\s*\)\s*;?/g));
--seed--
--seed-contents--
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--
function printManyTimes(str) {
const SENTENCE = str + " is cool!";
for (let i = 0; i < str.length; i+=2) {
console.log(SENTENCE);
}
}
printManyTimes("freeCodeCamp");