diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/compare-scopes-of-the-var-and-let-keywords.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/compare-scopes-of-the-var-and-let-keywords.chinese.md index bbd899b305..0d43725e83 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/compare-scopes-of-the-var-and-let-keywords.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/compare-scopes-of-the-var-and-let-keywords.chinese.md @@ -2,26 +2,93 @@ id: 587d7b87367417b2b2512b40 title: Compare Scopes of the var and let Keywords challengeType: 1 -videoUrl: '' -localeTitle: 比较var的范围并让关键字 +forumTopicId: 301195 +localeTitle: 比较 var 和 let 关键字的作用域 --- ## Description -
使用var关键字声明变量时,它将全局声明,如果在函数内声明,则声明为本地。 let关键字的行为类似,但具有一些额外的功能。在块,语句或表达式中使用let关键字声明变量时,其范围仅限于该块,语句或表达式。例如:
var numArray = [];
for(var i = 0; i <3; i ++){
numArray.push(ⅰ);
}
的console.log(numArray);
//返回[0,1,2]
的console.log(ⅰ);
//返回3
使用var关键字, i被全局声明。因此,当执行i++时,它会更新全局变量。此代码类似于以下内容:
var numArray = [];
var i;
for(i = 0; i <3; i ++){
numArray.push(ⅰ);
}
的console.log(numArray);
//返回[0,1,2]
的console.log(ⅰ);
//返回3
如果您要创建一个函数并将其存储以供以后在使用i变量的for循环中使用,则此行为将导致问题。这是因为存储的函数将始终引用更新的全局i变量的值。
var printNumTwo;
for(var i = 0; i <3; i ++){
if(i === 2){
printNumTwo = function(){
return i;
};
}
}
的console.log(printNumTwo());
//返回3
正如你所看到的, printNumTwo()打印3,而不是2.这是因为分配给该值i进行了更新和printNumTwo()返回全球i ,而不是价值i的作用是在创建for循环的时候了。 let关键字不遵循此行为:
'use strict';
让printNumTwo;
for(let i = 0; i <3; i ++){
if(i === 2){
printNumTwo = function(){
return i;
};
}
}
的console.log(printNumTwo());
//返回2
的console.log(ⅰ);
//返回“i没有定义”
i没有定义,因为它没有在全局范围内声明。它仅在for循环语句中声明。 printNumTwo()返回正确的值,因为循环语句中的let关键字创建了具有唯一值(0,1和2)的三个不同的i变量。
+
+当你使用var关键字来声明一个变量的时候,这个变量会被声明成全局变量,或是函数内的局部变量。 +let关键字的作用类似,但会有一些额外的特性。如果你在代码块、语句或表达式中使用关键字let声明变量,这个变量的作用域就被限制在当前的代码块,语句或表达式之中。 +举个例子: + +```js +var numArray = []; +for (var i = 0; i < 3; i++) { + numArray.push(i); +} +console.log(numArray); +// 返回 [0, 1, 2] +console.log(i); +// 返回 3 +``` + +当使用var关键字的时候,i会被声明成全局变量。当i++执行的时候,它会改变全局变量的值。这段代码可以看做下面这样: + +```js +var numArray = []; +var i; +for (i = 0; i < 3; i++) { + numArray.push(i); +} +console.log(numArray); +// 返回 [0, 1, 2] +console.log(i); +// 返回 3 +``` + +如果你在for循环中创建了使用i变量的函数,那么在后续调用函数的时候,上面提到的这种行为就会出现问题。这是因为函数存储的值会因为全局变量i的变化而不断的改变。 + +```js +var printNumTwo; +for (var i = 0; i < 3; i++) { + if (i === 2) { + printNumTwo = function() { + return i; + }; + } +} +console.log(printNumTwo()); +// 返回 3 +``` + +可以看到,printNumTwo()打印了 3 而不是 2。这是因为i发生了改变,并且函数printNumTwo()返回的是全局变量i的值,而不是for循环中创建函数时i的值。let关键字就不会有这种现象: + +```js +'use strict'; +let printNumTwo; +for (let i = 0; i < 3; i++) { + if (i === 2) { + printNumTwo = function() { + return i; + }; + } +} +console.log(printNumTwo()); +// 返回 2 +console.log(i); +// 返回 "i 未定义" +``` + +i在全局作用域中没有声明,所以它没有被定义,它的声明只会发生在for循环内。在循环执行的时候,let关键字创建了三个不同的i变量,他们的值分别为 0、1 和 2,所以printNumTwo()返回了正确的值。 +
## Instructions -
修复代码,以便i在if语句中声明的是一个单独的变量,而不是i在函数的第一行声明的变量。确保不要在代码中的任何位置使用var关键字。本练习旨在说明varlet关键字如何将范围分配给声明的变量之间的区别。在编写与本练习中使用的函数类似的函数时,通常最好使用不同的变量名来避免混淆。
+
+修改这段代码,使得在if语句中声明的i变量与在函数的第一行声明的i变量是彼此独立的。请注意不要在你的代码的任何地方使用var关键字。 +这个练习说明了使用varlet关键字声明变量时,作用域之间的不同。当编写类似这个练习中的函数的时候,通常来说最好还是使用不同的变量名来避免误会。 +
## Tests
```yml tests: - - text: var在代码中不存在。 + - text: var不应该在代码中存在。 testString: getUserInput => assert(!getUserInput('index').match(/var/g)); - - text: 在if语句中声明的变量i应该等于“块范围”。 + - text: "在if语句中声明的i变量的值是 'block scope'。" testString: getUserInput => assert(getUserInput('index').match(/(i\s*=\s*).*\s*.*\s*.*\1('|")block\s*scope\2/g)); - - text: checkScope()应该返回“函数范围” + - text: "checkScope()应当返回 'function scope'" testString: assert(checkScope() === "function scope"); ``` @@ -35,16 +102,15 @@ tests: ```js function checkScope() { -"use strict"; - var i = "function scope"; + 'use strict'; + var i = 'function scope'; if (true) { - i = "block scope"; - console.log("Block scope i is: ", i); + i = 'block scope'; + console.log('Block scope i is: ', i); } - console.log("Function scope i is: ", i); + console.log('Function scope i is: ', i); return i; } - ``` @@ -57,6 +123,17 @@ function checkScope() {
```js -// solution required +function checkScope() { + 'use strict'; + let i = 'function scope'; + if (true) { + let i = 'block scope'; + console.log('Block scope i is: ', i); + } + + console.log('Function scope i is: ', i); + return i; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject.chinese.md new file mode 100644 index 0000000000..431872049c --- /dev/null +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject.chinese.md @@ -0,0 +1,89 @@ +--- +id: 5cdafbc32913098997531680 +title: Complete a Promise with resolve and reject +challengeType: 1 +forumTopicId: 301196 +localeTitle: 通过 resolve 和 reject 完成 Promise +--- + +## Description +
+promise 有三个状态:pendingfulfilledrejected。上一个挑战里创建的 promise 一直阻塞在 pending 状态里,因为没有调用 promise 的完成方法。promise 提供的 resolvereject 参数就是用来结束 promise 的。promise 成功时调用 resolve,promise 执行失败时调用 reject,这两个方法接收一个参数,如下所示。 + +```js +const myPromise = new Promise((resolve, reject) => { + if(condition here) { + resolve("Promise was fulfilled"); + } else { + reject("Promise was rejected"); + } +}); +``` + +上面的例子使用字符串做为函数的参数,也可以是任意类型,通常会是对象,里面可能是将要放到页面或其它地方的数据。 +
+ +## Instructions +
+使 promise 可以处理成功和失败情况。如果 responseFromServertrue,调用 resolve 方法使 promise 成功。给 resolve 传递值为 We got the data 的字符串。如果 responseFromServerfalse, 使用 reject 方法并传入值为 Data not received 的字符串。 +
+ +## Tests +
+ +```yml +tests: + - text: 当 if 条件是 true 时应该执行 resolve。 + testString: assert(removeJSComments(code).match(/if\s*\(\s*responseFromServer\s*\)\s*{\s*resolve\s*\(\s*('|"|`)We got the data\1\s*\)(\s*|\s*;\s*)}/g)); + - text: 当 if 条件是 false 时应该执行 reject。 + testString: assert(removeJSComments(code).match(/}\s*else\s*{\s*reject\s*\(\s*('|"|`)Data not received\1\s*\)(\s*|\s*;\s*)}/g)); +``` + +
+ +## Challenge Seed +
+
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer represents a response from a server + let responseFromServer; + + if(responseFromServer) { + // change this line + } else { + // change this line + } +}); +``` + +
+ +### After Test +
+ +```js +const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); +``` + +
+
+ +## Solution +
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer represents a response from a server + let responseFromServer; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); +``` + +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-a-javascript-promise.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-a-javascript-promise.chinese.md new file mode 100644 index 0000000000..831ebae913 --- /dev/null +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-a-javascript-promise.chinese.md @@ -0,0 +1,59 @@ +--- +id: 5cdafbb0291309899753167f +title: Create a JavaScript Promise +challengeType: 1 +forumTopicId: 301197 +localeTitle: 创建一个 JavaScript Promise +--- + +## Description +
+Promise 是异步编程的一种解决方案 - 它在未来的某时会生成一个值。任务完成,分执行成功和执行失败两种情况。Promise 是构造器函数,需要通过 new 关键字来创建。构造器参数是一个函数,该函数有两个参数 - resolvereject。通过它们来判断 promise 的执行结果。用法如下: + +```js +const myPromise = new Promise((resolve, reject) => { + +}); +``` + +
+ +## Instructions +
+创建一个名为 makeServerRequest 的 promise。给构造器函数传入 resolvereject 两个参数。 +
+ +## Tests +
+ +```yml +tests: + - text: 应该给名为 makeServerRequest 的变量指定一个 promise。 + testString: assert(makeServerRequest instanceof Promise); + - text: promise 应该接收一个函数做为参数,该函数应该包含 resolvereject 两个参数。 + testString: assert(code.match(/Promise\(\s*(function\s*\(\s*resolve\s*,\s*reject\s*\)\s*{|\(\s*resolve\s*,\s*reject\s*\)\s*=>\s*{)[^}]*}/g)); +``` + +
+ +## Challenge Seed +
+
+ +```js + +``` + +
+
+ +## Solution +
+ +```js +const makeServerRequest = new Promise((resolve, reject) => { + +}); +``` + +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-a-module-script.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-a-module-script.chinese.md new file mode 100644 index 0000000000..c58a894c81 --- /dev/null +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-a-module-script.chinese.md @@ -0,0 +1,70 @@ +--- +id: 5cddbfd622f1a59093ec611d +title: Create a Module Script +challengeType: 6 +forumTopicId: 301198 +localeTitle: 创建一个模块脚本 +--- + +## Description +
+起初,JavaScript 几乎只在 HTML web 扮演一个很小的角色。今天,一切不同了,很多网站几乎全是用 JavaScript 所写。为了让 JavaScript 更模块化、更整洁以及更易于维护,ES6 引入了在多个 JavaScript 文件之间共享代码的机制。它可以导出文件的一部分供其它文件使用,然后在需要它的地方按需导入。为了使用这一功能, 需要在 HTML 文档里创建一个类型为 module 的脚本。例子如下: + +```html + +``` + +使用了 module 类型的脚本后可以再接下来的挑战里使用 importexport 特性。 +
+ +## Instructions +
+给 HTML 文档添加 module 类型的脚本,指定源文件为 index.js。 +
+ +## Tests +
+ +```yml +tests: + - text: 应该创建一个 script 标签。 + testString: assert(code.match(/<\s*script[^>]*>\s*<\/\s*script\s*>/g)); + - text: script 标签的类型应该为 module。 + testString: assert(code.match(/<\s*script\s+[^t]*type\s*=\s*('|")module\1[^>]*>\s*<\/\s*script\s*>/g)); + - text: script 标签的 src 属性应该为 index.js。 + testString: assert(code.match(/<\s*script\s+[^s]*src\s*=\s*('|")index\.js\1[^>]*>\s*<\/\s*script\s*>/g)); +``` + +
+ +## Challenge Seed +
+
+ +```html + + + + + + + +``` + +
+
+ +## Solution +
+ +```html + + + + + + + +``` + +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-an-export-fallback-with-export-default.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-an-export-fallback-with-export-default.chinese.md index 6ec695c0d4..805c675bb6 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-an-export-fallback-with-export-default.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-an-export-fallback-with-export-default.chinese.md @@ -2,58 +2,67 @@ id: 587d7b8c367417b2b2512b58 title: Create an Export Fallback with export default challengeType: 1 -videoUrl: '' -localeTitle: 使用导出默认值创建导出回退 +forumTopicId: 301199 +localeTitle: 用 export default 创建一个默认导出 --- ## Description -
export课程中,您了解了称为命名导出的语法。这使您可以使多个函数和变量可用于其他文件。您需要知道另一种export语法,称为导出默认值 。通常,如果只从文件导出一个值,您将使用此语法。它还用于为文件或模块创建回退值。以下是export default的快速示例:
export default function add(x,y){
  return x + y;
}
注意:由于export default用于声明模块或文件的回退值,因此每个模块或文件中只能有一个值作为默认导出。此外,您不能将export default用于varletconst
+
+在export的课程中,学习了命名导出语法。这可以在其他文件中引用一些函数或者变量。 +还需要了解另外一种被称为默认导出export的语法。在文件中只有一个值需要导出的时候,通常会使用这种语法。它也常常用于给文件或者模块创建返回值。 +下面是一个简单的export default例子: + +```js +// named function +export default function add(x, y) { + return x + y; +} + +// anonymous function +export default function(x, y) { + return x + y; +} +``` + +注意:当使用export default去声明一个文件或者模块的返回值时,在每个文件或者模块中应当只默认导出一个值。特别地,能将export deafultvarletconst一起使用。 +
## Instructions -
以下函数应该是模块的回退值。请添加必要的代码。
+
+下面的函数应该在这个模块中返回一个值。请添加需要的代码: +
## Tests
```yml tests: - - text: 适当使用export回落。 + - text: 正确的使用export进行返回。 testString: assert(code.match(/export\s+default\s+function(\s+subtract\s*|\s*)\(\s*x,\s*y\s*\)\s*{/g)); - ```
## Challenge Seed
-
```js -"use strict"; -function subtract(x,y) {return x - y;} - +function subtract(x, y) { + return x - y; +} ```
- -### Before Test -
- -```js -window.exports = function(){}; - -``` - -
- -
## Solution
```js -// solution required +export default function subtract(x, y) { + return x - y; +} ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-strings-using-template-literals.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-strings-using-template-literals.chinese.md index d1b1fa419b..ec4a7434f0 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-strings-using-template-literals.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/create-strings-using-template-literals.chinese.md @@ -2,28 +2,58 @@ id: 587d7b8a367417b2b2512b4e title: Create Strings using Template Literals challengeType: 1 -videoUrl: '' -localeTitle: 使用模板文字创建字符串 +forumTopicId: 301200 +localeTitle: 使用模板字面量创建字符串 --- ## Description -
ES6的一个新功能是模板文字 。这是一种特殊类型的字符串,可以更轻松地创建复杂字符串。模板文字允许您创建多行字符串并使用字符串插值功能来创建字符串。考虑以下代码:
const person = {
name: "Zodiac Hasbro",
age: 56
};

//具有多行和字符串插值的模板文字
const greeting =`您好,我的名字是${person.name}!
我是${person.age}岁。`

的console.log(greeting); //打印
//你好,我的名字是Zodiac Hasbro!
//我今年56岁
那里发生了很多事情。首先,例如使用反引号( ` ),而不是引号( '" ),换行字符串。其次,请注意,该字符串是多线,无论是在代码和输出。这节省了插入\n串内。上面使用的${variable}语法是占位符。基本上,您不必再使用+运算符连接。要将变量添加到字符串,只需将变量放在模板字符串中并用${包装它}同样,您可以在您的字符串表达式的其他文字,例如${a + b}这个新创建的字符串的方式为您提供了更大的灵活性,以创建强大的字符串。
+
+模板字符串是 ES6 的另外一项新的功能。这是一种可以轻松构建复杂字符串的方法。 +模板字符串可以使用多行字符串和字符串插值功能。 +请看以下代码: + +```js +const person = { + name: "Zodiac Hasbro", + age: 56 +}; + +// Template literal with multi-line and string interpolation +const greeting = `Hello, my name is ${person.name}! +I am ${person.age} years old.`; + +console.log(greeting); // prints +// Hello, my name is Zodiac Hasbro! +// I am 56 years old. + +``` + +这段代码有许多的不同: +首先,上面的例子使用了反引号(`)而不是引号(' 或者 ")定义字符串。 +其次,注意字符串是多行的,不管是代码还是输出。这是因为在字符串内插入了 \n。 +上面使用的${variable}语法是一个占位符。这样一来,你将不再需要使用+运算符来连接字符串。当需要在字符串里增加变量的时候,你只需要在变量的外面括上${},并将其放在字符串里就可以了。 +这个新的方式使你可以更灵活的创建复杂的字符串。 +
## Instructions -
使用带有反引号的模板文字语法来显示result对象的failure数组的每个条目。每个条目都应该包含在一个带有class属性text-warningli元素中,并列在resultDisplayArray
+
+使用模板字符串的反引号的语法来展示result对象的failure数组内的每个条目。每个条目应该括在带有text-warning类属性的li标签中,并赋值给resultDisplayArray。 +使用遍历方法(可以是任意形式的循环)输出指定值。 +
## Tests
```yml tests: - - text: resultDisplayArray是一个包含result failure消息的数组。 - testString: 'assert(typeof makeList(result.failure) === "object" && resultDisplayArray.length === 3, "resultDisplayArray is a list containing result failure messages.");' - - text: resultDisplayArray是所需的输出。 - testString: 'assert(makeList(result.failure).every((v, i) => v === `
  • ${result.failure[i]}
  • ` || v === `
  • ${result.failure[i]}
  • `), "resultDisplayArray is the desired output.");' - - text: 使用了模板字符串 - testString: 'getUserInput => assert(getUserInput("index").match(/`.*`/g), "Template strings were not used");' - + - text: resultDisplayArray 是一个包含了 result failure 内的消息的数组。 + testString: assert(typeof makeList(result.failure) === 'object' && resultDisplayArray.length === 3); + - text: resultDisplayArray 要有正确的输出。 + testString: assert(makeList(result.failure).every((v, i) => v === `
  • ${result.failure[i]}
  • ` || v === `
  • ${result.failure[i]}
  • `)); + - text: 应使用模板字符串。 + testString: getUserInput => assert(getUserInput('index').match(/(`.*\${.*}.*`)/)); + - text: 应该遍历。 + testString: getUserInput => assert(getUserInput('index').match(/for|map|reduce|forEach|while/)); ```
    @@ -55,7 +85,6 @@ function makeList(arr) { * `
  • linebreak
  • ` ] **/ const resultDisplayArray = makeList(result.failure); - ``` @@ -68,6 +97,25 @@ const resultDisplayArray = makeList(result.failure);
    ```js -// solution required +const result = { + success: ["max-length", "no-amd", "prefer-arrow-functions"], + failure: ["no-var", "var-on-top", "linebreak"], + skipped: ["id-blacklist", "no-dup-keys"] +}; +function makeList(arr) { + "use strict"; + + const resultDisplayArray = arr.map(val => `
  • ${val}
  • `); + + return resultDisplayArray; +} +/** + * makeList(result.failure) should return: + * [ `
  • no-var
  • `, + * `
  • var-on-top
  • `, + * `
  • linebreak
  • ` ] + **/ +const resultDisplayArray = makeList(result.failure); ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/declare-a-read-only-variable-with-the-const-keyword.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/declare-a-read-only-variable-with-the-const-keyword.chinese.md index 44371498af..549470049b 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/declare-a-read-only-variable-with-the-const-keyword.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/declare-a-read-only-variable-with-the-const-keyword.chinese.md @@ -2,28 +2,43 @@ id: 587d7b87367417b2b2512b41 title: Declare a Read-Only Variable with the const Keyword challengeType: 1 -videoUrl: '' -localeTitle: 使用const关键字声明只读变量 +forumTopicId: 301201 +localeTitle: 用 const 关键字声明只读变量 --- ## Description -
    let不是声明变量的唯一新方法。在ES6中,您还可以使用const关键字声明变量。 const拥有所有的真棒功能, let具有,与额外的奖励,使用声明的变量const是只读的。它们是一个常量值,这意味着一旦变量被赋值为const ,就无法重新赋值。
    "use strict"
    const FAV_PET ="Cats";
    FAV_PET = "Dogs"; //返回错误
    如您所见,尝试重新分配使用const声明的变量将引发错误。您应该始终使用const关键字命名您不想重新分配的变量。当您意外尝试重新分配一个旨在保持不变的变量时,这会有所帮助。命名常量时的常见做法是使用全部大写字母,单词用下划线分隔。
    +
    +let并不是唯一的新的声明变量的方式。在 ES6里面,你还可以使用const关键字来声明变量。 +const拥有let的所有优点,所不同的是,通过const声明的变量是只读的。这意味着通过const声明的变量只能被赋值一次,而不能被再次赋值。 + +```js +"use strict"; +const FAV_PET = "Cats"; +FAV_PET = "Dogs"; // returns error +``` + +可以看见,尝试给通过const声明的变量再次赋值会报错。你应该使用const关键字来对所有不打算再次赋值的变量进行声明。这有助于你避免给一个常量进行额外的再次赋值。一个最佳实践是对所有常量的命名采用全大写字母,并在单词之间使用下划线进行分隔。 + + 注意: 一般开发者会以大写做为常量标识符,小写字母或者驼峰命名做为变量(对象或数组)标识符。接下来的挑战里会涉及到小写变量标识符的数组。 +
    ## Instructions -
    更改代码,以便使用letconst声明所有变量。如果希望变量更改,请使用let ;如果希望变量保持不变,请使用const 。此外,重命名用const声明的变量以符合常规做法,这意味着常量应该全部大写。
    +
    +改变以下代码,使得所有的变量都使用letconst关键词来声明。当变量将会改变的时候使用let关键字,当变量要保持常量的时候使用const关键字。同时,对使用const声明的变量按照最佳实践重命名,变量名中的字母应该都是大写的。 +
    ## Tests
    ```yml tests: - - text: var在您的代码中不存在。 + - text: var在代码中不存在。 testString: getUserInput => assert(!getUserInput('index').match(/var/g)); - - text: SENTENCE应该是用const声明的常量变量。 + - text: SENTENCE应该是使用const声明的常量。 testString: getUserInput => assert(getUserInput('index').match(/(const SENTENCE)/g)); - - text: i应该以let来宣布。 + - text: i应该是使用let声明的变量。 testString: getUserInput => assert(getUserInput('index').match(/(let i)/g)); - - text: 应更改console.log以打印SENTENCE变量。 + - text: console.log应该修改为用于打印SENTENCE变量。 testString: getUserInput => assert(getUserInput('index').match(/console\.log\(\s*SENTENCE\s*\)\s*;?/g)); ``` @@ -42,7 +57,7 @@ function printManyTimes(str) { // change code below this line var sentence = str + " is cool!"; - for(var i = 0; i < str.length; i+=2) { + for (var i = 0; i < str.length; i+=2) { console.log(sentence); } @@ -50,7 +65,6 @@ function printManyTimes(str) { } printManyTimes("freeCodeCamp"); - ``` @@ -63,6 +77,20 @@ printManyTimes("freeCodeCamp");
    ```js -// solution required +function printManyTimes(str) { + "use strict"; + + // change code below this line + + const SENTENCE = str + " is cool!"; + for (let i = 0; i < str.length; i+=2) { + console.log(SENTENCE); + } + + // change code above this line + +} +printManyTimes("freeCodeCamp"); ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/explore-differences-between-the-var-and-let-keywords.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/explore-differences-between-the-var-and-let-keywords.chinese.md index 164eb4873c..2f8ea34ba9 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/explore-differences-between-the-var-and-let-keywords.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/explore-differences-between-the-var-and-let-keywords.chinese.md @@ -2,31 +2,58 @@ id: 587d7b87367417b2b2512b3f title: Explore Differences Between the var and let Keywords challengeType: 1 -videoUrl: '' -localeTitle: 探索var和let关键字之间的差异 +forumTopicId: 301202 +localeTitle: 探索 var 和 let 关键字之间的差异 --- -
    使用var关键字声明变量的最大问题之一是您可以在没有错误的情况下覆盖变量声明。 -
    var camper = 'James';
    var camper = 'David';
    console.log(camper);
    // 输出 'David'
    -正如您在上面的代码中看到的那样, camper变量最初被声明为James ,然后被重写为David 。在小型应用程序中,您可能不会遇到此类问题,但是当您的代码变大时,您可能会意外覆盖您不打算覆盖的变量。因为这种行为不会引发错误,所以搜索和修复错误变得更加困难。
    在ES6中引入了一个名为let的新关键字,用var关键字解决了这个潜在的问题。如果要在上面代码的变量声明中用let替换var ,结果将是一个错误。 -
    let camper = 'James';
    let camper = 'David'; // 抛出一个错误
    -您可以在浏览器的控制台中看到此错误。因此与var不同,使用let ,具有相同名称的变量只能声明一次。注意"use strict" 。这启用了严格模式,可以捕获常见的编码错误和“不安全”操作。例如: -
    "use strict";
    x = 3.14; // 抛出一个错误,因为 x 未定义
    +## Description +
    +使用var关键字来声明变量,会出现重复声明导致变量被覆盖却不会报错的问题: + +```js +var camper = 'James'; +var camper = 'David'; +console.log(camper); +// logs 'David' +``` + +在上面的代码中,camper的初始值为'James',然后又被覆盖成了'David'。 +在小型的应用中,你可能不会遇到这样的问题,但是当你的代码规模变得更加庞大的时候,就可能会在不经意间覆盖了之前定义的变量。 +这样的行为不会报错,导致了 debug 非常困难。
    +在 ES6 中引入了新的关键字let来解决var关键字带来的潜在问题。 +如果你在上面的代码中,使用了let关键字来代替var关键字,结果会是一个报错。 + +```js +let camper = 'James'; +let camper = 'David'; // throws an error +``` + +你可以在浏览器的控制台里看见这个错误。 +与var不同的是,当使用let的时候,同一名字的变量只能被声明一次。 +请注意"use strict"。这代表着开启了严格模式,用于检测常见的代码错误以及"不安全"的行为,例如: + +```js +"use strict"; +x = 3.14; // throws an error because x is not declared +``` +
    ## Instructions -
    更新代码,使其仅使用let关键字。
    +
    +请更新这段代码,并且在其中只使用let关键字 +
    ## Tests
    ```yml tests: - - text: var在代码中不存在。 + - text: 在代码中不应存在var。 testString: getUserInput => assert(!getUserInput('index').match(/var/g)); - - text: catName应该是Oliver 。 + - text: "catName变量的值应该为'Oliver'。" testString: assert(catName === "Oliver"); - - text: quote应该是"Oliver says Meow!" + - text: "quote变量的值应该为'Oliver says Meow!'" testString: assert(quote === "Oliver says Meow!"); ``` @@ -49,7 +76,6 @@ function catTalk() { } catTalk(); - ``` @@ -62,6 +88,15 @@ catTalk();
    ```js -// solution required +let catName; +let quote; +function catTalk() { + 'use strict'; + + catName = 'Oliver'; + quote = catName + ' says Meow!'; +} +catTalk(); ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then.chinese.md new file mode 100644 index 0000000000..500cb3ecb3 --- /dev/null +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/handle-a-fulfilled-promise-with-then.chinese.md @@ -0,0 +1,92 @@ +--- +id: 5cdafbd72913098997531681 +title: Handle a Fulfilled Promise with then +challengeType: 1 +forumTopicId: 301203 +localeTitle: 在 then 中处理 Promise 完成的情况 +--- + +## Description +
    +当程序需要花费未知的时间才能完成时 Promise 很有用(比如,一些异步操作),一般是网络请求。网络请求会花费一些时间,当结束时需要根据服务器的响应执行一些操作。这可以用 then 方法来实现,当 promise 完成 resolve 时会触发 then 方法。例子如下: + +```js +myPromise.then(result => { + // do something with the result. +}); +``` + +result 即传入 resolve 方法的参数。 +
    + +## Instructions +
    +给 promise 添加 then 方法。用 result 做为回调函数的参数并将 result 打印在控制台。 +
    + +## Tests +
    + +```yml +tests: + - text: 应该给 promise 方法调用 then 方法。 + testString: assert(codeWithoutSpaces.match(/(makeServerRequest|\))\.then\(/g)); + - text: then 方法应该有一个回调函数,回调函数参数为 result。 + testString: assert(resultIsParameter); + - text: 应该打印 result 到控制台。 + testString: assert(resultIsParameter && codeWithoutSpaces.match(/\.then\(.*?result.*?console.log\(result\).*?\)/)); +``` + +
    + +## Challenge Seed +
    +
    + +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to true to represent a successful response from a server + let responseFromServer = true; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); +``` + +
    + +### After Test +
    + +```js +const codeWithoutSpaces = code.replace(/\s/g, ''); +const resultIsParameter = /\.then\((function\(result\){|result|\(result\)=>)/.test(codeWithoutSpaces); +``` + +
    +
    + +## Solution +
    + +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to true to represent a successful response from a server + let responseFromServer = true; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); +``` + +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch.chinese.md new file mode 100644 index 0000000000..79908480df --- /dev/null +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/handle-a-rejected-promise-with-catch.chinese.md @@ -0,0 +1,103 @@ +--- +id: 5cdafbe72913098997531682 +title: Handle a Rejected Promise with catch +challengeType: 1 +forumTopicId: 301204 +localeTitle: 在 catch 中处理 Promise 失败的情况 +--- + +## Description +
    +当 promise 失败时会调用 catch 方法。当 promise 的 reject 方法执行时会直接调用。用法如下: + +```js +myPromise.catch(error => { + // do something with the error. +}); +``` + +error 是传入 reject 方法的参数。 + +注意: thencatch 方法可以在 promise 后面链式调用。 +
    + +## Instructions +
    +给 promise 添加 catch 方法。用 error 做为回调函数的参数并把 error 打印到控制台。 +
    + +## Tests +
    + +```yml +tests: + - text: 应该在 promise 上调用 catch 方法。 + testString: assert(codeWithoutSpaces.match(/(makeServerRequest|\))\.catch\(/g)); + - text: catch 方法应该有一个回调函数,函数参数为error。 + testString: assert(errorIsParameter); + - text: 应该打印error到控制台。 + testString: assert(errorIsParameter && codeWithoutSpaces.match(/\.catch\(.*?error.*?console.log\(error\).*?\)/)); +``` + +
    + +## Challenge Seed +
    +
    + +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to false to represent an unsuccessful response from a server + let responseFromServer = false; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); +``` + +
    + +### After Test +
    + +```js +const codeWithoutSpaces = code.replace(/\s/g, ''); +const errorIsParameter = /\.catch\((function\(error\){|error|\(error\)=>)/.test(codeWithoutSpaces); +``` + +
    + +
    + +## Solution +
    + +```js +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to false to represent an unsuccessful response from a server + let responseFromServer = false; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); + +makeServerRequest.catch(error => { + console.log(error); +}); +``` + +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/import-a-default-export.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/import-a-default-export.chinese.md index 9c7e782e6c..c7b70ad620 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/import-a-default-export.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/import-a-default-export.chinese.md @@ -2,62 +2,60 @@ id: 587d7b8d367417b2b2512b59 title: Import a Default Export challengeType: 1 -videoUrl: '' -localeTitle: 导入默认导出 +forumTopicId: 301205 +localeTitle: 导入一个默认的导出 --- ## Description -
    在上一次挑战中,您了解了export default及其用途。请务必注意,要导入默认导出,您需要使用不同的import语法。在下面的示例中,我们有一个函数add ,它是文件的默认导出"math_functions" 。以下是如何导入它:
    从“math_functions”导入添加;
    添加(5,4); //将返回9
    语法在一个关键位置有所不同 - 导入的值add不会被花括号{}包围。与导出值不同,导入默认导出的主要方法是在import后简单地写入值的名称。
    +
    +在上一个挑战里,学习了export default的用法。还需要一种import的语法来导入默认的导出。 +在下面的例子里有一个add函数, 它在"math_functions"文件里默认被导出。来看看来如何导入它: + +```js +import add from "./math_functions.js"; +``` + +这个语法只有一处不同的地方 —— 被导入的add值,并没有被花括号{}所包围。与导出值的方法不同,导入默认导出的写法仅仅只是简单的将变量名写在import之后。 +
    ## Instructions -
    在下面的代码中,请从文件"math_functions"导入默认导出, subtract ,该文件位于与此文件相同的目录中。
    +
    +在下面的代码中,请导入在同目录下的"math_functions"文件中默认导出的subtract值。 +
    ## Tests
    ```yml tests: - - text: 正确导入export default方法。 + - text: 正确导入export default方法导出的值。 testString: assert(code.match(/import\s+subtract\s+from\s+('|")\.\/math_functions\.js\1/g)); - ```
    ## Challenge Seed
    -
    ```js -"use strict"; + +// add code above this line + subtract(7,4); - ```
    - -### Before Test -
    - -```js -window.require = function(str) { -if (str === 'math_functions') { -return function(a, b) { -return a - b; -}}}; - -``` - -
    - -
    ## Solution
    ```js -// solution required +import subtract from "./math_functions.js"; +// add code above this line + +subtract(7,4); ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/mutate-an-array-declared-with-const.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/mutate-an-array-declared-with-const.chinese.md index 6fa3aaf595..95bad2188e 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/mutate-an-array-declared-with-const.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/mutate-an-array-declared-with-const.chinese.md @@ -2,15 +2,31 @@ id: 587d7b87367417b2b2512b42 title: Mutate an Array Declared with const challengeType: 1 -videoUrl: '' -localeTitle: 改变用const声明的数组 +forumTopicId: 301206 +localeTitle: 改变一个用 const 声明的数组 --- ## Description -
    const声明在现代JavaScript中有许多用例。一些开发人员更喜欢默认使用const分配所有变量,除非他们知道需要重新分配值。只有在这种情况下,他们才会使用let 。但是,重要的是要理解使用const分配给变量的对象(包括数组和函数)仍然是可变的。使用const声明仅阻止重新分配变量标识符。
    “严格使用”;
    const s = [5,6,7];
    s = [1,2,3]; //抛出错误,尝试分配const
    s [2] = 45; //就像使用var或let声明的数组一样工作
    的console.log(一个或多个); //返回[5,6,45]
    如您所见,您可以改变对象[5, 6, 7]本身,变量s仍将指向更改的数组[5, 6, 45] 。与所有数组一样, s中的数组元素是可变的,但由于使用了const ,因此不能使用变量标识符s使用赋值运算符指向不同的数组。
    +
    +在现代的 JavaScript 里,const声明有很多用法。 +一些开发者倾向默认使用const来声明所有变量,但如果它们打算在后续的代码中修改某个值,那在声明的时候就会用let。 +然而,你要注意,对象(包括数组和函数)在使用const声明的时候依然是可变的。使用const来声明只会保证它的标识不会被重新赋值。 + +```js +"use strict"; +const s = [5, 6, 7]; +s = [1, 2, 3]; // throws error, trying to assign a const +s[2] = 45; // works just as it would with an array declared with var or let +console.log(s); // returns [5, 6, 45] +``` + +从以上代码看出,你可以改变[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]。 +
    ## Tests
    @@ -19,11 +35,11 @@ localeTitle: 改变用const声明的数组 tests: - text: 不要替换const关键字。 testString: getUserInput => assert(getUserInput('index').match(/const/g)); - - text: s应该是一个常量变量(使用const )。 + - text: s应该为常量 (通过使用const)。 testString: getUserInput => assert(getUserInput('index').match(/const\s+s/g)); - - text: 不要更改原始数组声明。 + - text: 不要改变原数组的声明。 testString: getUserInput => assert(getUserInput('index').match(/const\s+s\s*=\s*\[\s*5\s*,\s*7\s*,\s*2\s*\]\s*;?/g)); - - text: 's应该等于[2, 5, 7] 。' + - text: s应该等于[2, 5, 7]。 testString: assert.deepEqual(s, [2, 5, 7]); ``` @@ -38,7 +54,7 @@ tests: ```js const s = [5, 7, 2]; function editInPlace() { - "use strict"; + 'use strict'; // change code below this line // s = [2, 5, 7]; <- this is invalid @@ -46,7 +62,6 @@ function editInPlace() { // change code above this line } editInPlace(); - ``` @@ -59,6 +74,18 @@ editInPlace();
    ```js -// solution required +const s = [5, 7, 2]; +function editInPlace() { + 'use strict'; + // change code below this line + + // s = [2, 5, 7]; <- this is invalid + s[0] = 2; + s[1] = 5; + s[2] = 7; + // change code above this line +} +editInPlace(); ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/prevent-object-mutation.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/prevent-object-mutation.chinese.md index 2c742b271f..7b9846ccbb 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/prevent-object-mutation.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/prevent-object-mutation.chinese.md @@ -2,15 +2,33 @@ id: 598f48a36c8c40764b4e52b3 title: Prevent Object Mutation challengeType: 1 -videoUrl: '' -localeTitle: 防止对象突变 +forumTopicId: 301207 +localeTitle: 防止对象改变 --- ## Description -
    正如之前的挑战所示, const声明本身并不能真正保护您的数据免受突变。为确保您的数据不会发生变化,JavaScript提供了一个Object.freeze函数来防止数据突变。对象冻结后,您将无法再从中添加,更新或删除属性。任何更改对象的尝试都将被拒绝而不会出现错误。
    让obj = {
    名称:“FreeCodeCamp”
    点评:“真棒”
    };
    Object.freeze(OBJ);
    obj.review =“坏”; //将被忽略。不允许变异
    obj.newProp =“测试”; //将被忽略。不允许变异
    的console.log(OBJ);
    // {name:“FreeCodeCamp”,评论:“很棒”}
    +
    +通过之前的挑战可以看出,const声明并不会真的保护你的数据不被改变。为了确保数据不被改变,JavaScript 提供了一个函数Object.freeze来防止数据改变。 +当一个对象被冻结的时候,你不能再对它的属性再进行增、删、改的操作。任何试图改变对象的操作都会被阻止,却不会报错。 + +```js +let obj = { + name:"FreeCodeCamp", + review:"Awesome" +}; +Object.freeze(obj); +obj.review = "bad"; // will be ignored. Mutation not allowed +obj.newProp = "Test"; // will be ignored. Mutation not allowed +console.log(obj); +// { name: "FreeCodeCamp", review:"Awesome"} +``` + +
    ## Instructions -
    在这个挑战中,您将使用Object.freeze来防止数学常量发生变化。您需要冻结MATH_CONSTANTS对象,以便没有人能够更改PI的值,添加或删除属性。
    +
    +在这个挑战中,你将使用Object.freeze来防止数学常量被改变。你需要冻结MATH_CONSTANTS对象,使得没有人可以改变PI的值,抑或增加或删除属性。 +
    ## Tests
    @@ -19,11 +37,11 @@ localeTitle: 防止对象突变 tests: - text: 不要替换const关键字。 testString: getUserInput => assert(getUserInput('index').match(/const/g)); - - text: MATH_CONSTANTS应该是一个常量变量(使用const )。 + - text: MATH_CONSTANTS应该为一个常量 (使用const)。 testString: getUserInput => assert(getUserInput('index').match(/const\s+MATH_CONSTANTS/g)); - - text: 请勿更改原始MATH_CONSTANTS 。 + - text: 不要改变原始的MATH_CONSTANTS。 testString: getUserInput => assert(getUserInput('index').match(/const\s+MATH_CONSTANTS\s+=\s+{\s+PI:\s+3.14\s+};/g)); - - text: PI等于3.14 。 + - text: PI等于3.14。 testString: assert(PI === 3.14); ``` @@ -37,7 +55,7 @@ tests: ```js function freezeObj() { - "use strict"; + 'use strict'; const MATH_CONSTANTS = { PI: 3.14 }; @@ -47,13 +65,12 @@ function freezeObj() { // change code above this line try { MATH_CONSTANTS.PI = 99; - } catch( ex ) { + } catch(ex) { console.log(ex); } return MATH_CONSTANTS.PI; } const PI = freezeObj(); - ``` @@ -66,6 +83,23 @@ const PI = freezeObj();
    ```js -// solution required +function freezeObj() { + 'use strict'; + const MATH_CONSTANTS = { + PI: 3.14 + }; + // change code below this line + Object.freeze(MATH_CONSTANTS); + + // change code above this line + try { + MATH_CONSTANTS.PI = 99; + } catch(ex) { + console.log(ex); + } + return MATH_CONSTANTS.PI; +} +const PI = freezeObj(); ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/reuse-javascript-code-using-import.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/reuse-javascript-code-using-import.chinese.md new file mode 100644 index 0000000000..1e5ba09aab --- /dev/null +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/reuse-javascript-code-using-import.chinese.md @@ -0,0 +1,71 @@ +--- +id: 587d7b8c367417b2b2512b55 +title: Reuse Javascript Code Using import +challengeType: 1 +forumTopicId: 301208 +localeTitle: 通过 import 复用 JavaScript 代码 +--- + +## Description +
    +import 可以导入文件或模块的一部分。在之前的课程里,例子从 math_functions.js 文件里导出了 add,下面看一下如何在其它的文件导入它: + +```js +import { add } from './math_functions.js'; +``` + +在这里,import 会在 math_functions.js 里找到 add,只导入这个函数,忽略剩余的部分。./ 告诉程序在当前文件的相同目录寻找 math_functions.js 文件。用这种方式导入时,相对路径(./)和文件扩展名(.js)都是必需的。 + +可以在导入语句里导入多个项目,如下: + +```js +import { add, subtract } from './math_functions.js'; +``` + +
    + +## Instructions +
    +添加 import 语句,使当前文件可以使用你在之前课程里导出的 uppercaseStringlowercaseString 函数,函数在当前路径下的 string_functions.js 文件里。 +
    + +## Tests +
    + +```yml +tests: + - text: 应该导入 uppercaseString、 + testString: assert(code.match(/import\s*{\s*(uppercaseString[^}]*|[^,]*,\s*uppercaseString\s*)}\s+from\s+('|")\.\/string_functions\.js\2/g)); + - text: 应该导入 lowercaseString、 + testString: assert(code.match(/import\s*{\s*(lowercaseString[^}]*|[^,]*,\s*lowercaseString\s*)}\s+from\s+('|")\.\/string_functions\.js\2/g)); +``` + +
    + +## Challenge Seed +
    +
    + +```js + +// add code above this line + +uppercaseString("hello"); +lowercaseString("WORLD!"); +``` + +
    +
    + +## Solution +
    + +```js +import { uppercaseString, lowercaseString } from './string_functions.js'; +// add code above this line + +uppercaseString("hello"); +lowercaseString("WORLD!"); +``` + +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions.chinese.md index 8907d569c1..9758094c97 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/set-default-parameters-for-your-functions.chinese.md @@ -2,26 +2,40 @@ id: 587d7b88367417b2b2512b46 title: Set Default Parameters for Your Functions challengeType: 1 -videoUrl: '' +forumTopicId: 301209 localeTitle: 设置函数的默认参数 --- ## Description -
    为了帮助我们创建更灵活的函数,ES6引入了函数的默认参数 。看看这段代码:
    function greeting(name =“Anonymous”){
    返回“你好”+名字;
    }
    的console.log(问候语( “约翰”)); // 你好约翰
    的console.log(问候()); //你好匿名
    默认参数在未指定参数时启动(未定义)。正如您在上面的示例中所看到的,当您未为参数提供值时,参数name将接收其默认值"Anonymous" 。您可以根据需要为任意数量的参数添加默认值。
    +
    +ES6 里允许给函数传入默认参数,来构建更加灵活的函数。 +请看以下代码: + +```js +const greeting = (name = "Anonymous") => "Hello " + name; + +console.log(greeting("John")); // Hello John +console.log(greeting()); // Hello Anonymous +``` + +默认参数会在参数没有被指定(值为 undefined )的时候起作用。在上面的例子中,参数name会在没有得到新的值的时候,默认使用值 "Anonymous"。你还可以给多个参数赋予默认值。 +
    ## Instructions -
    修改函数increment加入默认参数,这样它会加1 number ,如果value未指定。
    +
    +给函数increment加上默认参数,使得在value没有被赋值的时候,默认给number加1。 +
    ## Tests
    ```yml tests: - - text: 'increment(5, 2)应为7 。' + - text: increment(5, 2)的结果应该为7。 testString: assert(increment(5, 2) === 7); - - text: increment(5)的结果应为6 。 + - text: increment(5)的结果应该为6。 testString: assert(increment(5) === 6); - - text: 默认参数1用于value 。 + - text: 参数value的默认值应该为1。 testString: assert(code.match(/value\s*=\s*1/g)); ``` @@ -34,15 +48,10 @@ tests:
    ```js -const increment = (function() { - "use strict"; - return function increment(number, value) { - return number + value; - }; -})(); +const increment = (number, value) => number + value; + console.log(increment(5, 2)); // returns 7 console.log(increment(5)); // returns 6 - ```
    @@ -55,6 +64,7 @@ console.log(increment(5)); // returns 6
    ```js -// solution required +const increment = (number, value = 1) => number + value; ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/understand-the-differences-between-import-and-require.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/understand-the-differences-between-import-and-require.chinese.md deleted file mode 100644 index c928536385..0000000000 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/understand-the-differences-between-import-and-require.chinese.md +++ /dev/null @@ -1,63 +0,0 @@ ---- -id: 587d7b8c367417b2b2512b55 -title: Understand the Differences Between import and require -challengeType: 1 -videoUrl: '' -localeTitle: 理解import和require之间的差异 ---- - -## Description -
    过去,函数require()将用于导入外部文件和模块中的函数和代码。虽然方便,但这会带来一个问题:某些文件和模块相当大,您可能只需要来自这些外部资源的某些代码。 ES6为我们提供了一个非常方便的工具,称为import 。有了它,我们可以选择加载到给定文件中的模块或文件的哪些部分,从而节省时间和内存。请考虑以下示例。想象一下math_array_functions有大约20个函数,但我在当前文件中只需要一个countItems 。旧的require()方法会迫使我引入所有20个函数。使用这种新的import语法,我可以引入所需的功能,如下所示:
    从“math_array_functions”导入{countItems}
    上面代码的描述:
    从“file_path_goes_here”导入{function}
    //我们也可以用同样的方式导入变量!
    有几种方法可以编写import语句,但上面是一个非常常见的用例。 注意
    花括号内的函数周围的空格是最佳实践 - 它使得读取import语句更容易。 注意
    本节中的课程处理非浏览器功能。 import以及我们在其余课程中介绍的语句将无法直接在浏览器上运行。但是,我们可以使用各种工具来创建代码,使其在浏览器中工作。 注意
    在大多数情况下,文件路径在它之前需要./ ;否则,node将首先尝试将其作为依赖项加载到node_modules目录中。
    - -## Instructions -
    添加适当的import语句,允许当前文件使用capitalizeString函数。此函数所在的文件称为"string_functions" ,它与当前文件位于同一目录中。
    - -## Tests -
    - -```yml -tests: - - text: 有效的import声明 - testString: 'getUserInput => assert(getUserInput("index").match(/import\s+\{\s*capitalizeString\s*\}\s+from\s+("|")string_functions\1/g), "valid import statement");' - -``` - -
    - -## Challenge Seed -
    - -
    - -```js -"use strict"; -capitalizeString("hello!"); - -``` - -
    - -### Before Test -
    - -```js -window.require = function (str) { -if (str === 'string_functions') { -return { -capitalizeString: str => str.toUpperCase() -}}}; - -``` - -
    - - -
    - -## Solution -
    - -```js -// solution required -``` -
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use--to-import-everything-from-a-file.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use--to-import-everything-from-a-file.chinese.md index 5558d346bc..b7d654aeac 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use--to-import-everything-from-a-file.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use--to-import-everything-from-a-file.chinese.md @@ -2,15 +2,31 @@ id: 587d7b8c367417b2b2512b57 title: Use * to Import Everything from a File challengeType: 1 -videoUrl: '' -localeTitle: 使用*从文件导入所有内容 +forumTopicId: 301210 +localeTitle: 用 * 从文件中导入所有内容 --- ## Description -
    假设您有一个文件要将其所有内容导入当前文件。这可以使用import *语法完成。这是一个示例,其中名为"math_functions"的文件的内容被导入到同一目录中的文件中:
    从“math_functions”导入*作为myMathModule;
    myMathModule.add(2,3);
    myMathModule.subtract(5,3);
    并打破代码:
    从“file_path_goes_here”导入* as object_with_name_of_your_choice
    object_with_name_of_your_choice.imported_function
    您可以使用import * as后面的任何名称import * as语句的一部分。为了使用此方法,它需要一个接收导入值的对象。从这里,您将使用点表示法来调用导入的值。
    +
    +我们还可以用import语法从文件中导入所有的内容。下面是一个从同目录下的"math_functions"文件中导入所有内容的例子: + +```js +import * as myMathModule from "./math_functions.js"; +``` + +上面的 import 语句会创建一个叫做 myMathModule 的对象。这只是一个变量名,可以随便命名。对象包含 math_functions.js 文件里的所有导出,可以像访问对象的属性那样访问里面的函数。下面是使用导入的 addsubtract 函数的例子: + +```js +myMathModule.add(2,3); +myMathModule.subtract(5,3); +``` + +
    ## Instructions -
    下面的代码需要在导入的同一目录中找到的文件"capitalize_strings"的内容。使用提供的对象将相应的import *语句添加到文件的顶部。
    +
    +下面的代码需要从同目录下的"string_functions"文件中导入所有内容。使用提供的对象,在当前文件的顶部添加正确的import *语句 +
    ## Tests
    @@ -19,45 +35,34 @@ localeTitle: 使用*从文件导入所有内容 tests: - text: 正确使用import * as语法。 testString: assert(code.match(/import\s*\*\s*as\s+stringFunctions\s+from\s*('|")\.\/string_functions\.js\1/g)); - ```
    ## Challenge Seed
    -
    ```js -"use strict"; +// add code above this line + +stringFunctions.uppercaseString("hello"); +stringFunctions.lowercaseString("WORLD!"); ```
    - -### Before Test -
    - -```js -window.require = function(str) { -if (str === 'capitalize_strings') { -return { -capitalize: str => str.toUpperCase(), -lowercase: str => str.toLowerCase() -}}}; - -``` - -
    - -
    ## Solution
    ```js -// solution required +import * as stringFunctions from "./string_functions.js"; +// add code above this line + +stringFunctions.uppercaseString("hello"); +stringFunctions.lowercaseString("WORLD!"); ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-arrow-functions-to-write-concise-anonymous-functions.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-arrow-functions-to-write-concise-anonymous-functions.chinese.md index 3ee399edf1..289407c635 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-arrow-functions-to-write-concise-anonymous-functions.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-arrow-functions-to-write-concise-anonymous-functions.chinese.md @@ -2,30 +2,59 @@ id: 587d7b87367417b2b2512b43 title: Use Arrow Functions to Write Concise Anonymous Functions challengeType: 1 -videoUrl: '' -localeTitle: 使用箭头函数编写简明的匿名函数 +forumTopicId: 301211 +localeTitle: 使用箭头函数编写简洁的匿名函数 --- ## Description -
    在JavaScript中,我们通常不需要命名我们的函数,特别是在将函数作为参数传递给另一个函数时。相反,我们创建内联函数。我们不需要命名这些函数,因为我们不会在其他任何地方重用它们。为此,我们经常使用以下语法:
    const myFunc = function(){
    const myVar =“value”;
    返回myVar;
    }
    ES6为我们提供了语法糖,而不必以这种方式编写匿名函数。相反,您可以使用箭头函数语法
    const myFunc =()=> {
    const myVar =“value”;
    返回myVar;
    }
    当没有函数体,并且只有返回值时,箭头函数语法允许您省略关键字return以及代码周围的括号。这有助于将较小的函数简化为单行语句:
    const myFunc =()=>“value”
    默认情况下,此代码仍将返回value
    +
    +在 JavaScript 里,我们会经常遇到不需要给函数命名的情况,尤其是在需要将一个函数作为参数传给另外一个函数的时候。这时,我们会创建匿名函数。因为这些函数不会在其他地方复用,所以我们不需要给它们命名。 +这种情况下,我们通常会使用以下语法: + +```js +const myFunc = function() { + const myVar = "value"; + return myVar; +} +``` + +ES6 提供了其他写匿名函数的方式的语法糖。你可以使用箭头函数: + +```js +const myFunc = () => { + const myVar = "value"; + return myVar; +} +``` + +当不需要函数体,只返回一个值的时候,箭头函数允许你省略return关键字和外面的大括号。这样就可以将一个简单的函数简化成一个单行语句。 + +```js +const myFunc = () => "value"; +``` + +这段代码仍然会返回value。 +
    ## Instructions -
    重写分配给变量magic的函数,该函数返回一个新的Date()以使用箭头函数语法。还要确保使用关键字var定义任何内容。
    +
    +使用箭头函数的语法重写magic函数,使其返回一个新的Date()。同时不要用var关键字来定义任何变量。 +
    ## Tests
    ```yml tests: - - text: 用户确实替换了var关键字。 + - text: 替换掉var关键字。 testString: getUserInput => assert(!getUserInput('index').match(/var/g)); - - text: magic应该是一个常量变量(通过使用const )。 + - text: magic应该为一个常量 (使用const)。 testString: getUserInput => assert(getUserInput('index').match(/const\s+magic/g)); - - text: magic是一种function 。 + - text: magic是一个function。 testString: assert(typeof magic === 'function'); - text: magic()返回正确的日期。 testString: assert(magic().setHours(0,0,0,0) === new Date().setHours(0,0,0,0)); - - text: function关键字未使用。 + - text: 不要使用function关键字。 testString: getUserInput => assert(!getUserInput('index').match(/function/g)); ``` @@ -42,7 +71,6 @@ var magic = function() { "use strict"; return new Date(); }; - ``` @@ -55,6 +83,10 @@ var magic = function() {
    ```js -// solution required +const magic = () => { + "use strict"; + return new Date(); +}; ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.chinese.md index 463a5dc721..5169b39d4c 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function.chinese.md @@ -2,28 +2,59 @@ id: 587d7b8b367417b2b2512b53 title: Use class Syntax to Define a Constructor Function challengeType: 1 -videoUrl: '' -localeTitle: 使用类语法定义构造函数 +forumTopicId: 301212 +localeTitle: 使用 class 语法定义构造函数 --- ## Description -
    ES6使用关键字class提供了一种帮助创建对象的新语法。需要注意的是, class语法只是一种语法,而不是面向对象范例的完整的基于类的实现,不像Java,Python或Ruby等语言。在ES5中,我们通常定义一个构造函数function,并使用new关键字实例化一个对象。
    var SpaceShuttle = function(targetPlanet){
    this.targetPlanet = targetPlanet;
    }
    var zeus = new SpaceShuttle('Jupiter');
    类语法只是替换构造函数创建:
    class SpaceShuttle {
    构造(targetPlanet){
    this.targetPlanet = targetPlanet;
    }
    }
    const zeus = new SpaceShuttle('Jupiter');
    请注意, class关键字声明了一个新函数,并添加了一个构造函数,该函数将在调用new调用 - 以创建新对象。
    +
    +ES6 提供了一个新的创建对象的语法,使用关键字class。 +值得注意的是,class只是一个语法糖,它并不像 Java、Python 或者 Ruby 这一类的语言一样,严格履行了面向对象的开发规范。 +在 ES5 里面,我们通常会定义一个构造函数,然后使用 new 关键字来实例化一个对象: + +```js +var SpaceShuttle = function(targetPlanet){ + this.targetPlanet = targetPlanet; +} +var zeus = new SpaceShuttle('Jupiter'); +``` + +class的语法只是简单地替换了构造函数的写法: + +```js +class SpaceShuttle { + constructor(targetPlanet) { + this.targetPlanet = targetPlanet; + } +} +const zeus = new SpaceShuttle('Jupiter'); +``` + +应该注意 class 关键字声明了一个函数,里面添加了一个构造器(constructor)。当调用 new 来创建一个新对象时构造器会被调用。 + +注意:
      +
    • 首字母大写驼峰命名法是 ES6 class 名的惯例,就像上面的 SpaceShuttle
    • +
    • 构造函数是一个特殊的函数,在用 class 创建时来创建和初始化对象。在 JavaScript 算法和数据结构证书的面向对象章节里会更深入介绍。
    +
    ## Instructions -
    使用class关键字并编写适当的构造函数来创建Vegetable类。使用Vegetable可以创建具有属性name的蔬菜对象,以传递给构造函数。
    +
    +使用class关键字,并写出正确的构造函数,来创建Vegetable这个类: +Vegetable这个类可以创建 vegetable 对象,这个对象拥有一个在构造函数中赋值的name属性。 +
    ## Tests
    ```yml tests: - - text: Vegetable应该是一个class具有限定constructor方法。 + - text: Vegetable 应该是一个 class,并在其中定义了constructor方法。 testString: assert(typeof Vegetable === 'function' && typeof Vegetable.constructor === 'function'); - - text: class关键字。 + - text: 使用了class关键字。 testString: assert(code.match(/class/g)); - - text: Vegetable可以实例化。 + - text: Vegetable可以被实例化。 testString: assert(() => {const a = new Vegetable("apple"); return typeof a === 'object';}); - - text: carrot.name应该返回carrot 。 + - text: carrot.name 应该返回 carrot. testString: assert(carrot.name=='carrot'); ``` @@ -36,17 +67,12 @@ 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 Vegetable; -} -const Vegetable = makeClass(); const carrot = new Vegetable('carrot'); console.log(carrot.name); // => should be 'carrot' - ```
    @@ -59,6 +85,12 @@ console.log(carrot.name); // => should be 'carrot'
    ```js -// solution required +class Vegetable { + constructor(name) { + this.name = name; + } +} +const carrot = new Vegetable('carrot'); ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.chinese.md index f0d90cac1d..5c54f8fb86 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.chinese.md @@ -2,26 +2,46 @@ id: 587d7b89367417b2b2512b4b title: Use Destructuring Assignment to Assign Variables from Arrays challengeType: 1 -videoUrl: '' -localeTitle: 使用解构分配从数组中分配变量 +forumTopicId: 301213 +localeTitle: 使用解构赋值从数组中分配变量 --- ## Description -
    ES6使解构数组像解构对象一样简单。扩展运算符和数组解构之间的一个关键区别是扩展运算符将数组的所有内容解包为逗号分隔列表。因此,您无法选择或选择要分配给变量的元素。对阵列进行解构可以让我们做到这一点:
    const [a,b] = [1,2,3,4,5,6];
    console.log(a,b); // 1,2
    变量a被赋予数组的第一个值,而b被赋予数组的第二个值。我们还可以通过使用逗号来访问所需索引,从而在数组中的任何索引处访问该值:
    const [a,b ,,, c] = [1,2,3,4,5,6];
    console.log(a,b,c); // 1,2,5
    +
    +在 ES6 里面,解构数组可以如同解构对象一样简单。 +与数组解构不同,数组的扩展运算会将数组里的所有内容分解成一个由逗号分隔的列表。所以,你不能选择哪个元素来给变量赋值。 +而对数组进行解构却可以让我们做到这一点: + +```js +const [a, b] = [1, 2, 3, 4, 5, 6]; +console.log(a, b); // 1, 2 +``` + +变量a以及b分别被数组的第一、第二个元素赋值。 +我们甚至能在数组解构中使用逗号分隔符,来获取任意一个想要的值: + +```js +const [a, b,,, c] = [1, 2, 3, 4, 5, 6]; +console.log(a, b, c); // 1, 2, 5 +``` + +
    ## Instructions -
    使用解构赋值来交换ab的值,以便a接收存储在b的值,并且b接收存储在a的值。
    +
    +使用数组解构来交换变量ab的值。使ab能分别获得对方的值。 +
    ## Tests
    ```yml tests: - - text: 交换后a值应为6。 + - text: 在交换后,a的值应该为6。 testString: assert(a === 6); - - text: 交换后b值应为8。 + - text: 在交换后,b的值应该为8。 testString: assert(b === 8); - - text: 使用数组解构来交换a和b。 + - text: 使用数组解构来交换ab。 testString: assert(/\[\s*(\w)\s*,\s*(\w)\s*\]\s*=\s*\[\s*\2\s*,\s*\1\s*\]/g.test(code)); ``` @@ -35,15 +55,11 @@ tests: ```js let a = 8, b = 6; -(() => { - "use strict"; - // change code below this line +// change code below this line - // change code above this line -})(); +// change code above this line console.log(a); // should be 6 console.log(b); // should be 8 - ``` @@ -56,6 +72,8 @@ console.log(b); // should be 8
    ```js -// solution required +let a = 8, b = 6; +[a, b] = [b, a]; ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-nested-objects.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-nested-objects.chinese.md index e90eadacf9..2ac159c429 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-nested-objects.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-nested-objects.chinese.md @@ -2,63 +2,102 @@ id: 587d7b89367417b2b2512b4a title: Use Destructuring Assignment to Assign Variables from Nested Objects challengeType: 1 -videoUrl: '' -localeTitle: 使用解构分配从嵌套对象分配变量 +forumTopicId: 301214 +localeTitle: 使用解构赋值从嵌套对象中分配变量 --- ## Description -
    我们可以类似地将嵌套对象解构为变量。请考虑以下代码:
    const a = {
    开始:{x:5,y:6},
    结束:{x:6,y:-9}
    };
    const {start:{x:startX,y:startY}} = a;
    console.log(startX,startY); // 5,6
    在上面的示例中,变量start被赋予a.start的值,该值也是一个对象。
    +
    +同样,我们可以将 嵌套的对象解构到变量中。 + +请看以下代码: + +```js +const user = { + johnDoe: { + age: 34, + email: 'johnDoe@freeCodeCamp.com' + } +}; +``` + +这是解构对象的属性并赋值给相同名字的变量: + +```js +const { johnDoe: { age, email }} = user; +``` + +这是将对象的属性值指定给一个不同的名字: + +```js +const { johnDoe: { age: userAge, email: userEmail }} = user; +``` + +
    ## Instructions -
    用解构赋值来获得maxforecast.tomorrow并将其分配给maxOfTomorrow
    +
    +将两个赋值语句替换成等价的解构赋值。lowTodayhighToday 应该为 LOCAL_FORECASTtoday.lowtoday.high 的值。 +
    ## Tests
    ```yml tests: - - text: maxOfTomorrow等于84.6 - testString: 'assert(getMaxOfTmrw(LOCAL_FORECAST) === 84.6, "maxOfTomorrow equals 84.6");' - - text: 使用嵌套解构 - testString: 'getUserInput => assert(getUserInput("index").match(/\{\s*tomorrow\s*:\s*\{\s*max\s*:\s*maxOfTomorrow\s*\}\s*\}\s*=\s*forecast/g),"nested destructuring was used");' - + - text: 不能使用 ES5 的赋值语句。 + testString: assert(!code.match(/lowToday = LOCAL_FORECAST\.today\.low/g) && !code.match(/highToday = LOCAL_FORECAST\.today.high/g)) + - text: 应该使用解构创建 lowToday 变量。 + testString: assert(code.match(/(var|const|let)\s*{\s*today\s*:\s*{\s*(low\s*:\s*lowToday[^}]*|[^,]*,\s*low\s*:\s*lowToday\s*)}\s*}\s*=\s*LOCAL_FORECAST(;|\s+|\/\/)/g)); + - text: 应该使用解构创建 highToday 变量。 + testString: assert(code.match(/(var|const|let)\s*{\s*today\s*:\s*{\s*(high\s*:\s*highToday[^}]*|[^,]*,\s*high\s*:\s*highToday\s*)}\s*}\s*=\s*LOCAL_FORECAST(;|\s+|\/\/)/g)); ```
    ## Challenge Seed
    -
    ```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
    ```js -// solution required +const LOCAL_FORECAST = { + yesterday: { low: 61, high: 75 }, + today: { low: 64, high: 77 }, + tomorrow: { low: 68, high: 80 } +}; + +// change code below this line + +const { today: { low: lowToday, high: highToday }} = LOCAL_FORECAST; + +// change code above this line + +console.log(highToday); // should be 77 +console.log(highTomorrow); // should be 80 ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects.chinese.md index 89cf263f05..5066ec0e35 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects.chinese.md @@ -2,63 +2,94 @@ id: 587d7b89367417b2b2512b49 title: Use Destructuring Assignment to Assign Variables from Objects challengeType: 1 -videoUrl: '' -localeTitle: 使用解构分配从对象分配变量 +forumTopicId: 301215 +localeTitle: 使用解构赋值从对象中分配变量 --- ## Description -
    我们之前看到扩展运算符如何有效地扩展或解包数组的内容。我们也可以用对象做类似的事情。 解构赋值是一种特殊的语法,用于将直接从对象获取的值整齐地分配给变量。请考虑以下ES5代码:
    var voxel = {x:3.6,y:7.4,z:6.54};
    var x = voxel.x; // x = 3.6
    var y = voxel.y; // y = 7.4
    var z = voxel.z; // z = 6.54
    这是与ES6解构语法相同的赋值语句:
    const {x,y,z} =体素; // x = 3.6,y = 7.4,z = 6.54
    相反,如果你想将voxel.x的值存储到a ,将voxel.yb ,将voxel.zc ,那么你也有这种自由。
    const {x:a,y:b,z:c} =体素// a = 3.6,b = 7.4,c = 6.54
    您可以将其读作“获取字段x并将值复制到a中”,依此类推。
    +
    +可以在解构的属性后添加冒号和新的变量名来给解构的值赋予一个新的变量名。 + +还是以上个例子的对象来举例: + +```js +const user = { name: 'John Doe', age: 34 }; +``` + +这是指定新的变量名的例子: + +```js +const { name: userName, age: userAge } = user; +// userName = 'John Doe', userAge = 34 +``` + +获取到了 user.name 的值并赋值给名为 userName 的变量。 +
    ## Instructions -
    使用解构从输入对象AVG_TEMPERATURES获得明天的平均温度,并在tomorrow将关键值赋值给tempOfTomorrow
    +
    +使用解构赋值语句替换两个赋值语句。确保 HIGH_TEMPERATUREStodaytomorrow 属性赋值给 highTodayhighTomorrow。 +
    ## Tests
    ```yml tests: - - text: getTempOfTmrw(AVG_TEMPERATURES)应为79 - testString: 'assert(getTempOfTmrw(AVG_TEMPERATURES) === 79, "getTempOfTmrw(AVG_TEMPERATURES) should be 79");' - - text: 使用了重新分配的解构 - testString: 'getUserInput => assert(getUserInput("index").match(/\{\s*tomorrow\s*:\s*tempOfTomorrow\s*}\s*=\s*avgTemperatures/g),"destructuring with reassignment was used");' - + - text: 应该移除 ES5 赋值语句。 + testString: assert(!code.match(/highToday = HIGH_TEMPERATURES\.today/g) && !code.match(/highTomorrow = HIGH_TEMPERATURES\.tomorrow/g)) + - text: 应该使用解构赋值语句创建 highToday 变量。 + testString: assert(code.match(/(var|const|let)\s*{\s*(today:\s*highToday[^}]*|[^,]*,\s*today\s*:\s*highToday\s*)}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g)); + - text: 应该使用解构赋值语句创建 highTomorrow 变量。 + testString: assert(code.match(/(var|const|let)\s*{\s*(tomorrow:\s*highTomorrow[^}]*|[^,]*,\s*tomorrow\s*:\s*highTomorrow\s*)}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g)); ```
    ## Challenge Seed
    -
    ```js -const AVG_TEMPERATURES = { - today: 77.5, - tomorrow: 79 +const HIGH_TEMPERATURES = { + yesterday: 75, + today: 77, + tomorrow: 80 }; -function getTempOfTmrw(avgTemperatures) { - "use strict"; - // change code below this line - const tempOfTomorrow = undefined; // change this line - // change code above this line - return tempOfTomorrow; -} +// change code below this line + +const highToday = HIGH_TEMPERATURES.today; +const highTomorrow = HIGH_TEMPERATURES.tomorrow; -console.log(getTempOfTmrw(AVG_TEMPERATURES)); // should be 79 +// change code above this line +console.log(yesterday) // should be not defined +console.log(highToday); // should be 77 +console.log(highTomorrow); // should be 80 ```
    - - -
    ## Solution
    ```js -// solution required +const HIGH_TEMPERATURES = { + yesterday: 75, + today: 77, + tomorrow: 80 +}; + +// change code below this line + +const { today: highToday, tomorrow: highTomorrow } = HIGH_TEMPERATURES; + +// change code above this line + +console.log(highToday); // should be 77 +console.log(highTomorrow); // should be 80 ``` +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-extract-values-from-objects.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-extract-values-from-objects.chinese.md new file mode 100644 index 0000000000..e0f09decb0 --- /dev/null +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-extract-values-from-objects.chinese.md @@ -0,0 +1,101 @@ +--- +id: 5cfa550e84205a357704ccb6 +title: Use Destructuring Assignment to Extract Values from Objects +challengeType: 1 +forumTopicId: 301216 +localeTitle: 使用解构赋值来获取对象的值 +--- + +## Description +
    +解构赋值 是 ES6 引入的新语法,用来从数组和对象中提取值,并优雅的对变量进行赋值。 + +有如下 ES5 代码: + +```js +const user = { name: 'John Doe', age: 34 }; + +const name = user.name; // name = 'John Doe' +const age = user.age; // age = 34 +``` + +下面是使用 ES6 解构赋值的等价语句: + +```js +const { name, age } = user; +// name = 'John Doe', age = 34 +``` + +在这里,nameage 被自动创建并赋予 user 对象相应属性的值。一目了然。 + +解构赋值的参数数量可以任意。 +
    + +## Instructions +
    +把两个赋值语句替换成等价的解构赋值。todaytomorrow 的值应该还为 HIGH_TEMPERATURES 对象的 todaytomorrow 属性的值。 +
    + +## Tests +
    + +```yml +tests: + - text: 应该移除 ES5 赋值语句。 + testString: assert(!code.match(/today = HIGH_TEMPERATURES\.today/g) && !code.match(/tomorrow = HIGH_TEMPERATURES\.tomorrow/g)) + - text: 应该解构创建 today 变量。 + testString: assert(code.match(/(var|let|const)\s*{\s*(today[^}]*|[^,]*,\s*today)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g)); + - text: 应该解构创建 tomorrow 变量。 + testString: assert(code.match(/(var|let|const)\s*{\s*(tomorrow[^}]*|[^,]*,\s*tomorrow)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g)); +``` + +
    + +## Challenge Seed +
    +
    + +```js +const HIGH_TEMPERATURES = { + yesterday: 75, + today: 77, + tomorrow: 80 +}; + +// change code below this line + +const today = HIGH_TEMPERATURES.today; +const tomorrow = HIGH_TEMPERATURES.tomorrow; + +// change code above this line + +console.log(yesterday) // should be not defined +console.log(today); // should be 77 +console.log(tomorrow); // should be 80 +``` + +
    +
    + +## Solution +
    + +```js +const HIGH_TEMPERATURES = { + yesterday: 75, + today: 77, + tomorrow: 80 +}; + +// change code below this line + +const { today, tomorrow } = HIGH_TEMPERATURES; + +// change code above this line + +console.log(yesterday) // should be not defined +console.log(today); // should be 77 +console.log(tomorrow); // should be 80 +``` + +
    diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.chinese.md index 4dfff8bbd2..8c7e0ce5c9 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.chinese.md @@ -2,27 +2,52 @@ id: 587d7b8a367417b2b2512b4d title: Use Destructuring Assignment to Pass an Object as a Function's Parameters challengeType: 1 -videoUrl: '' -localeTitle: 使用解构分配将对象作为函数的参数传递 +forumTopicId: 301217 +localeTitle: 使用解构赋值将对象作为函数的参数传递 --- ## Description -
    在某些情况下,您可以在函数参数本身中对对象进行解构。考虑以下代码:
    const profileUpdate =(profileData)=> {
    const {name,age,nationality,location} = profileData;
    //用这些变量做点什么
    }
    这有效地破坏了发送到函数中的对象。这也可以就地完成:
    const profileUpdate =({name,age,nationality,location})=> {
    / *用这些字段做某事* /
    }
    这将删除一些额外的行,使我们的代码看起来整洁。这具有额外的好处,即不必操纵函数中的整个对象;只有所需的字段才会复制到函数内部。
    +
    +在某些情况下,你可以在函数的参数里直接解构对象。 +请看以下代码: + +```js +const profileUpdate = (profileData) => { + const { name, age, nationality, location } = profileData; + // do something with these variables +} +``` + +上面的操作解构了传给函数的对象。这样的操作也可以直接在参数里完成: + +```js +const profileUpdate = ({ name, age, nationality, location }) => { + /* do something with these fields */ +} +``` + +这样的操作去除了多余的代码,使代码更加整洁。 +这样做还有个额外的好处:函数不需要再去操作整个对象,而仅仅是操作复制到函数作用域内部的参数。 +
    ## Instructions -
    在函数half的参数内使用解构赋值,仅在函数内发送maxmin
    +
    +对half的参数进行解构赋值,使得仅仅将maxmin的值传进函数。 +
    ## Tests
    ```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]
    变量ab从数组中获取第一个和第二个值。之后,由于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] +``` + +变量ab分别获取了数组的前两个元素的值。之后,因为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 -
    您可以从对象获取值,并在对象中设置属性的值。这些通常被称为gettersetter 。 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类。构造函数接受华氏温度。现在在类中创建gettersetter ,以获得摄氏温度。请记住, C = 5/9 * (F - 32)F = C * 9.0 / 5 + 32 ,其中F是华氏温标的温度值,C是摄氏温度相同温度的值注意当你实现这一点,你将在一个等级中跟踪班级内的温度 - 华氏温度或摄氏温度。这是getter或setter的强大功能 - 您正在为另一个用户创建一个API,无论您追踪哪个用户,都可以获得正确的结果。换句话说,您正在从使用者那里抽象出实现细节。
    +
    +使用class关键字来创建Thermostat类,它的构造函数应该可以接收 fahrenheit(华氏温度)作为参数。 +在类中创建 temperature 的 gettersetter,将温度转换成摄氏温度。 +温度转换的公式是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)返回NaNMath.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)会返回NaNMath.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: arr2arr1正确副本。 + - 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: 应使用mapfilterreduce - 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 -``` -