chore(i18n,curriculum): update translations (#44138)

This commit is contained in:
camperbot
2021-11-06 08:56:52 -07:00
committed by GitHub
parent 09b1592a53
commit 9385d7997b
69 changed files with 685 additions and 619 deletions

View File

@ -17,7 +17,7 @@ dashedName: assignment-with-a-returned-value
ourSum = sum(5, 12);
```
调用函数 `sum`函数返回 `17`,然后将该值赋给变量 `ourSum`
将调用 `sum` 函数,该函数返回 `17` 的值并将其分配给 `ourSum` 变量
# --instructions--
@ -49,13 +49,14 @@ assert(/processed\s*=\s*processArg\(\s*7\s*\)/.test(code));
```js
// Setup
var processed = 0;
let processed = 0;
function processArg(num) {
return (num + 3) / 5;
}
// Only change code below this line
```
# --solutions--

View File

@ -25,7 +25,7 @@ dashedName: comparison-with-the-inequality-operator
# --instructions--
`if` 语句中添加不等运算符 `!=`,这样函数在 `val` 不等于 `99` 的时候,会返回 `Not Equal`
`if` 语句中添加不等运算符 `!=` 以便函数在 `val` 不等于 `99` 时返回字符串 `Not Equal`
# --hints--

View File

@ -26,7 +26,7 @@ dashedName: comparison-with-the-strict-equality-operator
# --instructions--
`if` 语句中,添加严格相等运算符,这样函数在`val` 严格等于 `7` 的时候,会返回 `Equal`
`if` 语句中使用严格相等运算符,这样当 `val` 严格等于 `7` 时,函数将返回字符串 `Equal`
# --hints--

View File

@ -22,7 +22,7 @@ dashedName: concatenating-strings-with-plus-operator
例如:
```js
var ourStr = "I come first. " + "I come second.";
const ourStr = "I come first. " + "I come second.";
```
字符串 `I come first. I come second.` 将显示在控制台中。
@ -44,10 +44,10 @@ assert(myStr === 'This is the start. This is the end.');
assert(code.match(/(["']).*\1\s*\+\s*(["']).*\2/g));
```
应使用 `var` 关键字创建 `myStr`
`myStr` 应该使用 `const` 关键字创建
```js
assert(/var\s+myStr/.test(code));
assert(/const\s+myStr/.test(code));
```
应把结果赋值给 `myStr` 变量。
@ -73,11 +73,11 @@ assert(/myStr\s*=/.test(code));
## --seed-contents--
```js
var myStr; // Change this line
const myStr = ""; // Change this line
```
# --solutions--
```js
var myStr = "This is the start. " + "This is the end.";
const myStr = "This is the start. " + "This is the end.";
```

View File

@ -16,13 +16,14 @@ dashedName: count-backwards-with-a-for-loop
设置 `i = 10`,并且当 `i > 0` 的时候才继续循环。 我们使用 `i -= 2` 来让 `i` 每次循环递减 2。
```js
var ourArray = [];
for (var i = 10; i > 0; i -= 2) {
const ourArray = [];
for (let i = 10; i > 0; i -= 2) {
ourArray.push(i);
}
```
循环结束后,`ourArray` 的值为 `[10,8,6,4,2]`。 让我们改变初始值和最后的表达式,这样我们就可以按照奇数从后往前两两倒着数。
`ourArray` 现在将包含 `[10, 8, 6, 4, 2]`。 让我们改变初始值和最后的表达式,这样我们就可以按照奇数从后往前两两倒着数。
# --instructions--
@ -42,7 +43,7 @@ assert(/for\s*\([^)]+?\)/.test(code));
assert(code.match(/myArray.push/));
```
`myArray` 应该等于 `[9,7,5,3,1]`
`myArray` 应该等于 `[9, 7, 5, 3, 1]`
```js
assert.deepEqual(myArray, [9, 7, 5, 3, 1]);
@ -60,16 +61,17 @@ if(typeof myArray !== "undefined"){(function(){return myArray;})();}
```js
// Setup
var myArray = [];
const myArray = [];
// Only change code below this line
```
# --solutions--
```js
var myArray = [];
for (var i = 9; i > 0; i -= 2) {
const myArray = [];
for (let i = 9; i > 0; i -= 2) {
myArray.push(i);
}
```

View File

