* fix: Chinese test suite Add localeTiltes, descriptions, and adjust test text and testStrings to get the automated test suite working. * fix: ran script, updated testStrings and solutions
2.5 KiB
2.5 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7b87367417b2b2512b41 | Declare a Read-Only Variable with the const Keyword | 1 | 使用const关键字声明只读变量 |
Description
let
不是声明变量的唯一新方法。在ES6中,您还可以使用const
关键字声明变量。 const
拥有所有的真棒功能, let
具有,与额外的奖励,使用声明的变量const
是只读的。它们是一个常量值,这意味着一旦变量被赋值为const
,就无法重新赋值。 "use strict"如您所见,尝试重新分配使用
const FAV_PET ="Cats";
FAV_PET = "Dogs"; //返回错误
const
声明的变量将引发错误。您应该始终使用const
关键字命名您不想重新分配的变量。当您意外尝试重新分配一个旨在保持不变的变量时,这会有所帮助。命名常量时的常见做法是使用全部大写字母,单词用下划线分隔。 Instructions
let
或const
声明所有变量。如果希望变量更改,请使用let
;如果希望变量保持不变,请使用const
。此外,重命名用const
声明的变量以符合常规做法,这意味着常量应该全部大写。 Tests
tests:
- text: <code>var</code>在您的代码中不存在。
testString: getUserInput => assert(!getUserInput('index').match(/var/g));
- text: <code>SENTENCE</code>应该是用<code>const</code>声明的常量变量。
testString: getUserInput => assert(getUserInput('index').match(/(const SENTENCE)/g));
- text: <code>i</code>应该以<code>let</code>来宣布。
testString: getUserInput => assert(getUserInput('index').match(/(let i)/g));
- text: 应更改<code>console.log</code>以打印<code>SENTENCE</code>变量。
testString: getUserInput => assert(getUserInput('index').match(/console\.log\(\s*SENTENCE\s*\)\s*;?/g));
Challenge Seed
function printManyTimes(str) {
"use strict";
// change code below this line
var sentence = str + " is cool!";
for(var i = 0; i < str.length; i+=2) {
console.log(sentence);
}
// change code above this line
}
printManyTimes("freeCodeCamp");
Solution
// solution required