-
```js
const LOCAL_FORECAST = {
- today: { min: 72, max: 83 },
- tomorrow: { min: 73.3, max: 84.6 }
+ yesterday: { low: 61, high: 75 },
+ today: { low: 64, high: 77 },
+ tomorrow: { low: 68, high: 80 }
};
-function getMaxOfTmrw(forecast) {
- "use strict";
- // change code below this line
- const maxOfTomorrow = undefined; // change this line
- // change code above this line
- return maxOfTomorrow;
-}
+// change code below this line
+
+const lowToday = LOCAL_FORECAST.today.low;
+const highToday = LOCAL_FORECAST.today.high;
-console.log(getMaxOfTmrw(LOCAL_FORECAST)); // should be 84.6
+// change code above this line
+console.log(lowToday); // should be 64
+console.log(highToday); // should be 77
```
-
-
-
## Solution
```yml
tests:
- - text: stats
应该是一个object
。
- testString: 'assert(typeof stats === "object", "stats
should be an object
.");'
- - text: half(stats)
应为28.015
- testString: 'assert(half(stats) === 28.015, "half(stats)
should be 28.015
");'
- - text: 使用了解构。
- testString: 'getUserInput => assert(getUserInput("index").match(/\(\s*\{\s*\w+\s*,\s*\w+\s*\}\s*\)/g), "Destructuring was used.");'
+ - text: stats
的类型应该是一个object
。
+ testString: assert(typeof stats === 'object');
+ - text: half(stats)
应该等于28.015
+ testString: assert(half(stats) === 28.015);
+ - text: 应该使用解构赋值。
+ testString: assert(code.replace(/\s/g, '').match(/half=\({\w+,\w+}\)/));
+ - text: 应该使用解构参数。
+ testString: assert(!code.match(/stats\.max|stats\.min/));
```
@@ -42,20 +67,13 @@ const stats = {
min: -0.75,
average: 35.85
};
-const half = (function() {
- "use strict"; // do not change this line
- // change code below this line
- return function half(stats) {
- // use function argument destructuring
- return (stats.max + stats.min) / 2.0;
- };
- // change code above this line
+// change code below this line
+const half = (stats) => (stats.max + stats.min) / 2.0; // use function argument destructuring
+// change code above this line
-})();
console.log(stats); // should be object
console.log(half(stats)); // should be 28.015
-
```
@@ -68,6 +86,16 @@ console.log(half(stats)); // should be 28.015
```js
-// solution required
+const stats = {
+ max: 56.78,
+ standard_deviation: 4.34,
+ median: 34.54,
+ mode: 23.87,
+ min: -0.75,
+ average: 35.85
+};
+
+const half = ( {max, min} ) => (max + min) / 2.0;
```
+
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements.chinese.md
deleted file mode 100644
index e967e43217..0000000000
--- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements.chinese.md
+++ /dev/null
@@ -1,63 +0,0 @@
----
-id: 587d7b8a367417b2b2512b4c
-title: Use Destructuring Assignment with the Rest Operator to Reassign Array Elements
-challengeType: 1
-videoUrl: ''
-localeTitle: 使用与Rest运算符的Destructuring Assignment重新分配数组元素
----
-
-## Description
-在某些涉及数组解构的情况下,我们可能希望将其余元素收集到一个单独的数组中。结果类似于Array.prototype.slice()
,如下所示: const [a,b,... arr] = [1,2,3,4,5,7];
console.log(a,b); // 1,2
的console.log(ARR); // [3,4,5,7]
变量a
和b
从数组中获取第一个和第二个值。之后,由于rest操作符的存在, arr
以数组的形式获取其余的值。 rest元素仅作为列表中的最后一个变量正常工作。在中,您不能使用rest运算符来捕获一个子数组,该子数组会遗漏原始数组的最后一个元素。
-
-## Instructions
-使用与rest运算符的解构赋值来执行有效的Array.prototype.slice()
以便arr
是原始数组source
的子数组,省略前两个元素。
-
-## Tests
-
-
-```yml
-tests:
- - text: 'arr
应为[3,4,5,6,7,8,9,10]
'
- testString: 'assert(arr.every((v, i) => v === i + 3) && arr.length === 8,"arr
should be [3,4,5,6,7,8,9,10]
");'
- - text: 应该使用解构。
- testString: 'getUserInput => assert(getUserInput("index").match(/\[\s*\w*\s*,\s*\w*\s*,\s*...\w+\s*\]/g),"Destructuring should be used.");'
- - text: 不应使用Array.slice()
。
- testString: 'getUserInput => assert(!getUserInput("index").match(/slice/g), "Array.slice()
should not be used.");'
-
-```
-
-
-
-## Challenge Seed
-
-
-
-
-```js
-const source = [1,2,3,4,5,6,7,8,9,10];
-function removeFirstTwo(list) {
- "use strict";
- // change code below this line
- arr = list; // change this
- // change code above this line
- return arr;
-}
-const arr = removeFirstTwo(source);
-console.log(arr); // should be [3,4,5,6,7,8,9,10]
-console.log(source); // should be [1,2,3,4,5,6,7,8,9,10];
-
-```
-
-
-
-
-
-
-
-## Solution
-
-
-```js
-// solution required
-```
-
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.chinese.md
new file mode 100644
index 0000000000..4011491be3
--- /dev/null
+++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.chinese.md
@@ -0,0 +1,85 @@
+---
+id: 587d7b8a367417b2b2512b4c
+title: Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements
+challengeType: 1
+forumTopicId: 301218
+localeTitle: 使用解构赋值配合 rest 操作符来重新分配数组元素
+---
+
+## Description
+
+在解构数组的某些情况下,我们可能希望将剩下的元素放进另一个数组里面。
+以下代码的结果与使用Array.prototype.slice()
相同:
+
+```js
+const [a, b, ...arr] = [1, 2, 3, 4, 5, 7];
+console.log(a, b); // 1, 2
+console.log(arr); // [3, 4, 5, 7]
+```
+
+变量a
与b
分别获取了数组的前两个元素的值。之后,因为rest
操作符的存在,arr
获取了原数组剩余的元素的值,并构成了一个新的数组。
+rest
操作只能对数组列表最后的元素起作用。这意味着你不能使用rest
操作符来截取原数组中间元素的子数组。
+
+
+## Instructions
+
+使用解构赋值以及rest
操作符来进行一个Array.prototype.slice
相同的操作。使得arr
是原数组source
除开前两个元素的子数组。
+
+
+## Tests
+
+
+```yml
+tests:
+ - text: arr
应该为[3,4,5,6,7,8,9,10]
+ testString: assert(arr.every((v, i) => v === i + 3) && arr.length === 8);
+ - text: 没有使用Array.slice()
。
+ testString: getUserInput => assert(!getUserInput('index').match(/slice/g));
+ - text: 使用了解构赋值。
+ testString: assert(code.replace(/\s/g, '').match(/\[(([_$a-z]\w*)?,){1,}\.\.\.arr\]=list/i));
+
+```
+
+
+
+## Challenge Seed
+
+
+
+
+```js
+const source = [1,2,3,4,5,6,7,8,9,10];
+function removeFirstTwo(list) {
+ "use strict";
+ // change code below this line
+ const arr = list; // change this
+ // change code above this line
+ return arr;
+}
+const arr = removeFirstTwo(source);
+console.log(arr); // should be [3,4,5,6,7,8,9,10]
+console.log(source); // should be [1,2,3,4,5,6,7,8,9,10];
+```
+
+
+
+
+
+
+
+## Solution
+
+
+```js
+const source = [1,2,3,4,5,6,7,8,9,10];
+function removeFirstTwo(list) {
+ "use strict";
+ // change code below this line
+ const [, , ...arr] = list;
+ // change code above this line
+ return arr;
+}
+const arr = removeFirstTwo(source);
+```
+
+
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-export-to-reuse-a-code-block.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-export-to-reuse-a-code-block.chinese.md
deleted file mode 100644
index 80ba6c3a0f..0000000000
--- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-export-to-reuse-a-code-block.chinese.md
+++ /dev/null
@@ -1,62 +0,0 @@
----
-id: 587d7b8c367417b2b2512b56
-title: Use export to Reuse a Code Block
-challengeType: 1
-videoUrl: ''
-localeTitle: 使用export重用代码块
----
-
-## Description
-在之前的挑战中,您了解了import
以及如何利用它从大型文件导入少量代码。但是,为了使其工作,我们必须使用import
一个语句,称为导出 。当我们想要一些代码 - 一个函数或一个变量 - 可以在另一个文件中使用时,我们必须将其导出才能将其导入另一个文件。与import
一样, export
是非浏览器功能。以下是我们称为命名导出的内容 。有了这个,我们可以使用您在上一课中学到的import
语法将我们导出的任何代码导入到另一个文件中。这是一个例子: const capitalizeString =(string)=> {
return string.charAt(0).toUpperCase()+ string.slice(1);
}
export {capitalizeString} //如何导出函数。
export const foo =“bar”; //如何导出变量
或者,如果您想将所有export
语句压缩成一行,则可以采用以下方法: const capitalizeString =(string)=> {
return string.charAt(0).toUpperCase()+ string.slice(1);
}
const foo =“bar”;
export {capitalizeString,foo}
两种方法都是完全可以接受的。
-
-## Instructions
-下面是我想让其他文件使用的两个变量。利用我演示export
的第一种方式,导出两个变量。
-
-## Tests
-
-
-```yml
-tests:
- - text: foo
被导出了。
- testString: 'getUserInput => assert(getUserInput("index").match(/export\s+const\s+foo\s*=\s*"bar"/g), "foo
is exported.");'
- - text: bar
出口。
- testString: 'getUserInput => assert(getUserInput("index").match(/export\s+const\s+bar\s*=\s*"foo"/g), "bar
is exported.");'
-
-```
-
-
-
-## Challenge Seed
-
-
-
-
-```js
-"use strict";
-const foo = "bar";
-const bar = "foo";
-
-```
-
-
-
-### Before Test
-
-
-```js
-window.exports = function(){};
-
-```
-
-
-
-
-
-
-## Solution
-
-
-```js
-// solution required
-```
-
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-export-to-share-a-code-block.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-export-to-share-a-code-block.chinese.md
new file mode 100644
index 0000000000..6552bf9e15
--- /dev/null
+++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-export-to-share-a-code-block.chinese.md
@@ -0,0 +1,86 @@
+---
+id: 587d7b8c367417b2b2512b56
+title: Use export to Share a Code Block
+challengeType: 1
+forumTopicId: 301219
+localeTitle: 用 export 来重用代码块
+---
+
+## Description
+
+假设有一个文件 math_functions.js
,该文件包含了数学运算相关的一些函数。其中一个存储在变量 add
里,该函数接受两个数字做为参数返回它们的和。如果想在其它不同的 JavaScript 文件里使用这个函数,就需要 export
它。
+
+```js
+export const add = (x, y) => {
+ return x + y;
+}
+```
+
+上面是导出单个函数常用方法,还可以这样导出:
+
+```js
+const add = (x, y) => {
+ return x + y;
+}
+
+export { add };
+```
+
+导出变量和函数后,就可以在其它文件里导入使用从而避免了代码冗余。重复第一个例子的代码可以导出多个对象或函数,在第二个例子里面的导出语句中添加更多值也可以导出多项,例子如下:
+
+```js
+export { add, subtract };
+```
+
+
+
+## Instructions
+
+下面有两个变量需要在别的文件中可以使用。利用刚才展示的第一种方式,导出两个变量。
+
+
+## Tests
+
+
+```yml
+tests:
+ - text: 应该导出uppercaseString
变量。
+ testString: assert(code.match(/(export\s+const\s+uppercaseString|export\s*{\s*(uppercaseString[^}]*|[^,]*,\s*uppercaseString\s*)})/g));
+ - text: 应该导出lowercaseString
变量。
+ testString: assert(code.match(/(export\s+const\s+lowercaseString|export\s*{\s*(lowercaseString[^}]*|[^,]*,\s*lowercaseString\s*)})/g));
+
+```
+
+
+
+## Challenge Seed
+
+
+
+```js
+const uppercaseString = (string) => {
+ return string.toUpperCase();
+}
+
+const lowercaseString = (string) => {
+ return string.toLowerCase()
+}
+```
+
+
+
+
+## Solution
+
+
+```js
+export const uppercaseString = (string) => {
+ return string.toUpperCase();
+}
+
+export const lowercaseString = (string) => {
+ return string.toLowerCase()
+}
+```
+
+
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object.chinese.md
index 99479073a6..96aa16d297 100644
--- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object.chinese.md
+++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object.chinese.md
@@ -2,27 +2,70 @@
id: 587d7b8c367417b2b2512b54
title: Use getters and setters to Control Access to an Object
challengeType: 1
-videoUrl: ''
-localeTitle: 使用getter和setter来控制对象的访问
+forumTopicId: 301220
+localeTitle: 使用 getter 和 setter 来控制对象的访问
---
## Description
-您可以从对象获取值,并在对象中设置属性的值。这些通常被称为getter和setter 。 Getter函数旨在简单地将对象的私有变量的值返回(获取)给用户,而无需用户直接访问私有变量。 Setter函数用于根据传递给setter函数的值修改(设置)对象私有变量的值。此更改可能涉及计算,甚至完全覆盖先前的值。 class Book {
构造函数(作者){
this._author = author;
}
// getter
get writer(){
return this._author;
}
// setter
set writer(updatedAuthor){
this._author = updatedAuthor;
}
}
const lol = new Book('anonymous');
的console.log(lol.writer); //匿名
lol.writer ='wut';
的console.log(lol.writer); //哇
注意我们用来调用getter和setter的语法 - 就好像它们甚至不是函数一样。 Getter和setter很重要,因为它们隐藏了内部实现细节。
+
+你可以从对象中获得一个值,也可以给对象的属性赋值。
+这些通常行为被称为 getters 以及 setters。
+Getter 函数的作用是可以让对象返回一个私有变量,而不需要直接去访问私有变量。
+Setter 函数的作用是可以基于传进的参数来修改对象中私有变量。这些修改可以是计算,或者是直接替换之前的值。
+
+```js
+class Book {
+ constructor(author) {
+ this._author = author;
+ }
+ // getter
+ get writer() {
+ return this._author;
+ }
+ // setter
+ set writer(updatedAuthor) {
+ this._author = updatedAuthor;
+ }
+}
+const lol = new Book('anonymous');
+console.log(lol.writer); // anonymous
+lol.writer = 'wut';
+console.log(lol.writer); // wut
+```
+
+注意我们调用 getter 和 setter 的语法,它们看起来并不像一个函数调用。
+Getter 和 Setter 非常重要,因为它们隐藏了内部的实现细节。
+
+注意: 通常会在私有变量前添加下划线(_
)。这里并没有真正意义上让变量私有。
+
## Instructions
-使用class
关键字创建Thermostat类。构造函数接受华氏温度。现在在类中创建getter
和setter
,以获得摄氏温度。请记住, C = 5/9 * (F - 32)
和F = C * 9.0 / 5 + 32
,其中F是华氏温标的温度值,C是摄氏温度相同温度的值注意当你实现这一点,你将在一个等级中跟踪班级内的温度 - 华氏温度或摄氏温度。这是getter或setter的强大功能 - 您正在为另一个用户创建一个API,无论您追踪哪个用户,都可以获得正确的结果。换句话说,您正在从使用者那里抽象出实现细节。
+
+使用class
关键字来创建Thermostat
类,它的构造函数应该可以接收 fahrenheit(华氏温度)作为参数。
+在类中创建 temperature 的 getter
和setter
,将温度转换成摄氏温度。
+温度转换的公式是C = 5/9 * (F - 32)
以及F = C * 9.0 / 5 + 32
,F 代表华氏温度,C 代表摄氏温度。
+注意: 当你实现这个作业的时候,你应当在类中使用一个温度标准,无论是华氏温度还是摄氏温度。
+是时候展现 getter 和 setter 的威力了——无论你的 API 内部使用的是哪种温度标准,用户都能得到正确的结果。
+或者说,你从用户需求中抽象出了实现细节。
+
## Tests
```yml
tests:
- - text: Thermostat
应该是一个class
具有限定constructor
方法。
- testString: 'assert(typeof Thermostat === "function" && typeof Thermostat.constructor === "function","Thermostat
should be a class
with a defined constructor
method.");'
- - text: class
关键字。
- testString: 'getUserInput => assert(getUserInput("index").match(/class/g),"class
keyword was used.");'
- - text: Thermostat
可以实例化。
- testString: 'assert(() => {const t = new Thermostat(32); return typeof t === "object" && t.temperature === 0;}, "Thermostat
can be instantiated.");'
+ - text: Thermostat
应该是一个class
,并且在其中定义了constructor
方法。
+ testString: assert(typeof Thermostat === 'function' && typeof Thermostat.constructor === 'function');
+ - text: 应该使用 class
关键字。
+ testString: assert(code.match(/class/g));
+ - text: Thermostat
应该可以被实例化。
+ testString: assert((() => {const t = new Thermostat(32);return typeof t === 'object' && t.temperature === 0;})());
+ - text: 应该定义一个 getter
。
+ testString: assert((() => {const desc = Object.getOwnPropertyDescriptor(Thermostat.prototype, 'temperature');return !!desc && typeof desc.get === 'function';})());
+ - text: 应该定义一个 setter
。
+ testString: assert((() => {const desc = Object.getOwnPropertyDescriptor(Thermostat.prototype, 'temperature');return !!desc && typeof desc.set === 'function';})());
+ - text: 调用 setter
应该设置 temperature。
+ testString: assert((() => {const t = new Thermostat(32); t.temperature = 26;return t.temperature !== 0;})());
```
@@ -34,19 +77,14 @@ tests:
```js
-function makeClass() {
- "use strict";
- /* Alter code below this line */
+/* Alter code below this line */
+
+/* Alter code above this line */
- /* Alter code above this line */
- return Thermostat;
-}
-const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C
-
```
@@ -59,6 +97,25 @@ temp = thermos.temperature; // 26 in C
```js
-// solution required
+
+/* Alter code below this line */
+class Thermostat {
+ constructor(fahrenheit) {
+ this._tempInCelsius = 5/9 * (fahrenheit - 32);
+ }
+ get temperature(){
+ return this._tempInCelsius;
+ }
+ set temperature(newTemp){
+ this._tempInCelsius = newTemp;
+ }
+}
+/* Alter code above this line */
+
+const thermos = new Thermostat(76); // setting in Fahrenheit scale
+let temp = thermos.temperature; // 24.44 in C
+thermos.temperature = 26;
+temp = thermos.temperature; // 26 in C
```
+
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters.chinese.md
deleted file mode 100644
index 7d020cde2b..0000000000
--- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters.chinese.md
+++ /dev/null
@@ -1,64 +0,0 @@
----
-id: 587d7b88367417b2b2512b47
-title: Use the Rest Operator with Function Parameters
-challengeType: 1
-videoUrl: ''
-localeTitle: 将Rest运算符与函数参数一起使用
----
-
-## Description
-为了帮助我们创建更灵活的函数,ES6引入了函数参数的rest运算符 。使用rest运算符,您可以创建带有可变数量参数的函数。这些参数存储在一个数组中,以后可以从函数内部访问。看看这段代码: function howMany(... args){
返回“你已经通过”+ args.length +“arguments。”;
}
console.log(howMany(0,1,2)); //你已经传递了3个参数
console.log(howMany(“string”,null,[1,2,3],{})); //你已经传递了4个参数。
其余运算符无需检查args
数组,并允许我们在args
数组上应用map()
, filter()
和reduce()
。
-
-## Instructions
-修改函数sum
,使其使用rest运算符,并以相同的方式使用任意数量的参数。
-
-## Tests
-
-
-```yml
-tests:
- - text: 'sum(0,1,2)
的结果应为3'
- testString: 'assert(sum(0,1,2) === 3, "The result of sum(0,1,2)
should be 3");'
- - text: 'sum(1,2,3,4)
的结果应为10'
- testString: 'assert(sum(1,2,3,4) === 10, "The result of sum(1,2,3,4)
should be 10");'
- - text: sum(5)
的结果应为5
- testString: 'assert(sum(5) === 5, "The result of sum(5)
should be 5");'
- - text: sum()
的结果应为0
- testString: 'assert(sum() === 0, "The result of sum()
should be 0");'
- - text: sum
函数在args
参数上使用...
spread运算符。
- testString: 'getUserInput => assert(getUserInput("index").match(/function\s+sum\s*\(\s*...args\s*\)\s*{/g), "The sum
function uses the ...
spread operator on the args
parameter.");'
-
-```
-
-
-
-## Challenge Seed
-
-
-
-
-```js
-const sum = (function() {
- "use strict";
- return function sum(x, y, z) {
- const args = [ x, y, z ];
- return args.reduce((a, b) => a + b, 0);
- };
-})();
-console.log(sum(1, 2, 3)); // 6
-
-```
-
-
-
-
-
-
-
-## Solution
-
-
-```js
-// solution required
-```
-
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-the-rest-parameter-with-function-parameters.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-the-rest-parameter-with-function-parameters.chinese.md
new file mode 100644
index 0000000000..0d6c237dc1
--- /dev/null
+++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-the-rest-parameter-with-function-parameters.chinese.md
@@ -0,0 +1,78 @@
+---
+id: 587d7b88367417b2b2512b47
+title: Use the Rest Parameter with Function Parameters
+challengeType: 1
+forumTopicId: 301221
+localeTitle: 将 rest 操作符与函数参数一起使用
+---
+
+## Description
+
+ES6 推出了用于函数参数的 rest 操作符帮助我们创建更加灵活的函数。在rest
操作符的帮助下,你可以创建有一个变量来接受多个参数的函数。这些参数被储存在一个可以在函数内部读取的数组中。
+请看以下代码:
+
+```js
+function howMany(...args) {
+ return "You have passed " + args.length + " arguments.";
+}
+console.log(howMany(0, 1, 2)); // You have passed 3 arguments.
+console.log(howMany("string", null, [1, 2, 3], { })); // You have passed 4 arguments.
+```
+
+rest
操作符可以避免查看args
数组的需求,并且允许我们在参数数组上使用map()
、fiter()
和 reduce()
。
+
+
+## Instructions
+
+修改sum
函数,来让它使用rest
操作符,并且它可以在有任何数量的参数时以相同的形式工作。
+
+
+## Tests
+
+
+```yml
+tests:
+ - text: sum(0,1,2)
的返回结果应该为3。
+ testString: assert(sum(0,1,2) === 3);
+ - text: sum(1,2,3,4)
的返回结果应该为10。
+ testString: assert(sum(1,2,3,4) === 10);
+ - text: sum(5)
的返回结果应该为5。
+ testString: assert(sum(5) === 5);
+ - text: sum()
的返回结果应该为 0。
+ testString: assert(sum() === 0);
+ - text: 对sum
函数的args
参数使用了...
展开操作符。
+ testString: assert(code.replace(/\s/g,'').match(/sum=\(\.\.\.args\)=>/));
+
+```
+
+
+
+## Challenge Seed
+
+
+
+
+```js
+const sum = (x, y, z) => {
+ const args = [x, y, z];
+ return args.reduce((a, b) => a + b, 0);
+}
+console.log(sum(1, 2, 3)); // 6
+```
+
+
+
+
+
+
+
+## Solution
+
+
+```js
+const sum = (...args) => {
+ return args.reduce((a, b) => a + b, 0);
+}
+```
+
+
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place.chinese.md
index dc637e1a94..6e405c59bd 100644
--- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place.chinese.md
+++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place.chinese.md
@@ -2,26 +2,52 @@
id: 587d7b89367417b2b2512b48
title: Use the Spread Operator to Evaluate Arrays In-Place
challengeType: 1
-videoUrl: ''
-localeTitle: 使用Spread运算符来就地评估数组
+forumTopicId: 301222
+localeTitle: 使用 spread 运算符展开数组项
---
## Description
- ES6引入了扩展运算符 ,它允许我们在需要多个参数或元素的位置扩展数组和其他表达式。下面的ES5代码使用apply()
来计算数组中的最大值: var arr = [6,89,3,45];
var maximus = Math.max.apply(null,arr); //返回89
我们必须使用Math.max.apply(null, arr)
因为Math.max(arr)
返回NaN
。 Math.max()
期望以逗号分隔的参数,但不是数组。扩展运算符使这种语法更易于阅读和维护。 const arr = [6,89,3,45];
const maximus = Math.max(... arr); //返回89
...arr
返回一个解压缩的数组。换句话说,它传播阵列。但是,扩展运算符只能在就地工作,就像在函数的参数或数组文字中一样。以下代码不起作用: const spreaded = ... arr; //将抛出语法错误
+
+ES6 允许我们使用 展开操作符 来展开数组,以及需要多个参数或元素的表达式。
+下面的 ES5 代码使用了apply()
来计算数组的最大值:
+
+```js
+var arr = [6, 89, 3, 45];
+var maximus = Math.max.apply(null, arr); // returns 89
+```
+
+我们必须使用Math.max.apply(null,arr)
,是因为直接调用Math.max(arr)
会返回NaN
。Math.max()
函数需要传入的是一系列由逗号分隔的参数,而不是一个数组。
+展开操作符可以提升代码的可读性,这对后续的代码维护是有积极作用的。
+
+```js
+const arr = [6, 89, 3, 45];
+const maximus = Math.max(...arr); // returns 89
+```
+
+...arr
返回了一个“打开”的数组。或者说它 展开 了数组。
+然而,展开操作符只能够在函数的参数中,或者数组之中使用。下面的代码将会报错:
+
+```js
+const spreaded = ...arr; // will throw a syntax error
+```
+
+
## Instructions
-使用spread运算符将arr1
所有内容复制到另一个数组arr2
。
+
+使用展开操作符将arr1
中的内容都赋值到arr2
中去。
+
## Tests
```yml
tests:
- - text: arr2
是arr1
正确副本。
+ - text: arr2
的值是由arr1
拷贝而来的。
testString: assert(arr2.every((v, i) => v === arr1[i]));
- - text: ...
传播运算符用于复制arr1
。
+ - text: 用...
展开操作符来赋值arr1
。
testString: assert(code.match(/Array\(\s*\.\.\.arr1\s*\)|\[\s*\.\.\.arr1\s*\]/));
- - text: 更改arr1
时, arr2
保持不变。
+ - text: 当arr1
改变的时候,arr2
不会改变。
testString: assert((arr1, arr2) => {arr1.push('JUN'); return arr2.length < arr1.length});
```
@@ -36,12 +62,10 @@ tests:
```js
const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;
-(function() {
- "use strict";
- arr2 = []; // change this line
-})();
-console.log(arr2);
+arr2 = []; // change this line
+
+console.log(arr2);
```
@@ -54,6 +78,10 @@ console.log(arr2);
```js
-// solution required
+const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
+let arr2;
+
+arr2 = [...arr1];
```
+
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-arrow-functions-with-parameters.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-arrow-functions-with-parameters.chinese.md
index 7d39aeec09..3e53218b49 100644
--- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-arrow-functions-with-parameters.chinese.md
+++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-arrow-functions-with-parameters.chinese.md
@@ -2,30 +2,54 @@
id: 587d7b88367417b2b2512b44
title: Write Arrow Functions with Parameters
challengeType: 1
-videoUrl: ''
-localeTitle: 用参数写箭头函数
+forumTopicId: 301223
+localeTitle: 编写带参数的箭头函数
---
## Description
-就像普通函数一样,您可以将参数传递给箭头函数。 //将输入值加倍并返回
const doubler =(item)=> item * 2;
您也可以将多个参数传递给箭头函数。
+
+和一般的函数一样,你也可以给箭头函数传递参数。
+
+```js
+// 给传入的数值乘以 2 并返回结果
+const doubler = (item) => item * 2;
+```
+
+如果箭头函数只有一个参数,则可以省略包含该参数的括号。
+
+```js
+// the same function, without the argument parentheses
+const doubler = item => item * 2;
+```
+
+可以将多个参数传递到箭头函数中。
+
+```js
+// multiplies the first input value by the second and returns it
+const multiplier = (item, multi) => item * multi;
+```
+
+
## Instructions
-重写myConcat
函数,该函数将arr2
内容追加到arr1
以便该函数使用箭头函数语法。
+
+使用箭头函数的语法重写myConcat
函数,使其可以将arr2
的内容填充在arr1
里。
+
## Tests
```yml
tests:
- - text: 用户确实替换了var
关键字。
+ - text: 替换掉所有的var
关键字。
testString: getUserInput => assert(!getUserInput('index').match(/var/g));
- - text: myConcat
应该是一个常量变量(使用const
)。
+ - text: myConcat
应该是一个常量 (使用const
)。
testString: getUserInput => assert(getUserInput('index').match(/const\s+myConcat/g));
- - text: myConcat
应该是一个函数
+ - text: myConcat
应该是一个函数。
testString: assert(typeof myConcat === 'function');
- - text: myConcat()
返回正确的array
+ - text: myConcat()
应该返回 [1, 2, 3, 4, 5]
。
testString: assert(() => { const a = myConcat([1], [2]); return a[0] == 1 && a[1] == 2; });
- - text: function
关键字未使用。
+ - text: 不要使用function
关键字。
testString: getUserInput => assert(!getUserInput('index').match(/function/g));
```
@@ -44,7 +68,6 @@ var myConcat = function(arr1, arr2) {
};
// test your code
console.log(myConcat([1, 2], [3, 4, 5]));
-
```
@@ -57,6 +80,12 @@ console.log(myConcat([1, 2], [3, 4, 5]));
```js
-// solution required
+const myConcat = (arr1, arr2) => {
+ "use strict";
+ return arr1.concat(arr2);
+};
+// test your code
+console.log(myConcat([1, 2], [3, 4, 5]));
```
+
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-concise-declarative-functions-with-es6.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-concise-declarative-functions-with-es6.chinese.md
index 178c86afb2..5458c8500f 100644
--- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-concise-declarative-functions-with-es6.chinese.md
+++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-concise-declarative-functions-with-es6.chinese.md
@@ -2,26 +2,51 @@
id: 587d7b8b367417b2b2512b50
title: Write Concise Declarative Functions with ES6
challengeType: 1
-videoUrl: ''
-localeTitle: 用ES6编写简明的声明函数
+forumTopicId: 301224
+localeTitle: 用 ES6 编写简洁的函数声明
---
## Description
-在ES5中定义对象内的函数时,我们必须使用关键字function
,如下所示: const person = {
名称:“泰勒”,
sayHello:function(){
回来`你好!我的名字是$ {this.name} .`;
}
};
使用ES6,您可以在定义对象中的函数时完全删除function
关键字和冒号。以下是此语法的示例: const person = {
名称:“泰勒”,
问好() {
回来`你好!我的名字是$ {this.name} .`;
}
};
+
+在 ES5 中,当我们需要在对象中定义一个函数的时候,我们必须如下面这般使用function
关键字:
+
+```js
+const person = {
+ name: "Taylor",
+ sayHello: function() {
+ return `Hello! My name is ${this.name}.`;
+ }
+};
+```
+
+在 ES6 语法的对象中定义函数的时候,你可以完全删除function
关键字和冒号。请看以下例子:
+
+```js
+const person = {
+ name: "Taylor",
+ sayHello() {
+ return `Hello! My name is ${this.name}.`;
+ }
+};
+```
+
+
## Instructions
-重构对象bicycle
内的函数setGear
以使用上述简写语法。
+
+使用以上这种简短的语法,重构在bicycle
对象中的setGear
函数。
+
## Tests
```yml
tests:
- - text: 未使用传统函数表达式。
+ - text: 不应使用function
关键字定义方法。
testString: getUserInput => assert(!removeJSComments(code).match(/function/));
- - text: setGear
是一个声明函数。
+ - text: setGear
应是一个函数。
testString: assert(typeof bicycle.setGear === 'function' && code.match(/setGear\s*\(.+\)\s*\{/));
- - text: bicycle.setGear(48)
应该返回48。
+ - text: 执行bicycle.setGear(48)
应可以让gear
的值变为 48。
testString: assert((new bicycle.setGear(48)).gear === 48);
```
@@ -38,19 +63,24 @@ tests:
const bicycle = {
gear: 2,
setGear: function(newGear) {
- "use strict";
this.gear = newGear;
}
};
// change code above this line
bicycle.setGear(3);
console.log(bicycle.gear);
-
```
+### After Test
+
+```js
+const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, '');
+```
+
+
@@ -58,6 +88,13 @@ console.log(bicycle.gear);
```js
-// solution required
+const bicycle = {
+ gear: 2,
+ setGear(newGear) {
+ this.gear = newGear;
+ }
+};
+bicycle.setGear(3);
```
+
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-object-property-shorthand.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-object-property-shorthand.chinese.md
new file mode 100644
index 0000000000..0e0238634a
--- /dev/null
+++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-concise-object-literal-declarations-using-object-property-shorthand.chinese.md
@@ -0,0 +1,89 @@
+---
+id: 587d7b8a367417b2b2512b4f
+title: Write Concise Object Literal Declarations Using Object Property Shorthand
+challengeType: 1
+forumTopicId: 301225
+localeTitle: 使用简单字段编写简洁的对象字面量声明
+---
+
+## Description
+
+ES6 添加了一些很棒的功能,以便于更方便地定义对象。
+请看以下代码:
+
+```js
+const getMousePosition = (x, y) => ({
+ x: x,
+ y: y
+});
+```
+
+getMousePosition
是一个返回了拥有2个属性的对象的简单函数。
+ES6 提供了一个语法糖,消除了类似x: x
这种冗余的写法.你可以仅仅只写一次x
,解释器会自动将其转换成x: x
。
+下面是使用这种语法重写的同样的函数:
+
+```js
+const getMousePosition = (x, y) => ({ x, y });
+```
+
+
+
+## Instructions
+
+请使用简单属性对象的语法来创建并返回一个Person
对象。
+
+
+## Tests
+
+
+```yml
+tests:
+ - text: '输出是{name: "Zodiac Hasbro", age: 56, gender: "male"}
。'
+ testString: assert.deepEqual({name:"Zodiac Hasbro",age:56,gender:"male"}, createPerson("Zodiac Hasbro", 56, "male"));
+ - text: '不要使用key:value
。'
+ testString: getUserInput => assert(!getUserInput('index').match(/:/g));
+
+```
+
+
+
+## Challenge Seed
+
+
+
+
+```js
+const createPerson = (name, age, gender) => {
+ "use strict";
+ // change code below this line
+ return {
+ name: name,
+ age: age,
+ gender: gender
+ };
+ // change code above this line
+};
+console.log(createPerson("Zodiac Hasbro", 56, "male")); // returns a proper object
+```
+
+
+
+
+
+
+
+## Solution
+
+
+```js
+const createPerson = (name, age, gender) => {
+ "use strict";
+ return {
+ name,
+ age,
+ gender
+ };
+};
+```
+
+
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions.chinese.md
deleted file mode 100644
index 9152376959..0000000000
--- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions.chinese.md
+++ /dev/null
@@ -1,69 +0,0 @@
----
-id: 587d7b88367417b2b2512b45
-title: Write Higher Order Arrow Functions
-challengeType: 1
-videoUrl: ''
-localeTitle: 编写高阶箭头函数
----
-
-## Description
-是时候我们看到处理数据时箭头功能有多强大了。 Arrow函数与高阶函数(例如map()
, filter()
和reduce()
非常兼容,它们将其他函数作为处理数据集合的参数。阅读以下代码: FBPosts.filter(function(post){
return post.thumbnail!== null && post.shares> 100 && post.likes> 500;
})
我们用filter()
写了这个,至少使它有点可读。现在将它与以下使用箭头函数语法的代码进行比较: FBPosts.filter((post)=> post.thumbnail!== null && post.shares> 100 && post.likes> 500)
此代码更简洁,使用更少的代码行完成相同的任务。
-
-## Instructions
-使用箭头函数语法计算数组realNumberArray
中只有正整数(十进制数不是整数)的realNumberArray
,并将新数组存储在变量squaredIntegers
。
-
-## Tests
-
-
-```yml
-tests:
- - text: squaredIntegers
应该是一个常量变量(通过使用const
)。
- testString: 'getUserInput => assert(getUserInput("index").match(/const\s+squaredIntegers/g), "squaredIntegers
should be a constant variable (by using const
).");'
- - text: squaredIntegers
应该是一个array
- testString: 'assert(Array.isArray(squaredIntegers), "squaredIntegers
should be an array
");'
- - text: 'squaredIntegers
应该是[16, 1764, 36]
squaredIntegers
[16, 1764, 36]
'
- testString: 'assert.deepStrictEqual(squaredIntegers, [16, 1764, 36], "squaredIntegers
should be [16, 1764, 36]
");'
- - text: function
关键字未使用。
- testString: 'getUserInput => assert(!getUserInput("index").match(/function/g), "function
keyword was not used.");'
- - text: 不应该使用循环
- testString: 'getUserInput => assert(!getUserInput("index").match(/(for)|(while)/g), "loop should not be used");'
- - text: 应使用map
, filter
或reduce
- testString: 'getUserInput => assert(getUserInput("index").match(/map|filter|reduce/g), "map
, filter
, or reduce
should be used");'
-
-```
-
-
-
-## Challenge Seed
-
-
-
-
-```js
-const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
-const squareList = (arr) => {
- "use strict";
- // change code below this line
- const squaredIntegers = arr;
- // change code above this line
- return squaredIntegers;
-};
-// test your code
-const squaredIntegers = squareList(realNumberArray);
-console.log(squaredIntegers);
-
-```
-
-
-
-
-
-
-
-## Solution
-
-
-```js
-// solution required
-```
-