@ -1,6 +1,6 @@
---
id: 587d7b87367417b2b2512b41
title: 用 const 关键字声明只读变量
title: 使用 const 关键字声明只读变量
challengeType: 1
forumTopicId: 301201
dashedName: declare-a-read-only-variable-with-the-const-keyword
@ -8,50 +8,62 @@ dashedName: declare-a-read-only-variable-with-the-const-keyword
# --description--
`let` 并不是唯一的新的声明变量的方式。 在 ES6 里面,你还可以使用 `const` 关键字声明变量。
关键字 `let` 并不是声明变量的唯一新方法。 在 ES6 ,你还可以使用 `const` 关键字声明变量。
`const` `let` 的所有优点,不同的是,通过 `const` 声明的变量是只读的。 这意味着通过 `const` 声明的变量只能被赋值一次,而不能被再次赋值
`const` `let` 的所有出色功能,另外还有一个额外的好处,即使用 `const` 声明的变量是只读的。 它们是一个常量值,这意味着一旦一个变量被赋值为 `const`,它就不能被重新赋值
```js
const FAV_PET = "Cats";
FAV_PET = "Dogs";
```
控制台将由于给 `FAV_PET` 重新赋值而显示错误。
由于重新分配 `FAV_PET` 的值,控制台将显示错误。
可见,尝试给用 `const` 声明的变量重新赋值会报错。 你应该使用 `const` 关键字来声明所有不打算再次赋值的变量。 这有助于避免给一个常量进行额外的再次赋值。 一个最佳实践是对所有常量的命名采用全大写字母,并在单词之间使用下划线进行分隔。
你应该始终使用 `const` 关键字命名不想重新分配的变量。 这有助于避免给一个常量进行额外的再次赋值。
**注意:**通常,开发者会用大写字母作为常量标识符,用小写字母或者驼峰命名作为变量(对象或数组)标识符。 后面的挑战会涉及到在数组中使用小写变量标识符
命名常量的常见做法是全部使用大写字母,单词之间用下划线分隔
**注意:** 对于不可变值,开发人员通常使用大写变量标识符,对可变值(对象和数组)使用小写或驼峰式标识符。 你将在后面的挑战中了解有关对象、数组以及不可变和可变值的更多信息。 同样在后面的挑战中,你将看到大写、小写或驼峰式变量标识符的示例。
# --instructions--
改变以下代码,使得所有的变量都使用 `let``const` 关键词来声明。 当变量将会改变的时候使用 `let` 关键字,当变量保持常量的时候使用 `const` 关键字。 同时,对使`const` 声明的变量按照最佳实践重命名,变量名中的字母应该都是大写
更改代码,以便使用 `let``const` 声明所有变量。 当你希望变量改变时使用 `let`,而当你希望变量保持不变时使用 `const`。 此外,重命名`const` 声明的变量以符合常见做法,这意味着常量应该全部大写。
# --hints--
代码中不应有 `var`
`var` 不应存在于你的代码中
```js
(getUserInput) => assert(!getUserInput('index').match(/var/g));
```
`SENTENCE` 应该是使用 `const` 声明的常量
你应该将 `fCC` 更改为全部大写
```js
(getUserInput) => assert(getUserInput('index').match(/(const SENTENCE)/g));
(getUserInput) => {
assert(getUserInput('index').match(/(FCC)/));
assert(!getUserInput('index').match(/fCC/));
}
```
`i` 应该是使`let`声明的。
`FCC` 应该是一个`const` 声明的常量变量
```js
(getUserInput) => assert(getUserInput('index').match(/(let i)/g));
assert.equal(FCC, 'freeCodeCamp');
assert.match(code, /const\s+FCC/);
```
`console.log` 应该修改为用于打印 `SENTENCE` 变量
`fact` 应该用 `let` 声明
```js
(getUserInput) => assert(getUserInput('index').match(/(let fact)/g));
```
`console.log` 应该更改为打印 `FCC``fact` 变量。
```js
(getUserInput) =>
assert(getUserInput('index').match(/console\.log\(\s*SENTENCE\s*\)\s*;?/g));
assert(getUserInput('index').match(/console\.log\(\s*FCC\s*\,\s*fact\s*\)\s*;?/g));
```
# --seed--
@ -59,31 +71,18 @@ FAV_PET = "Dogs";
## --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");
var fCC = "freeCodeCamp"; // Change this line
var fact = "is cool!"; // Change this line
fact = "is awesome!";
console.log(fCC, fact); // Change this line
```
# --solutions--
```js
function printManyTimes(str) {
const FCC = "freeCodeCamp";
let fact = "is cool!";
const SENTENCE = str + " is cool!";
for (let i = 0; i < str.length; i+=2) {
console.log(SENTENCE);
}
}
printManyTimes("freeCodeCamp");
fact = "is awesome!";
console.log(FCC, fact);
```

View File

@ -9,21 +9,27 @@ dashedName: declare-string-variables
# --description--
之前我们写过这样的代码
之前,你使用以下代码声明变量
```js
var myName;
```
但是你也可以像这样声明一个字符串变量:
```js
var myName = "your name";
```
`"your name"` 被称<dfn>字符串</dfn><dfn>字面量</dfn>这是一个字符串,因为它是一系列包含在单引号或双引号中的零或多个字符。
`"your name"` 被称<dfn>string</dfn> <dfn>literal</dfn>字符串文字或字符串是用单引号或双引号括起来的一系列零个或多个字符。
# --instructions--
创建两个新的字符串变量:`myFirstName``myLastName`,并用你的姓和名分别为它们赋值。
创建两个新的字符串变量:`myFirstName``myLastName`,并分别为它们分配你的名字和姓氏的值。
# --hints--
`myFirstName` 应该是一个字符串,至少包含一个字符。
`myFirstName` 应该是一个至少包含一个字符的字符串
```js
assert(
@ -41,7 +47,7 @@ assert(
);
```
`myLastName` 应该是一个字符串,至少包含一个字符。
`myLastName` 应该是一个至少包含一个字符的字符串
```js
assert(

View File

@ -8,55 +8,53 @@ dashedName: explore-differences-between-the-var-and-let-keywords
# --description--
使用 `var` 关键字声明变量,会出现重复声明导致变量被覆盖却不会报错的问题。
使用 `var` 关键字声明变量的最大问题之一是你可以轻松覆盖变量声明:
```js
var camper = 'James';
var camper = 'David';
var camper = "James";
var camper = "David";
console.log(camper);
```
这里控制台显示字符串 `David`
在上面的代码中,`camper` 变量最初声明为 `James`,然后被覆盖为 `David`。 然后控制台显示字符串 `David`
在上面的代码中,`camper` 变量的初始值为 `James`,然后又被覆盖成了 `David`在小型应用中,你可能不会遇到这样的问题。但是你的代码规模变得更加庞大的时候,就可能会在不经意间覆盖了之前定义的变量。 因为这样的情况不会报错,所以搜索和修复 bug 会变得非常困难。
在 ES6 中引入了新的关键字 `let` 来解决 `var` 关键字带来的潜在问题。 如果你在上面的代码中使用 `let` 关键字来代替 `var` 关键字,结果会是一个报错。
在小型应用程序中,你可能不会遇到此类问题。 但是随着你的代码库变大,你可能会意外地覆盖一个你不打算覆盖的变量。 由于此行为不会引发错误,因此搜索和修复错误变得更加困难。
ES6 中引入了一个名为 `let` 的关键字,这是对 JavaScript 的一次重大更新,以解决与 `var` 关键字有关的潜在问题。 你将在后面的挑战中了解其他 ES6 特性。
如果将上面代码中的 `var` 替换为 `let` ,则会导致错误:
```js
let camper = 'James';
let camper = 'David';
let camper = "James";
let camper = "David";
```
可以在浏览器控制台里看见这个错误。 与 `var` 不同的是,当使用 `let` 的时候,同一名字的变量只能被声明一次。 请注意 `"use strict"`。 这代表着开启了严格模式,用于检测常见的代码错误以及“不安全”的行为, 例如:
该错误可以在你的浏览器控制台中看到。
```js
"use strict";
x = 3.14;
```
这将显示一个错误 `x is not defined`
所以不像 `var`,当你使用 `let` 时,同名的变量只能声明一次。
# --instructions--
更新这段代码,使用 `let` 关键字。
更新代码,使其仅使用 `let` 关键字。
# --hints--
代码中不应有 `var`
`var` 不应存在于代码中。
```js
(getUserInput) => assert(!getUserInput('index').match(/var/g));
```
`catName` 变量的值应该为 `Oliver`
`catName` 应该是字符串 `Oliver`
```js
assert(catName === 'Oliver');
```
`quote` 变量的值应该为 `Oliver says Meow!`
`catSound` 应该是字符串 `Meow!`
```js
assert(quote === 'Oliver says Meow!');
assert(catSound === 'Meow!');
```
# --seed--
@ -64,28 +62,13 @@ assert(quote === 'Oliver says Meow!');
## --seed-contents--
```js
var catName;
var quote;
function catTalk() {
"use strict";
catName = "Oliver";
quote = catName + " says Meow!";
}
catTalk();
var catName = "Oliver";
var catSound = "Meow!";
```
# --solutions--
```js
let catName;
let quote;
function catTalk() {
'use strict';
catName = 'Oliver';
quote = catName + ' says Meow!';
}
catTalk();
let catName = "Oliver";
let catSound = "Meow!";
```

View File

@ -17,7 +17,7 @@ console.log("Alan Peter".length);
字符串 `10` 将会出现在控制台中。
例如,如果我们创建了一个变量 `var firstName = "Ada"`,我们可以通过使用 `firstName.length` 属性来获得字符串 `Ada` 的长度。
例如,如果我们创建了一个变量 `const firstName = "Ada"`,我们可以通过使用 `firstName.length` 找出字符串 `Ada` 的长度属性
# --instructions--
@ -29,8 +29,8 @@ console.log("Alan Peter".length);
```js
assert(
code.match(/var lastNameLength = 0;/) &&
code.match(/var lastName = "Lovelace";/)
code.match(/let lastNameLength = 0;/) &&
code.match(/const lastName = "Lovelace";/)
);
```
@ -52,18 +52,17 @@ assert(code.match(/=\s*lastName\.length/g) && !code.match(/lastName\s*=\s*8/));
```js
// Setup
var lastNameLength = 0;
var lastName = "Lovelace";
let lastNameLength = 0;
const lastName = "Lovelace";
// Only change code below this line
lastNameLength = lastName;
```
# --solutions--
```js
var lastNameLength = 0;
var lastName = "Lovelace";
let lastNameLength = 0;
const lastName = "Lovelace";
lastNameLength = lastName.length;
```

View File

@ -11,13 +11,13 @@ dashedName: global-scope-and-functions
在 JavaScript 中,<dfn>作用域</dfn>涉及到变量的作用范围。 在函数外定义的变量具有 <dfn>全局</dfn> 作用域。 这意味着,具有全局作用域的变量可以在代码的任何地方被调用。
这些没有使用 `var` 关键字定义的变量,会被自动创建`global` 作用域中,形成全局变量。 当在代码其他地方无意间定义了一个变量,刚好变量名与全局变量相同,这时会产生意想不到的后果。 因此你应该总是使`var` 关键字来声明你的变量。
使用 `let``const` 关键字声明的变量`global` 范围内自动创建。 当在代码其他地方无意间定义了一个变量,刚好变量名与全局变量相同,这时会产生意想不到的后果。 你应该总是用 `let``const` 声明你的变量。
# --instructions--
使用 `var`,在函数外声明一个全局变量 `myGlobal` 并给它一个初始值 `10`
使用 `let``const`,在任何函数外声明一个名为 `myGlobal` 的全局变量。 并给它一个初始值 `10`
在函数 `fun1` 的内部***不***使用 `var` 关键字,声明 `oopsGlobal`,并给它赋值为 `5`
在函数 `fun1` ***不*** 使用 `let``const` 关键字,将 `5` 分配给 `oopsGlobal`
# --hints--
@ -33,10 +33,10 @@ assert(typeof myGlobal != 'undefined');
assert(myGlobal === 10);
```
应使用 `var` 关键字定义 `myGlobal`
`myGlobal` 应该使用 `let``const` 关键字声明
```js
assert(/var\s+myGlobal/.test(code));
assert(/(let|const)\s+myGlobal/.test(code));
```
`oopsGlobal` 应为全局变量,值为 `5`
@ -109,7 +109,7 @@ function fun2() {
# --solutions--
```js
var myGlobal = 10;
const myGlobal = 10;
function fun1() {
oopsGlobal = 5;

View File

@ -14,13 +14,14 @@ dashedName: iterate-odd-numbers-with-a-for-loop
初始化 `i = 0`,当 `i < 10` 的时候继续循环。 `i += 2``i` 每次循环之后增加 2。
```js
var ourArray = [];
for (var i = 0; i < 10; i += 2) {
const ourArray = [];
for (let i = 0; i < 10; i += 2) {
ourArray.push(i);
}
```
循环结束后,`ourArray` 的值为 `[0,2,4,6,8]`。 改变计数器(`initialization` ,这样我们可以用奇数来递增。
`ourArray` 现在将包含 `[0, 2, 4, 6, 8]`。 改变计数器(`initialization` ,这样我们可以用奇数来递增。
# --instructions--
@ -34,7 +35,7 @@ for (var i = 0; i < 10; i += 2) {
assert(/for\s*\([^)]+?\)/.test(code));
```
`myArray` 应该等于 `[1,3,5,7,9]`
`myArray` 应该等于 `[1, 3, 5, 7, 9]`
```js
assert.deepEqual(myArray, [1, 3, 5, 7, 9]);
@ -52,16 +53,17 @@ if(typeof myArray !== "undefined"){(function(){return myArray;})();}
```js
// Setup
var myArray = [];
const myArray = [];
// Only change code below this line
```
# --solutions--
```js
var myArray = [];
for (var i = 1; i < 10; i += 2) {
const myArray = [];
for (let i = 1; i < 10; i += 2) {
myArray.push(i);
}
```

View File

@ -15,7 +15,7 @@ JavaScript 中最常见的循环就是 `for`,它可以循环指定次数。
for 循环中的可选三个表达式用分号隔开:
`for (a; b; c)`,其中 `a` 为初始化语句,`b` 是循环条件语句,`c`终止循环条件表达式。
`for (a; b; c)`,其中`a`为初始化语句,`b`条件语句,`c`最终的表达式。
初始化语句只会在执行循环开始之前执行一次。 它通常用于定义和设置你的循环变量。
@ -26,13 +26,14 @@ for 循环中的可选三个表达式用分号隔开:
在下面的例子中,先初始化 `i = 0`,条件 `i < 5` 为 true 时,进入循环。 每次循环后 `i` 的值增加 `1`,然后执行终止循环条件表达式 `i++`
```js
var ourArray = [];
for (var i = 0; i < 5; i++) {
const ourArray = [];
for (let i = 0; i < 5; i++) {
ourArray.push(i);
}
```
最终 `ourArray` 的值为 `[0,1,2,3,4]`.
`ourArray` 现在的值为 `[0, 1, 2, 3, 4]`
# --instructions--
@ -46,7 +47,7 @@ for (var i = 0; i < 5; i++) {
assert(/for\s*\([^)]+?\)/.test(code));
```
`myArray` 应该等于 `[1,2,3,4,5]`
`myArray` 应该等于 `[1, 2, 3, 4, 5]`
```js
assert.deepEqual(myArray, [1, 2, 3, 4, 5]);
@ -64,16 +65,17 @@ if (typeof myArray !== "undefined"){(function(){return myArray;})();}
```js
// Setup
var myArray = [];
const myArray = [];
// Only change code below this line
```
# --solutions--
```js
var myArray = [];
for (var i = 1; i < 6; i++) {
const myArray = [];
for (let i = 1; i < 6; i++) {
myArray.push(i);
}
```

View File

@ -14,9 +14,10 @@ dashedName: iterate-with-javascript-while-loops
我们将学习的第一种类型的循环称为 `while` 循环,当 while 指定的条件为真,循环才会执行,反之不执行。
```js
var ourArray = [];
var i = 0;
while(i < 5) {
const ourArray = [];
let i = 0;
while (i < 5) {
ourArray.push(i);
i++;
}
@ -38,7 +39,7 @@ while(i < 5) {
assert(code.match(/while/g));
```
`myArray` 应该等于 `[5,4,3,2,1,0]`
`myArray` 应该等于 `[5, 4, 3, 2, 1, 0]`
```js
assert.deepEqual(myArray, [5, 4, 3, 2, 1, 0]);
@ -56,17 +57,18 @@ if(typeof myArray !== "undefined"){(function(){return myArray;})();}
```js
// Setup
var myArray = [];
const myArray = [];
// Only change code below this line
```
# --solutions--
```js
var myArray = [];
var i = 5;
while(i >= 0) {
const myArray = [];
let i = 5;
while (i >= 0) {
myArray.push(i);
i--;
}

View File

@ -16,8 +16,8 @@ dashedName: manipulate-arrays-with-pop
数组中任何类型的元素(数值,字符串,甚至是数组)都可以被弹出来 。
```js
var threeArr = [1, 4, 6];
var oneDown = threeArr.pop();
const threeArr = [1, 4, 6];
const oneDown = threeArr.pop();
console.log(oneDown);
console.log(threeArr);
```
@ -26,7 +26,7 @@ console.log(threeArr);
# --instructions--
使用 `.pop()` 函数移除 `myArray`最后一个元素,并且把弹出的值赋给 `removedFromMyArray`
使用 `.pop()` 函数 `myArray`删除最后一项,并将取出的值分配给新变量 `removedFromMyArray`
# --hints--
@ -69,22 +69,22 @@ assert(
## --after-user-code--
```js
(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
if (typeof removedFromMyArray !== 'undefined') (function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
```
## --seed-contents--
```js
// Setup
var myArray = [["John", 23], ["cat", 2]];
const myArray = [["John", 23], ["cat", 2]];
// Only change code below this line
var removedFromMyArray;
```
# --solutions--
```js
var myArray = [["John", 23], ["cat", 2]];
var removedFromMyArray = myArray.pop();
const myArray = [["John", 23], ["cat", 2]];
const removedFromMyArray = myArray.pop();
```

View File

@ -16,15 +16,15 @@ dashedName: manipulate-arrays-with-shift
示例:
```js
var ourArray = ["Stimpson", "J", ["cat"]];
var removedFromOurArray = ourArray.shift();
const ourArray = ["Stimpson", "J", ["cat"]];
const removedFromOurArray = ourArray.shift();
```
`removedFromOurArray` 值为 `Stimpson``ourArray` 值为 `["J", ["cat"]]`
# --instructions--
使用 `.shift()` 函数移除 `myArray`第一项,并把移出的值赋给 `removedFromMyArray`
使用 `.shift()` 函数 `myArray`删除第一项,并将“移除的值”值分配给新变量 `removedFromMyArray`
# --hints--
@ -65,24 +65,24 @@ assert(
## --after-user-code--
```js
(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
if (typeof removedFromMyArray !== 'undefined') (function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
```
## --seed-contents--
```js
// Setup
var myArray = [["John", 23], ["dog", 3]];
const myArray = [["John", 23], ["dog", 3]];
// Only change code below this line
var removedFromMyArray;
```
# --solutions--
```js
var myArray = [["John", 23], ["dog", 3]];
const myArray = [["John", 23], ["dog", 3]];
// Only change code below this line
var removedFromMyArray = myArray.shift();
const removedFromMyArray = myArray.shift();
```

View File

@ -16,7 +16,7 @@ dashedName: manipulate-arrays-with-unshift
示例:
```js
var ourArray = ["Stimpson", "J", "cat"];
const ourArray = ["Stimpson", "J", "cat"];
ourArray.shift();
ourArray.unshift("Happy");
```
@ -25,7 +25,7 @@ ourArray.unshift("Happy");
# --instructions--
使用 `unshift()` 函数把 `["Paul",35]``myArray` 的头部
使用 `unshift()` `["Paul", 35]` 加到 `myArray` 变量的开头
# --hints--
@ -63,16 +63,17 @@ assert(
```js
// Setup
var myArray = [["John", 23], ["dog", 3]];
const myArray = [["John", 23], ["dog", 3]];
myArray.shift();
// Only change code below this line
```
# --solutions--
```js
var myArray = [["John", 23], ["dog", 3]];
const myArray = [["John", 23], ["dog", 3]];
myArray.shift();
myArray.unshift(["Paul", 35]);
```

View File

@ -9,12 +9,12 @@ dashedName: modify-array-data-with-indexes
# --description--
与字符串的数据不可变不同,数组的数据是可变的( <dfn>mutable</dfn>可以自由地改变
与字符串不同,数组的条目是 <dfn>可变的</dfn> 并且可以自由更改,即使数组是用 `const` 声明的
**示例**
```js
var ourArray = [50,40,30];
const ourArray = [50, 40, 30];
ourArray[0] = 15;
```
@ -28,7 +28,7 @@ ourArray[0] = 15;
# --hints--
`myArray` 应该等于 `[45,64,99]`
`myArray` 现在应该是 `[45, 64, 99]`
```js
assert(
@ -73,14 +73,15 @@ if(typeof myArray !== "undefined"){(function(){return myArray;})();}
```js
// Setup
var myArray = [18,64,99];
const myArray = [18, 64, 99];
// Only change code below this line
```
# --solutions--
```js
var myArray = [18,64,99];
const myArray = [18, 64, 99];
myArray[0] = 45;
```

View File

@ -12,11 +12,12 @@ dashedName: nesting-for-loops
如果你有一个二维数组,可以使用相同的逻辑,先遍历外面的数组,再遍历里面的子数组。 下面是一个例子:
```js
var arr = [
[1,2], [3,4], [5,6]
const arr = [
[1, 2], [3, 4], [5, 6]
];
for (var i=0; i < arr.length; i++) {
for (var j=0; j < arr[i].length; j++) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
console.log(arr[i][j]);
}
}
@ -30,13 +31,13 @@ for (var i=0; i < arr.length; i++) {
# --hints--
`multiplyAll([[1],[2],[3]])` 应该返回 `6`
`multiplyAll([[1], [2], [3]])` 应该返回 `6`
```js
assert(multiplyAll([[1], [2], [3]]) === 6);
```
`multiplyAll([[1,2],[3,4],[5,6,7]])` 应该返回 `5040`
`multiplyAll([[1, 2], [3, 4], [5, 6, 7]])` 应该返回 `5040`
```js
assert(
@ -48,7 +49,7 @@ assert(
);
```
`multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]])` 应该返回 `54`
`multiplyAll([[5, 1], [0.2, 4, 0.5], [3, 9]])` 应该返回 `54`
```js
assert(
@ -66,28 +67,26 @@ assert(
```js
function multiplyAll(arr) {
var product = 1;
let product = 1;
// Only change code below this line
// Only change code above this line
return product;
}
multiplyAll([[1,2],[3,4],[5,6,7]]);
multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);
```
# --solutions--
```js
function multiplyAll(arr) {
var product = 1;
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
let product = 1;
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
product *= arr[i][j];
}
}
return product;
}
multiplyAll([[1,2],[3,4],[5,6,7]]);
```

View File

@ -33,43 +33,43 @@ myFun();
# --hints--
`abTest(2,2)` 应该返回一个数字
`abTest(2, 2)` 应该返回一个数字
```js
assert(typeof abTest(2, 2) === 'number');
```
`abTest(2,2)` 应该返回 `8`
`abTest(2, 2)` 应该返回 `8`
```js
assert(abTest(2, 2) === 8);
```
`abTest(-2,2)` 应该返回 `undefined`
`abTest(-2, 2)` 应该返回 `undefined`
```js
assert(abTest(-2, 2) === undefined);
```
`abTest(2,-2)` 应该返回 `undefined`
`abTest(2, -2)` 应该返回 `undefined`
```js
assert(abTest(2, -2) === undefined);
```
`abTest(2,8)` 应该返回 `18`
`abTest(2, 8)` 应该返回 `18`
```js
assert(abTest(2, 8) === 18);
```
`abTest(3,3)` 应该返回 `12`
`abTest(3, 3)` 应该返回 `12`
```js
assert(abTest(3, 3) === 12);
```
`abTest(0,0)` 应该返回 `0`
`abTest(0, 0)` 应该返回 `0`
```js
assert(abTest(0, 0) === 0);

View File

@ -14,7 +14,7 @@ dashedName: returning-boolean-values-from-functions
有时人们通过 `if/else` 语句来做比较,像这样。
```js
function isEqual(a,b) {
function isEqual(a, b) {
if (a === b) {
return true;
} else {
@ -26,7 +26,7 @@ function isEqual(a,b) {
但有更好的方式来达到相同的效果。 既然 `===` 返回 `true``false` 我们可以直接返回比较结果:
```js
function isEqual(a,b) {
function isEqual(a, b) {
return a === b;
}
```
@ -37,13 +37,13 @@ function isEqual(a,b) {
# --hints--
`isLess(10,15)` 应该返回 `true`
`isLess(10, 15)` 应该返回 `true`
```js
assert(isLess(10, 15) === true);
```
`isLess(15,10)` 应该返回 `false`
`isLess(15, 10)` 应该返回 `false`
```js
assert(isLess(15, 10) === false);

View File

@ -13,13 +13,13 @@ dashedName: use-bracket-notation-to-find-the-first-character-in-a-string
大多数现代编程语言,如 JavaScript不同于人类从 1 开始计数。 它们是从 0 开始计数。 这被称为基于零(<dfn>Zero-based</dfn>)的索引。
例如,单词 `Charles` 的索引 0 的字符是 `C`。 所以 `var firstName = "Charles"`,你可以使用 `firstName[0]` 来获得第一个位置上的字符
例如,单词 `Charles` 的索引 0 的字符是 `C`。 所以如果 `const firstName = "Charles"`,你可以通过 `firstName[0]` 得到字符串第一个字母的值
示例:
```js
var firstName = "Charles";
var firstLetter = firstName[0];
const firstName = "Charles";
const firstLetter = firstName[0];
```
`firstLetter` 值为字符串 `C`
@ -56,8 +56,8 @@ assert(code.match(/firstLetterOfLastName\s*?=\s*?lastName\[.*?\]/));
```js
// Setup
var firstLetterOfLastName = "";
var lastName = "Lovelace";
let firstLetterOfLastName = "";
const lastName = "Lovelace";
// Only change code below this line
firstLetterOfLastName = lastName; // Change this line
@ -66,8 +66,8 @@ firstLetterOfLastName = lastName; // Change this line
# --solutions--
```js
var firstLetterOfLastName = "";
var lastName = "Lovelace";
let firstLetterOfLastName = "";
const lastName = "Lovelace";
// Only change code below this line
firstLetterOfLastName = lastName[0];

View File

@ -11,13 +11,13 @@ dashedName: use-bracket-notation-to-find-the-last-character-in-a-string
要获取字符串的最后一个字符,可以用字符串的长度减 1 的索引值。
例如,如果 `var firstName = "Ada"` 中,那么你可以通过 `firstName[firstName.length - 1]` 来得到字符串最后一个字
例如,如果 `const firstName = "Ada"`,则可以使用 `firstName[firstName.length - 1]` 获取字符串最后一个字母的值
示例:
```js
var firstName = "Ada";
var lastLetter = firstName[firstName.length - 1];
const firstName = "Ada";
const lastLetter = firstName[firstName.length - 1];
```
`lastLetter` 值为字符串 `a`
@ -54,15 +54,15 @@ assert(code.match(/\.length/g).length > 0);
```js
// Setup
var lastName = "Lovelace";
const lastName = "Lovelace";
// Only change code below this line
var lastLetterOfLastName = lastName; // Change this line
const lastLetterOfLastName = lastName; // Change this line
```
# --solutions--
```js
var lastName = "Lovelace";
var lastLetterOfLastName = lastName[lastName.length - 1];
const lastName = "Lovelace";
const lastLetterOfLastName = lastName[lastName.length - 1];
```

View File

@ -11,13 +11,13 @@ dashedName: use-bracket-notation-to-find-the-nth-to-last-character-in-a-string
我们既可以获取字符串的最后一个字符,也可以用获取字符串的倒数第 N 个字符。
例如,你可以通过 `firstName[firstName.length - 3]` 来获得 `var firstName = "Augusta"` 字符串的倒数第三个字符。
例如,你可以使用 `firstName[firstName.length - 3]` 获取 `const firstName = "Augusta"` 字符串的倒数第三个字母的值
例如:
```js
var firstName = "Augusta";
var thirdToLastLetter = firstName[firstName.length - 3];
const firstName = "Augusta";
const thirdToLastLetter = firstName[firstName.length - 3];
```
`thirdToLastLetter` 的值应该为字符串 `s`
@ -54,15 +54,15 @@ assert(code.match(/\.length/g).length > 0);
```js
// Setup
var lastName = "Lovelace";
const lastName = "Lovelace";
// Only change code below this line
var secondToLastLetterOfLastName = lastName; // Change this line
const secondToLastLetterOfLastName = lastName; // Change this line
```
# --solutions--
```js
var lastName = "Lovelace";
var secondToLastLetterOfLastName = lastName[lastName.length - 2];
const lastName = "Lovelace";
const secondToLastLetterOfLastName = lastName[lastName.length - 2];
```

View File

@ -9,7 +9,7 @@ dashedName: use-conditional-logic-with-if-statements
# --description--
`If` 语句用于在代码中做条件判断。 关键字 `if` 告诉 JavaScript 在小括号中的条件为真的情况下去执行定义在大括号里面的代码。 这种条件被称为 `Boolean` 条件,因为他们只可能是 `true`(真)或 `false`(假)。
`if` 语句用于在代码中做出决定。 关键字 `if` 告诉 JavaScript 在小括号中的条件为真的情况下去执行定义在大括号里面的代码。 这种条件被称为 `Boolean` 条件,因为他们只可能是 `true`(真)或 `false`(假)。
当条件的计算结果为 `true`,程序执行大括号内的语句。 当布尔条件的计算结果为 `false`,大括号内的代码将不会执行。
@ -22,10 +22,11 @@ dashedName: use-conditional-logic-with-if-statements
```js
function test (myCondition) {
if (myCondition) {
return "It was true";
return "It was true";
}
return "It was false";
}
test(true);
test(false);
```

View File

@ -8,11 +8,13 @@ dashedName: compare-scopes-of-the-var-and-let-keywords
# --description--
使用 `var` 关键字来声明一个变量的时候,这个变量会被声明成全局变量,或是函数内的局部变量
如果你不熟悉 `let`,请查看 [这个挑战](/learn/javascript-algorithms-and-data-structures/basic-javascript/explore-differences-between-the-var-and-let-keywords)
`let` 关键字的作用与此类似,但会有一些额外的特性。 如果在代码块、语句或表达式中使用关键字 `let` 声明变量,这个变量的作用域就被限制在当前的代码块、语句或表达式之中
使用 `var` 关键字声明变量时,它是全局声明的,如果在函数内部声明则是局部声明的
举个例子:
`let` 关键字的行为类似,但有一些额外的功能。 在代码块、语句或表达式中使用 `let` 关键字声明变量时,其作用域仅限于该代码块、语句或表达式。
例如:
```js
var numArray = [];
@ -23,9 +25,9 @@ console.log(numArray);
console.log(i);
```
这里控制台将显示值 `[0, 1, 2]``3`
此处控制台将显示值 `[0, 1, 2]``3`
因为使用 `var` 关键字,`i` 被声明为全局变量。 所以当 `i++` 执行时,它会更新全局变量。 这个代码和下方的代码类似
使用 `var` 关键字,`i` 是全局声明的。 所以当 `i++` 执行时,它会更新全局变量。 此代码类似于以下内容
```js
var numArray = [];
@ -37,9 +39,9 @@ console.log(numArray);
console.log(i);
```
这里控制台将显示值 `[0, 1, 2]``3`
此处控制台将显示值 `[0, 1, 2]``3`
如果你创建一个函数,将它存储起来,稍后在使用 `i` 变量的 `for` 循环中使用。这么做可能会出现问题。 这是因为存储的函数会总是指向更新后的全局 `i` 变量的值。
如果你创建一个函数,将它存储起来,稍后在使用 `i` 变量的 `for` 循环中使用。这么做可能会出现问题。 这是因为存储的函数将始终引用更新后的全局 `i` 变量的值。
```js
var printNumTwo;
@ -53,9 +55,9 @@ for (var i = 0; i < 3; i++) {
console.log(printNumTwo());
```
这里控制台将显示值 `3`
此处控制台将显示值 `3`
可以看到,`printNumTwo()` 打印了 3而不是 2。 这是因为赋值给 `i` 的值已经更新,`printNumTwo()` 返回全局的 `i`,而不是在 for 循环中创建函数时 `i` 的值。 `let` 关键字就不会出现这种现象:
可以看到,`printNumTwo()` 打印了 3 而不是 2。 这是因为赋值给 `i` 的值已经更新,`printNumTwo()` 返回全局的 `i`,而不是在 for 循环中创建函数时 `i` 的值。 `let` 关键字就不会出现这种现象:
```js
let printNumTwo;
@ -72,7 +74,7 @@ console.log(i);
在这里控制台将显示值 `2` 和一个错误提示 `i is not defined`
`i` 未定义,因为它没有在全局范围内声明。 它只在 `for` 循环语句中被声明。 `printNumTwo()` 返回了正确的值,因为 `let` 关键字在循环语句中使 `i` 变量产生了三个不同的值(分别为 0、1、2
`i` 未定义,因为它没有在全局范围内声明。 它只在 `for` 循环语句中被声明。 `printNumTwo()` 返回了正确的值,因为 `let` 关键字创建了三个具有唯一值0、1 和 2的不同 `i` 变量在循环语句中。
# --instructions--

View File

@ -8,11 +8,13 @@ dashedName: mutate-an-array-declared-with-const
# --description--
在现代的 JavaScript 里,`const` 声明有很多用法
如果你不熟悉 `const`,请查看[这个挑战](/learn/javascript-algorithms-and-data-structures/basic-javascript/declare-a-read-only-variable-with-the-const-keyword)
一些开发者倾向于默认使用 `const` 声明所有变量,除非他们打算后续重新给变量赋值, 那么他们在声明的时候就会用 `let`
`const` 声明在现代 JavaScript 中有很多用例
然而,你要注意,对象(包括数组和函数)在使用 `const` 声明的时候依然是可变的。 使用 `const` 来声明只会保证变量不会被重新赋值
默认情况下,一些开发人员更喜欢使用 `const` 分配所有变量,除非他们知道需要重新分配值。 只有在这种情况下,他们才使用 `let`
但是,重要的是要了解使用 `const` 分配给变量的对象(包括数组和函数)仍然是可变的。 使用 `const` 声明只能防止变量标识符的重新分配。
```js
const s = [5, 6, 7];
@ -21,13 +23,13 @@ s[2] = 45;
console.log(s);
```
`s = [1, 2, 3]` 导致一个错误。 `console.log` 显示值 `[5, 6, 45]`
`s = [1, 2, 3]` 导致错误。 `console.log` 显示值 `[5, 6, 45]`
可以发现,你可以改变对象 `[5, 6, 7]` 本身,变量 `s` 会指向改变后的数组 `[5, 6, 45]`所有数组一样,数组 `s` 中的元素是可以被改变的,但是因为使用了 `const` 关键字,你不能使用赋值操作符将变量标识 `s` 指向另外一个数组。
如你所见,你可以改变对象 `[5, 6, 7]` 本身,变量 `s` 仍将指向更改后的数组 `[5, 6, 45]`所有数组一样,`s` 中的数组元素是可变的,但是因为使用了 `const`,所以不能使用变量标识 `s` 指向一个使用赋值运算符的不同数组。
# --instructions--
这里有一个使用 `const s = [5, 7, 2]` 声明的数组。 使用对各元素赋值的方法将数组改成 `[2, 5, 7]`
数组声明为 `const s = [5, 7, 2]`。 使用对各元素赋值的方法将数组改成 `[2, 5, 7]`
# --hints--
@ -37,7 +39,7 @@ console.log(s);
(getUserInput) => assert(getUserInput('index').match(/const/g));
```
`s` 应该为常量(使用 `const`)。
`s` 应该是一个常量变量(通过使用 `const`)。
```js
(getUserInput) => assert(getUserInput('index').match(/const\s+s/g));

View File

@ -10,7 +10,7 @@ dashedName: refactor-global-variables-out-of-functions
目前为止,我们已经看到了函数式编程的两个原则:
1) 不要更改变量或对象 - 创建新变量和对象,并在需要时从函数返回它们。 提示:使用类似 `var newArr = arrVar` `arrVar` 是一个数组,代码只是创建一个对现有变量的引用,而不是副本。 所以更改 `newArr` 中的值会同时更改 `arrVar` 中的值。
1) 不要更改变量或对象 - 创建新变量和对象,并在需要时从函数返回它们。 提示:使用类似 `const newArr = arrVar` 的东西,其中 `arrVar` 是一个数组,只会创建对现有变量的引用,而不是副本。 所以更改 `newArr` 中的值会同时更改 `arrVar` 中的值。
2) 声明函数参数 - 函数内的任何计算仅取决于参数,而不取决于任何全局对象或变量。
@ -86,7 +86,7 @@ assert(
```js
// The global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
const bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
// Change code below this line
function add (bookName) {
@ -99,7 +99,7 @@ function add (bookName) {
// Change code below this line
function remove (bookName) {
var book_index = bookList.indexOf(bookName);
const book_index = bookList.indexOf(bookName);
if (book_index >= 0) {
bookList.splice(book_index, 1);
@ -109,9 +109,9 @@ function remove (bookName) {
}
}
var newBookList = add(bookList, 'A Brief History of Time');
var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
const newBookList = add(bookList, 'A Brief History of Time');
const newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
const newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
console.log(bookList);
```
@ -120,13 +120,13 @@ console.log(bookList);
```js
// The global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
const bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
function add (bookList, bookName) {
function add(bookList, bookName) {
return [...bookList, bookName];
}
function remove (bookList, bookName) {
function remove(bookList, bookName) {
const bookListCopy = [...bookList];
const bookNameIndex = bookList.indexOf(bookName);
if (bookNameIndex >= 0) {
@ -135,7 +135,7 @@ function remove (bookList, bookName) {
return bookListCopy;
}
var newBookList = add(bookList, 'A Brief History of Time');
var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
const newBookList = add(bookList, 'A Brief History of Time');
const newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
const newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
```

View File

@ -55,7 +55,7 @@ assert(code.match(/\s*\.\s*filter/g));
assert(!code.match(/for\s*?\([\s\S]*?\)/g));
```
`filteredList` 应等于 `[{"title": "Inception","rating": "8.8"},{"title": "Interstellar","rating": "8.6"},{"title": "The Dark Knight","rating": "9.0"},{"title": "Batman Begins","rating": "8.3"}]`
`filteredList`等于 `[{"title": "Inception", "rating": "8.8"}, {"title": "Interstellar", "rating": "8.6"}, {"title": "The Dark Knight", "rating": "9.0"}, {"title": "Batman Begins", "rating": "8.3"}]`
```js
assert.deepEqual(filteredList, [
@ -72,7 +72,7 @@ assert.deepEqual(filteredList, [
```js
// The global variable
var watchList = [
const watchList = [
{
"Title": "Inception",
"Year": "2010",
@ -187,7 +187,7 @@ var watchList = [
// Only change code below this line
var filteredList;
const filteredList = "";
// Only change code above this line
@ -197,8 +197,7 @@ console.log(filteredList);
# --solutions--
```js
// The global variable
var watchList = [
const watchList = [
{
"Title": "Inception",
"Year": "2010",
@ -311,7 +310,5 @@ var watchList = [
}
];
// Only change code below this line
let filteredList = watchList.filter(e => e.imdbRating >= 8).map( ({Title: title, imdbRating: rating}) => ({title, rating}) );
// Only change code above this line
const filteredList = watchList.filter(e => e.imdbRating >= 8).map( ({Title: title, imdbRating: rating}) => ({title, rating}) );
```

View File

@ -61,7 +61,7 @@ assert(!code.match(/for\s*?\([\s\S]*?\)/));
assert(code.match(/\.map/g));
```
`ratings` 应等于 `[{"title":"Inception","rating":"8.8"},{"title":"Interstellar","rating":"8.6"},{"title":"The Dark Knight","rating":"9.0"},{"title":"Batman Begins","rating":"8.3"},{"title":"Avatar","rating":"7.9"}]`
`ratings`等于 `[{"title": "Inception", "rating": "8.8"}, {"title": "Interstellar", "rating": "8.6"}, {"title": "The Dark Knight", "rating": "9.0"},{"title": "Batman Begins", "rating": "8.3"}, {"title": "Avatar", "rating": "7.9"}]`
```js
assert.deepEqual(ratings, [
@ -79,7 +79,7 @@ assert.deepEqual(ratings, [
```js
// The global variable
var watchList = [
const watchList = [
{
"Title": "Inception",
"Year": "2010",
@ -194,9 +194,9 @@ var watchList = [
// Only change code below this line
var ratings = [];
for(var i=0; i < watchList.length; i++){
ratings.push({title: watchList[i]["Title"], rating: watchList[i]["imdbRating"]});
const ratings = [];
for (let i = 0; i < watchList.length; i++) {
ratings.push({title: watchList[i]["Title"], rating: watchList[i]["imdbRating"]});
}
// Only change code above this line
@ -207,8 +207,7 @@ console.log(JSON.stringify(ratings));
# --solutions--
```js
// The global variable
var watchList = [
const watchList = [
{
"Title": "Inception",
"Year": "2010",
@ -321,7 +320,7 @@ var watchList = [
}
];
var ratings = watchList.map(function(movie) {
const ratings = watchList.map(function(movie) {
return {
title: movie["Title"],
rating: movie["imdbRating"]