diff --git a/02-javascript-algorithms-and-data-structures/functional-programming.json b/02-javascript-algorithms-and-data-structures/functional-programming.json
index f981533..d6d239b 100644
--- a/02-javascript-algorithms-and-data-structures/functional-programming.json
+++ b/02-javascript-algorithms-and-data-structures/functional-programming.json
@@ -8,24 +8,24 @@
"id": "587d7b8d367417b2b2512b5b",
"title": "Learn About Functional Programming",
"description": [
- "Functional programming is a style of programming where solutions are simple, isolated functions, without any side effects outside of the function scope.",
+ "函数式编程是一种解决方案简单,功能独立,对作用域外没有任何副作用的编程范式。",
"INPUT -> PROCESS -> OUTPUT
",
- "Functional programming is about:",
- "1) Isolated functions - there is no dependence on the state of the program, which includes global variables that are subject to change",
- "2) Pure functions - the same input always gives the same output",
- "3) Functions with limited side effects - any changes, or mutations, to the state of the program outside the function are carefully controlled",
+ "函数式编程:",
+ "1)功能独立——不依赖于程序的状态(比如可能发生变化的全局变量);",
+ "2)纯函数——同一个输入永远能得到同一个输出;",
+ "3)有限的副作用——可以严格地限制函数外部对状态的更改导致的状态变化。",
"
prepareTea
and getTea
functions are already defined for you. Call the getTea
function to get 40 cups of tea for the team, and store them in the tea4TeamFCC
variable."
+ "freeCodeCamp 成员在 love tea 的故事。",
+ "在代码编辑器中,已经为你定义好了prepareTea
和getTea
函数。调用getTea
函数为团队准备 40 杯茶,并将它们存储在tea4TeamFCC
变量里。"
],
"tests": [
{
- "text": "The tea4TeamFCC
variable should hold 40 cups of tea for the team.",
- "testString": "assert(tea4TeamFCC.length === 40, 'The tea4TeamFCC
variable should hold 40 cups of tea for the team.');"
+ "text": "tea4TeamFCC
变量里应有 40 杯为团队准备的茶。",
+ "testString": "assert(tea4TeamFCC.length === 40, 'tea4TeamFCC
变量里应有 40 杯为团队准备的茶。');"
},
{
- "text": "The tea4TeamFCC
variable should hold cups of green tea.",
- "testString": "assert(tea4TeamFCC[0] === 'greenTea', 'The tea4TeamFCC
variable should hold cups of green tea.');"
+ "text": "tea4TeamFCC
变量里应有 greenTea。",
+ "testString": "assert(tea4TeamFCC[0] === 'greenTea', 'tea4TeamFCC
变量里应有 greenTea。');"
}
],
"solutions": [],
@@ -38,15 +38,15 @@
"name": "index",
"contents": [
"/**",
- " * A long process to prepare tea.",
- " * @return {string} A cup of tea.",
+ " * 备茶过程",
+ " * @return {string} 一杯茶",
" **/",
"const prepareTea = () => 'greenTea';",
"",
"/**",
- " * Get given number of cups of tea.",
- " * @param {number} numOfCups Number of required cups of tea.",
- " * @return {ArraygetTea
function from last challenge to handle various tea requests. We can modify getTea
to accept a function as a parameter to be able to change the type of tea it prepares. This makes getTea
more flexible, and gives the programmer more control when client requests change.",
- "But first, let's cover some functional terminology:",
- "Callbacks
are the functions that are slipped or passed into another function to decide the invocation of that function. You may have seen them passed to other methods, for example in filter
, the callback function tells JavaScript the criteria for how to filter an array.",
- "Functions that can be assigned to a variable, passed into another function, or returned from another function just like any other normal value, are called first class
functions. In JavaScript, all functions are first class
functions.",
- "The functions that take a function as an argument, or return a function as a return value are called higher order
functions.",
- "When the functions are passed in to another function or returned from another function, then those functions which gets passed in or returned can be called a lambda
.",
+ "FCC 团队需求有变更,现在想要两种茶:绿茶(green tea)和红茶(black tea)。事实证明,用户需求变更是很常见的。",
+ "基于以上信息,我们需要重构上一节挑战中的getTea
函数来处理多种茶的请求。我们可以修改getTea
接受一个函数作为参数,使它能够修改茶的类型。这让getTea
更灵活,也使需求变更时为程序员提供更多控制权。",
+ "首先,我们将介绍一些术语:",
+ "Callbacks
是被传递到另一个函数中调用的函数。你应该已经在其他函数中看过这个写法,例如在filter
中,回调函数告诉 JavaScript 以什么规则过滤数组。",
+ "函数就像其他正常值一样,可以赋值给变量、传递给另一个函数,或从其它函数返回,这种函数叫做first class
函数。在 JavaScript 中,所有函数都是first class
函数。",
+ "将函数为参数或返回值的函数叫做higher order
函数。",
+ "当函数传递给另一个函数或从另一个函数返回时,那些传入或返回的函数可以叫做lambda
。",
"tea4GreenTeamFCC
and tea4BlackTeamFCC
variables, respectively. Note that the getTea
function has been modified so it now takes a function as the first argument.",
- "Note: The data (the number of cups of tea) is supplied as the last argument. We'll discuss this more in later lessons."
+ "准备 27 杯绿茶和 13 杯红茶,分别存入tea4GreenTeamFCC
和tea4BlackTeamFCC
变量。请注意,getTea
函数已经变了,现在它接收一个函数作为第一个参数。",
+ "注意:数据(茶的数量)作为最后一个参数。我们将在后面的课程中对此进行更多讨论。"
],
"tests": [
{
- "text": "The tea4GreenTeamFCC
variable should hold 27 cups of green tea for the team.",
- "testString": "assert(tea4GreenTeamFCC.length === 27, 'The tea4GreenTeamFCC
variable should hold 27 cups of green tea for the team.');"
+ "text": "tea4GreenTeamFCC
变量应存有为团队准备的 27 杯茶。",
+ "testString": "assert(tea4GreenTeamFCC.length === 27, 'tea4GreenTeamFCC
变量应存有为团队准备的 27 杯茶。');"
},
{
- "text": "The tea4GreenTeamFCC
variable should hold cups of green tea.",
- "testString": "assert(tea4GreenTeamFCC[0] === 'greenTea', 'The tea4GreenTeamFCC
variable should hold cups of green tea.');"
+ "text": "tea4GreenTeamFCC
变量应存有绿茶。",
+ "testString": "assert(tea4GreenTeamFCC[0] === 'greenTea', 'tea4GreenTeamFCC
变量应存有绿茶。');"
},
{
- "text": "The tea4BlackTeamFCC
variable should hold 13 cups of black tea.",
- "testString": "assert(tea4BlackTeamFCC.length === 13, 'The tea4BlackTeamFCC
variable should hold 13 cups of black tea.');"
+ "text": "tea4BlackTeamFCC
变量应存有 13 杯红茶。",
+ "testString": "assert(tea4BlackTeamFCC.length === 13, 'tea4BlackTeamFCC
变量应存有 13 杯红茶。');"
},
{
- "text": "The tea4BlackTeamFCC
variable should hold cups of black tea.",
- "testString": "assert(tea4BlackTeamFCC[0] === 'blackTea', 'The tea4BlackTeamFCC
variable should hold cups of black tea.');"
+ "text": "tea4BlackTeamFCC
变量应存有红茶。",
+ "testString": "assert(tea4BlackTeamFCC[0] === 'blackTea', 'tea4BlackTeamFCC
变量应存有红茶。');"
}
],
"solutions": [],
@@ -115,22 +115,22 @@
"name": "index",
"contents": [
"/**",
- " * A long process to prepare green tea.",
- " * @return {string} A cup of green tea.",
+ " * 绿茶准备过程",
+ " * @return {string} 一杯绿茶",
" **/",
"const prepareGreenTea = () => 'greenTea';",
"",
"/**",
- " * A long process to prepare black tea.",
- " * @return {string} A cup of black tea.",
+ " * 备红茶准备过程",
+ " * @return {string} 一杯红茶",
" **/",
"const prepareBlackTea = () => 'blackTea';",
"",
"/**",
- " * Get given number of cups of tea.",
- " * @param {function():string} prepareTea The type of tea preparing function.",
- " * @param {number} numOfCups Number of required cups of tea.",
- " * @return {Arrayfor
loop that gives exact directions to iterate over the indices of an array.",
- "In contrast, functional programming is a form of declarative programming. You tell the computer what you want done by calling a method or function.",
- "JavaScript offers many predefined methods that handle common tasks so you don't need to write out how the computer should perform them. For example, instead of using the for
loop mentioned above, you could call the map
method which handles the details of iterating over an array. This helps to avoid semantic errors, like the \"Off By One Errors\" that were covered in the Debugging section.",
- "Consider the scenario: you are browsing the web in your browser, and want to track the tabs you have opened. Let's try to model this using some simple object-oriented code.",
- "A Window object is made up of tabs, and you usually have more than one Window open. The titles of each open site in each Window object is held in an array. After working in the browser (opening new tabs, merging windows, and closing tabs), you want to print the tabs that are still open. Closed tabs are removed from the array and new tabs (for simplicity) get added to the end of it.",
- "The code editor shows an implementation of this functionality with functions for tabOpen()
, tabClose()
, and join()
. The array tabs
is part of the Window object that stores the name of the open pages.",
- "['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium', 'new tab', 'Netflix', 'YouTube', 'Vine', 'GMail', 'Work mail', 'Docs', 'freeCodeCamp', 'new tab']
but the output will be slightly different.",
- "Work through the code and see if you can figure out the problem, then advance to the next challenge to learn more."
+ "函数式编程是一种好习惯,它能让代码管理更简单,不受隐藏 bug 影响。在我们开始函数式编程之前,为了更好的突显可能遇到的问题,我们先看看命令式编程。",
+ "类似在英语(和许多其他语言)中,命令式时态用于给出命令,编程中的命令式是给计算机一组语句来执行任务。",
+ "这些语句通常会改变程序的状态,例如更新全局变量,典型的例子就是写一个for
循环,它给出了迭代数组索引的精确方向。",
+ "相反,函数式编程是声明式编程的一种形式,通过调用方法或函数来告诉计算机要做什么。",
+ "JavaScript 提供了许多处理常见任务的方法,所以你无需写出计算机应如何执行它们。例如,你可以用map
函数替代上面提到的for
循环来处理数组迭代。这有助于避免语义错误,如调试章节介绍的\"Off By One Errors\"。",
+ "考虑这样的场景:你正在浏览器中浏览网页,并想操作你打开的标签。下面我们来试试用面向对象的思路来描述这种情景。",
+ "窗口对象由选项卡组成,通常会打开多个窗口。窗口对象中每个打开网站的标题都保存在一个数组中。在对浏览器进行了如打开新标签、合并窗口、关闭标签之类的操作后,你需要输出所有打开的标签。关掉的标签将从数组中删除,新打开的标签(为简单起见)则添加到数组的末尾。",
+ "代码编辑器中显示了此功能的实现,其中包含tabOpen()
,tabClose()
,和 join()
函数。tabs
数组是窗口对象的一部分用于储存打开页面的名称。",
+ "['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium', 'new tab', 'Netflix', 'YouTube', 'Vine', 'GMail', 'Work mail', 'Docs', 'freeCodeCamp', 'new tab']
但输出会略有不同。",
+ "仔细阅读代码,看看你是否能找出问题所在,然后进入下一个挑战,了解更多信息。"
],
"tests": [
{
@@ -191,41 +191,41 @@
"ext": "js",
"name": "index",
"contents": [
- "// tabs is an array of titles of each site open within the window",
+ "// tabs 是窗口中打开网站的标题数组",
"var Window = function(tabs) {",
- " this.tabs = tabs; // we keep a record of the array inside the object",
+ " this.tabs = tabs; // 我们将数组记录在对象内部",
"};",
"",
- "// When you join two windows into one window",
+ "// 当两个窗口合并成一个窗口时",
"Window.prototype.join = function (otherWindow) {",
" this.tabs = this.tabs.concat(otherWindow.tabs);",
" return this;",
"};",
"",
- "// When you open a new tab at the end",
+ "// 在末尾打开一个新标签",
"Window.prototype.tabOpen = function (tab) {",
- " this.tabs.push('new tab'); // let's open a new tab for now",
+ " this.tabs.push('new tab'); // 打开一个新的标签",
" return this;",
"};",
"",
- "// When you close a tab",
+ "// 关闭一个标签",
"Window.prototype.tabClose = function (index) {",
- " var tabsBeforeIndex = this.tabs.splice(0, index); // get the tabs before the tab",
- " var tabsAfterIndex = this.tabs.splice(index); // get the tabs after the tab",
+ " var tabsBeforeIndex = this.tabs.splice(0, index); // 获取前面的标签",
+ " var tabsAfterIndex = this.tabs.splice(index); // 获取后面的标签",
"",
- " this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // join them together ",
+ " this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // 拼接到一起",
" return this;",
" };",
"",
- "// Let's create three browser windows",
- "var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // Your mailbox, drive, and other work sites",
- "var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); // Social sites",
- "var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); // Entertainment sites",
+ "// 创建三个浏览器窗口",
+ "var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // 邮箱、文档及其他与工作相关的网站",
+ "var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); // 社交网站",
+ "var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); // 娱乐网站",
"",
- "// Now perform the tab opening, closing, and other operations",
+ "// 执行标签打开,关闭和其他操作",
"var finalTabs = socialWindow",
- " .tabOpen() // Open a new tab for cat memes",
- " .join(videoWindow.tabClose(2)) // Close third tab in video window, and join",
+ " .tabOpen() // 新开一个 cat memes 的标签",
+ " .join(videoWindow.tabClose(2)) // 在娱乐网站关闭第三个标签,加入数组",
" .join(workWindow.tabClose(1).tabOpen());",
"",
"alert(finalTabs.tabs);"
@@ -239,23 +239,23 @@
"id": "587d7b8e367417b2b2512b5e",
"title": "Avoid Mutations and Side Effects Using Functional Programming",
"description": [
- "If you haven't already figured it out, the issue in the previous challenge was with the splice
call in the tabClose()
function. Unfortunately, splice
changes the original array it is called on, so the second call to it used a modified array, and gave unexpected results.",
- "This is a small example of a much larger pattern - you call a function on a variable, array, or an object, and the function changes the variable or something in the object.",
- "One of the core principle of functional programming is to not change things. Changes lead to bugs. It's easier to prevent bugs knowing that your functions don't change anything, including the function arguments or any global variable.",
- "The previous example didn't have any complicated operations but the splice
method changed the original array, and resulted in a bug.",
- "Recall that in functional programming, changing or altering things is called mutation
, and the outcome is called a side effect
. A function, ideally, should be a pure function
, meaning that it does not cause any side effects.",
- "Let's try to master this discipline and not alter any variable or object in our code.",
+ "如果你还没想通,上一个挑战的问题出在tabClose()
函数里的splice
。不幸的是,splice
修改了调用它的原始数组,所以第二次调用它时是基于修改后的数组,才给出了意料之外的结果。",
+ "这是一个小例子,还有更广义的定义——你在变量,数组或对象上调用一个函数,这个会函数改变对象中的变量或其他东西。",
+ "函数式编程的核心原则之一是不改变任何东西。变化会导致错误。如果一个函数不改变传入的参数、全局变量等数据,那么它造成问题的可能性就会小很多。",
+ "前面的例子没有任何复杂的操作,但是splice
方法改变了原始数组,导致 bug 产生。",
+ "回想一下,在函数式编程中,改变或变更叫做mutation
,这种改变的结果叫做“副作用”(side effect
)。理想情况下,函数应该是不会产生任何副作用的pure function
。",
+ "让我们尝试掌握这个原则:不要改变代码中的任何变量或对象。",
"incrementer
so it returns the value of the global variable fixedValue
increased by one."
+ "填写incrementer
函数的代码,使其返回全局变量fixedValue
的值增加 1。"
],
"tests": [
{
- "text": "Your function incrementer
should not change the value of fixedValue
.",
- "testString": "assert(fixedValue === 4, 'Your function incrementer
should not change the value of fixedValue
.');"
+ "text": "incrementer
函数不能改变fixedValue
的值。",
+ "testString": "assert(fixedValue === 4, '你的incrementer
函数不能改变fixedValue
的值。');"
},
{
- "text": "Your incrementer
function should return a value that is one larger than the fixedValue
value.",
- "testString": "assert(newValue === 5, 'Your incrementer
function should return a value that is one larger than the fixedValue
value.');"
+ "text": "incrementer
函数应返回比fixedValue
变量更大的值。",
+ "testString": "assert(newValue === 5, 'incrementer
函数应返回比fixedValue
变量更大的值。');"
}
],
"solutions": [],
@@ -267,18 +267,18 @@
"ext": "js",
"name": "index",
"contents": [
- "// the global variable",
+ "// 全局变量",
"var fixedValue = 4;",
"",
"function incrementer () {",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" ",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"",
- "var newValue = incrementer(); // Should equal 5",
- "console.log(fixedValue); // Should print 4"
+ "var newValue = incrementer(); // 应等于 5",
+ "console.log(fixedValue); // 应打印 4"
],
"head": [],
"tail": []
@@ -289,28 +289,28 @@
"id": "587d7b8e367417b2b2512b5f",
"title": "Pass Arguments to Avoid External Dependence in a Function",
"description": [
- "The last challenge was a step closer to functional programming principles, but there is still something missing.",
- "We didn't alter the global variable value, but the function incrementer
would not work without the global variable fixedValue
being there.",
- "Another principle of functional programming is to always declare your dependencies explicitly. This means if a function depends on a variable or object being present, then pass that variable or object directly into the function as an argument.",
- "There are several good consequences from this principle. The function is easier to test, you know exactly what input it takes, and it won't depend on anything else in your program.",
- "This can give you more confidence when you alter, remove, or add new code. You would know what you can or cannot change and you can see where the potential traps are.",
- "Finally, the function would always produce the same output for the same set of inputs, no matter what part of the code executes it.",
+ "最后一个挑战是更接近函数式编程原则的挑战,但是仍然缺少一些东西。",
+ "虽然我们没有改变全局变量值,但在没有全局变量fixedValue
情况下,incrementer
函数将不起作用。",
+ "函数式编程的另一个原则是:总是显式声明依赖关系。如果函数依赖于一个变量或对象,那么将该变量或对象作为参数直接传递到函数中。",
+ "这样做会有很多好处,其中一点是让函数更容易测试,因为你确切地知道参数是什么,并且这个参数也不依赖于程序中的任何其他内容。",
+ "其次,这样做可以让你更加自信地更改,删除或添加新代码。因为你很清楚哪些是可以改的,哪些是不可以改的,这样你就知道哪里可能会有潜在的陷阱。",
+ "最后,无论代码的哪一部分执行它,函数总是会为同一组输入生成相同的输出。",
"incrementer
function to clearly declare its dependencies.",
- "Write the incrementer
function so it takes an argument, and then increases the value by one."
+ "更新incrementer
函数,明确声明其依赖项。",
+ "编写incrementer
函数,获取它的参数,然后将值增加 1。"
],
"tests": [
{
- "text": "Your function incrementer
should not change the value of fixedValue
.",
- "testString": "assert(fixedValue === 4, 'Your function incrementer
should not change the value of fixedValue
.');"
+ "text": "incrementer
函数不能修改fixedValue
的值。",
+ "testString": "assert(fixedValue === 4, 'incrementer
函数不能修改fixedValue
的值。');"
},
{
- "text": "Your incrementer
function should take a parameter.",
- "testString": "assert(code.match(/function\\s+?incrementer\\s*?\\(.+?\\)/g), 'Your incrementer
function should take a parameter.');"
+ "text": "incrementer
函数应该接收一个参数。",
+ "testString": "assert(code.match(/function\\s+?incrementer\\s*?\\(.+?\\)/g), 'incrementer
函数应该接收一个参数。');"
},
{
- "text": "Your incrementer
function should return a value that is one larger than the fixedValue
value.",
- "testString": "assert(newValue === 5, 'Your incrementer
function should return a value that is one larger than the fixedValue
value.');"
+ "text": "incrementer
函数应返回比fixedValue
更大的值。",
+ "testString": "assert(newValue === 5, 'incrementer
函数应返回比fixedValue
更大的值。');"
}
],
"solutions": [],
@@ -322,18 +322,18 @@
"ext": "js",
"name": "index",
"contents": [
- "// the global variable",
+ "// 全局变量",
"var fixedValue = 4;",
"",
- "// Add your code below this line",
+ "// 请在本行以下添加你的代码",
"function incrementer () {",
" ",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"",
- "var newValue = incrementer(fixedValue); // Should equal 5",
- "console.log(fixedValue); // Should print 4"
+ "var newValue = incrementer(fixedValue); // 应等于 5",
+ "console.log(fixedValue); // 应打印 4"
],
"head": [],
"tail": []
@@ -344,29 +344,29 @@
"id": "587d7b8f367417b2b2512b60",
"title": "Refactor Global Variables Out of Functions",
"description": [
- "So far, we have seen two distinct principles for functional programming:",
- "1) Don't alter a variable or object - create new variables and objects and return them if need be from a function.",
- "2) Declare function arguments - any computation inside a function depends only on the arguments, and not on any global object or variable.",
- "Adding one to a number is not very exciting, but we can apply these principles when working with arrays or more complex objects.",
+ "目前为止,我们已经看到了函数式编程的两个不同原则:",
+ "1) 不要更改变量或对象——创建新变量和对象,并在需要时从函数返回它们。",
+ "2) 声明函数参数——函数内的任何计算仅取决于参数,而不取决于任何全局对象或变量。",
+ "给数字加 1 不够刺激,我们可以在处理数组或更复杂的对象时应用这些原则。",
"bookList
is not changed inside either function. The add
function should add the given bookName
to the end of an array. The remove
function should remove the given bookName
from an array. Both functions should return an array, and any new parameters should be added before the bookName
one."
+ "重构代码,使全局数组bookList
在任何函数内部都不会被改变。add
函数可以将指定的bookName
增加到数组末尾。remove
函数可以从数组中移除指定bookName
。两个函数都返回数组,并且任何参数都应该添加到bookName
前面。"
],
"tests": [
{
- "text": "bookList
should not change and still equal [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]
.",
- "testString": "assert(JSON.stringify(bookList) === JSON.stringify([\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]), 'bookList
should not change and still equal [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]
.');"
+ "text": "bookList
不应该改变,应等于[\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]
.",
+ "testString": "assert(JSON.stringify(bookList) === JSON.stringify([\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]), 'bookList
不应该改变,应等于[\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]
.');"
},
{
- "text": "newBookList
should equal [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"]
.",
- "testString": "assert(JSON.stringify(newBookList) === JSON.stringify(['The Hound of the Baskervilles', 'On The Electrodynamics of Moving Bodies', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time']), 'newBookList
should equal [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"]
.');"
+ "text": "newBookList
应等于[\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"]
.",
+ "testString": "assert(JSON.stringify(newBookList) === JSON.stringify(['The Hound of the Baskervilles', 'On The Electrodynamics of Moving Bodies', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time']), 'newBookList
应等于[\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"]
.');"
},
{
- "text": "newerBookList
should equal [\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]
.",
- "testString": "assert(JSON.stringify(newerBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae']), 'newerBookList
should equal [\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]
.');"
+ "text": "newerBookList
应等于[\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]
.",
+ "testString": "assert(JSON.stringify(newerBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae']), 'newerBookList
应等于[\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]
.');"
},
{
- "text": "newestBookList
should equal [\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"]
.",
- "testString": "assert(JSON.stringify(newestBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time']), 'newestBookList
should equal [\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"]
.');"
+ "text": "newestBookList
应等于[\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"]
.",
+ "testString": "assert(JSON.stringify(newestBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time']), 'newestBookList
应等于[\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"]
.');"
}
],
"solutions": [],
@@ -382,30 +382,30 @@
"ext": "js",
"name": "index",
"contents": [
- "// the global variable",
+ "// 全局变量",
"var bookList = [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"];",
"",
- "/* This function should add a book to the list and return the list */",
- "// New parameters should come before the bookName one",
+ "/* 这个函数应该增加一本书到列表中,并返回这个列表 */",
+ "// 新参数应在 bookName 之前",
"",
- "// Add your code below this line",
+ "// 请在本行以下添加你的代码",
"function add (bookName) {",
" ",
" return bookList.push(bookName);",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"",
- "/* This function should remove a book from the list and return the list */",
- "// New parameters should come before the bookName one",
+ "/* 这个函数应该移除一本书到列表中,并返回这个列表 */",
+ "// 新参数应在 bookName 之前",
"",
- "// Add your code below this line",
+ "// 请在本行以下添加你的代码",
"function remove (bookName) {",
" if (bookList.indexOf(bookName) >= 0) {",
" ",
" return bookList.splice(0, 1, bookName);",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
" }",
"}",
"",
@@ -424,30 +424,30 @@
"id": "587d7b8f367417b2b2512b61",
"title": "Use the map Method to Extract Data from an Array",
"description": [
- "So far we have learned to use pure functions to avoid side effects in a program. Also, we have seen the value in having a function only depend on its input arguments.",
- "This is only the beginning. As its name suggests, functional programming is centered around a theory of functions.",
- "It would make sense to be able to pass them as arguments to other functions, and return a function from another function. Functions are considered First Class Objects
in JavaScript, which means they can be used like any other object. They can be saved in variables, stored in an object, or passed as function arguments.",
- "Let's start with some simple array functions, which are methods on the array object prototype. In this exercise we are looking at Array.prototype.map()
, or more simply map
.",
- "Remember that the map
method is a way to iterate over each item in an array. It creates a new array (without changing the original one) after applying a callback function to every element.",
+ "目前为止,我们已经学会了使用纯函数来避免程序中的副作用。此外,我们已经看到函数的值仅取决于其输入参数。",
+ "这仅仅是个开始。顾名思义,函数式编程以函数理论为中心。",
+ "能够将它们作为参数传递给其他函数,和从另一个函数返回一个函数是有意义的。函数在 JavaScript 中被视为First Class Objects
,它们可以像任何其他对象一样使用。它们可以保存在变量中,存储在对象中,也可以作为函数参数传递。",
+ "让我们从一些简单的数组函数开始,这些函数是数组对象原型上的方法。在本练习中,我们来了解下数组的map
方法(即Array.prototype.map()
)。",
+ "请记住,map
方法是迭代数组中每一项的方式之一。在对每个元素应用回调函数后,它会创建一个新数组(不改变原来的数组)。",
"watchList
array holds objects with information on several movies. Use map
to pull the title and rating from watchList
and save the new array in the rating
variable. The code in the editor currently uses a for
loop to do this, replace the loop functionality with your map
expression."
+ "watchList
数组保存了包含一些电影信息的对象。使用map
从watchList
中提取标题(title
)和评分(rating
),并将新数组保存在rating
变量里。目前编辑器中的代码是使用for
循环实现,使用map
表达式替换循环功能。"
],
"tests": [
{
- "text": "The watchList
variable should not change.",
- "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'The watchList
variable should not change.');"
+ "text": "watchList
不能被改变",
+ "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'watchList
不能被改变');"
},
{
- "text": "Your code should not use a for
loop.",
- "testString": "assert(!code.match(/for\\s*?\\(.+?\\)/g), 'Your code should not use a for
loop.');"
+ "text": "你的代码不能使用for
循环。",
+ "testString": "assert(!code.match(/for\\s*?\\(.+?\\)/g), '你的代码不能使用for
循环。');"
},
{
- "text": "Your code should use the map
method.",
- "testString": "assert(code.match(/\\.map/g), 'Your code should use the map
method.');"
+ "text": "你的代码应使用map
方法。",
+ "testString": "assert(code.match(/\\.map/g), '你的代码应使用map
方法。');"
},
{
- "text": "rating
should equal [{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}]
.",
- "testString": "assert(JSON.stringify(rating) === JSON.stringify([{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}]), 'rating
should equal [{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}]
.');"
+ "text": "rating
应等于[{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}]
.",
+ "testString": "assert(JSON.stringify(rating) === JSON.stringify([{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}]), 'rating
应等于[{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}]
.');"
}
],
"solutions": [],
@@ -459,7 +459,7 @@
"ext": "js",
"name": "index",
"contents": [
- "// the global variable",
+ "// 全局变量",
"var watchList = [",
" { ",
" \"Title\": \"Inception\",",
@@ -573,14 +573,14 @@
" }",
"];",
"",
- "// Add your code below this line",
+ "// 请在本行以下添加你的代码",
"",
"var rating = [];",
"for(var i=0; i < watchList.length; i++){",
" rating.push({title: watchList[i][\"Title\"], rating: watchList[i][\"imdbRating\"]});",
"}",
"",
- "// Add your code above this line",
+ "// 请在本行以上添加你的代码",
"",
"console.log(rating); "
],
@@ -593,21 +593,21 @@
"id": "587d7b8f367417b2b2512b62",
"title": "Implement map on a Prototype",
"description": [
- "As you have seen from applying Array.prototype.map()
, or simply map()
earlier, the map
method returns an array of the same length as the one it was called on. It also doesn't alter the original array, as long as its callback function doesn't.",
- "In other words, map
is a pure function, and its output depends solely on its inputs. Plus, it takes another function as its argument.",
- "It would teach us a lot about map
to try to implement a version of it that behaves exactly like the Array.prototype.map()
with a for
loop or Array.prototype.forEach()
.",
- "Note: A pure function is allowed to alter local variables defined within its scope, although, it's preferable to avoid that as well.",
+ "我们之前用map
方法(即Array.prototype.map()
)返回一个与调用它的数组长度相同的数组。只要它的回调函数不改变原始数组,它就不会改变原始数组。",
+ "换句话说,map
是一个纯函数,它的输出仅取决于输入的数组和作为参数传入的回调函数。",
+ "为了加深对map
方法的理解,现在我们来用for
或Array.prototype.forEach()
自己实现一下这个方法。",
+ "注意:纯函数可以改变其作用域内定义的局部变量,但我们最好不要这样做。",
"Array.prototype.myMap()
, which should behave exactly like Array.prototype.map()
. You may use a for
loop or the forEach
method."
+ "写一个和Array.prototype.map()
一样的Array.prototype.myMap()
。你可以用for
循环或者forEach
方法。"
],
"tests": [
{
- "text": "new_s
should equal [46, 130, 196, 10]
.",
- "testString": "assert(JSON.stringify(new_s) === JSON.stringify([46, 130, 196, 10]), 'new_s
should equal [46, 130, 196, 10]
.');"
+ "text": "new_s
应等于[46, 130, 196, 10]
。",
+ "testString": "assert(JSON.stringify(new_s) === JSON.stringify([46, 130, 196, 10]), 'new_s
应等于[46, 130, 196, 10]
。');"
},
{
- "text": "Your code should not use the map
method.",
- "testString": "assert(!code.match(/\\.map/g), 'Your code should not use the map
method.');"
+ "text": "不能使用map
方法。",
+ "testString": "assert(!code.match(/\\.map/g), '不能使用map
方法。');"
}
],
"solutions": [],
@@ -619,14 +619,14 @@
"ext": "js",
"name": "index",
"contents": [
- "// the global Array",
+ "// 全局变量",
"var s = [23, 65, 98, 5];",
"",
"Array.prototype.myMap = function(callback){",
" var newArray = [];",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
" return newArray;",
"",
"};",
@@ -644,27 +644,27 @@
"id": "587d7b8f367417b2b2512b63",
"title": "Use the filter Method to Extract Data from an Array",
"description": [
- "Another useful array function is Array.prototype.filter()
, or simply filter()
. The filter
method returns a new array which is at most as long as the original array, but usually has fewer items.",
- "Filter
doesn't alter the original array, just like map
. It takes a callback function that applies the logic inside the callback on each element of the array. If an element returns true based on the criteria in the callback function, then it is included in the new array.",
+ "另一个有用的数组方法是filter()
(即Array.prototype.filter()
)。filter
方法会返回一个长度不大于原始数组的新数组。",
+ "和map
一样,Filter
不会改变原始数组,它接收一个回调函数,将回调内的逻辑应用于数组的每个元素。新数组包含根据回调函数内条件返回 true 的元素。",
"watchList
holds an array of objects with information on several movies. Use a combination of filter
and map
to return a new array of objects with only title
and rating
keys, but where imdbRating
is greater than or equal to 8.0. Note that the rating values are saved as strings in the object and you may want to convert them into numbers to perform mathematical operations on them."
+ "watchList
是包含一些电影信息的对象。结合filter
和map
返回一个只包含title
和rating
属性的新数组,并且imdbRating
值大于或等于 8.0。请注意,评级值在对象中保存为字符串,你可能需要将它转换成数字来执行运算。"
],
"tests": [
{
- "text": "The watchList
variable should not change.",
- "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'The watchList
variable should not change.');"
+ "text": "watchList
应保持不变。",
+ "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'watchList
应保持不变。');"
},
{
- "text": "Your code should use the filter
method.",
- "testString": "assert(code.match(/\\.filter/g), 'Your code should use the filter
method.');"
+ "text": "应使用filter
方法。",
+ "testString": "assert(code.match(/\\.filter/g), '应使用filter
方法。');"
},
{
- "text": "Your code should not use a for
loop.",
- "testString": "assert(!code.match(/for\\s*?\\(.+?\\)/g), 'Your code should not use a for
loop.');"
+ "text": "不能使用for
循环。",
+ "testString": "assert(!code.match(/for\\s*?\\(.+?\\)/g), '不能使用for
循环。');"
},
{
- "text": "filteredList
should equal [{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}]
.",
- "testString": "assert.deepEqual(filteredList, [{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}], 'filteredList
should equal [{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}]
.');"
+ "text": "filteredList
应等于[{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}]
。",
+ "testString": "assert.deepEqual(filteredList, [{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}], 'filteredList
应等于[{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}]
。');"
}
],
"solutions": [],
@@ -676,7 +676,7 @@
"ext": "js",
"name": "index",
"contents": [
- "// the global variable",
+ "// 全局变量",
"var watchList = [",
" { ",
" \"Title\": \"Inception\",",
@@ -790,11 +790,11 @@
" }",
"];",
"",
- "// Add your code below this line",
+ "// 请在本行以下添加你的代码",
"",
"var filteredList;",
"",
- "// Add your code above this line",
+ "// 请在本行以上添加你的代码",
"",
"console.log(filteredList); "
],
@@ -807,19 +807,20 @@
"id": "587d7b8f367417b2b2512b64",
"title": "Implement the filter Method on a Prototype",
"description": [
- "It would teach us a lot about the filter
method if we try to implement a version of it that behaves exactly like Array.prototype.filter()
. It can use either a for
loop or Array.prototype.forEach()
.",
- "Note: A pure function is allowed to alter local variables defined within its scope, although, it's preferable to avoid that as well.",
+ "为了加深对filter
的理解,现在我们来自己实现一下Array.prototype.filter()
方法。",
+ "可以用for
循环或Array.prototype.forEach()
。",
+ "请注意:纯函数可以改变其作用域内定义的局部变量,但我们最好不要这样做。",
"Array.prototype.myFilter()
, which should behave exactly like Array.prototype.filter()
. You may use a for
loop or the Array.prototype.forEach()
method."
+ "编写一个和Array.prototype.filter()
功能一样的Array.prototype.myFilter()
方法。你可以用for
循环或Array.prototype.forEach()
方法。"
],
"tests": [
{
- "text": "new_s
should equal [23, 65, 5]
.",
- "testString": "assert(JSON.stringify(new_s) === JSON.stringify([23, 65, 5]), 'new_s
should equal [23, 65, 5]
.');"
+ "text": "new_s
应等于[23, 65, 5]
。",
+ "testString": "assert(JSON.stringify(new_s) === JSON.stringify([23, 65, 5]), 'new_s
应等于[23, 65, 5]
。');"
},
{
- "text": "Your code should not use the filter
method.",
- "testString": "assert(!code.match(/\\.filter/g), 'Your code should not use the filter
method.');"
+ "text": "不能使用filter
方法。",
+ "testString": "assert(!code.match(/\\.filter/g), '不能使用filter
方法。');"
}
],
"solutions": [],
@@ -831,14 +832,14 @@
"ext": "js",
"name": "index",
"contents": [
- "// the global Array",
+ "// 全局变量",
"var s = [23, 65, 98, 5];",
"",
"Array.prototype.myFilter = function(callback){",
" var newArray = [];",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
" return newArray;",
"",
"};",
@@ -856,32 +857,32 @@
"id": "587d7b90367417b2b2512b65",
"title": "Return Part of an Array Using the slice Method",
"description": [
- "The slice
method returns a copy of certain elements of an array. It can take two arguments, the first gives the index of where to begin the slice, the second is the index for where to end the slice (and it's non-inclusive). If the arguments are not provided, the default is to start at the beginning of the array through the end, which is an easy way to make a copy of the entire array. The slice
method does not mutate the original array, but returns a new one.",
- "Here's an example:",
- "var arr = [\"Cat\", \"Dog\", \"Tiger\", \"Zebra\"];", + "
var newArray = arr.slice(1, 3);
// Sets newArray to [\"Dog\", \"Tiger\"]
slice
方法可以从已有数组中返回指定元素。它接受两个参数,第一个规定从何处开始选取,第二个规定从何处结束选取(不包括该元素)。如果没有传参,则默认为从数组的开头开始到结尾结束,这是复制整个数组的简单方式。slice
返回一个新数组,不会修改原始数组。",
+ "举个例子:",
+ "var arr = [\"Cat\", \"Dog\", \"Tiger\", \"Zebra\"];", "
var newArray = arr.slice(1, 3);
// 将新数组设置为 [\"Dog\", \"Tiger\"]
slice
method in the sliceArray
function to return part of the anim
array given the provided beginSlice
and endSlice
indices. The function should return an array."
+ "在sliceArray
函数中使用slice
方法,给出beginSlice
和endSlice
索引,返回anim
数组的一部分,这个函数应返回一个数组。"
],
"tests": [
{
- "text": "Your code should use the slice
method.",
- "testString": "assert(code.match(/\\.slice/g), 'Your code should use the slice
method.');"
+ "text": "你的代码中应使用slice
方法。",
+ "testString": "assert(code.match(/\\.slice/g), '你的代码中应使用slice
方法。');"
},
{
- "text": "The inputAnim
variable should not change.",
- "testString": "assert(JSON.stringify(inputAnim) === JSON.stringify([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"]), 'The inputAnim
variable should not change.');"
+ "text": "不能改变inputAnim
变量。",
+ "testString": "assert(JSON.stringify(inputAnim) === JSON.stringify([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"]), '不能改变inputAnim
变量。');"
},
{
- "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)
should return [\"Dog\", \"Tiger\"]
.",
- "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)) === JSON.stringify([\"Dog\", \"Tiger\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)
should return [\"Dog\", \"Tiger\"]
.');"
+ "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)
应返回[\"Dog\", \"Tiger\"]
。",
+ "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)) === JSON.stringify([\"Dog\", \"Tiger\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)
应返回[\"Dog\", \"Tiger\"]
。');"
},
{
- "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)
should return [\"Cat\"]
.",
- "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)) === JSON.stringify([\"Cat\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)
should return [\"Cat\"]
.');"
+ "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)
应返回[\"Cat\"]
。",
+ "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)) === JSON.stringify([\"Cat\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)
应返回[\"Cat\"]
。');"
},
{
- "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)
should return [\"Dog\", \"Tiger\", \"Zebra\"]
.",
- "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)) === JSON.stringify([\"Dog\", \"Tiger\", \"Zebra\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)
should return [\"Dog\", \"Tiger\", \"Zebra\"]
.');"
+ "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)
应返回[\"Dog\", \"Tiger\", \"Zebra\"]
。",
+ "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)) === JSON.stringify([\"Dog\", \"Tiger\", \"Zebra\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)
应返回[\"Dog\", \"Tiger\", \"Zebra\"]
。');"
}
],
"solutions": [],
@@ -894,10 +895,10 @@
"name": "index",
"contents": [
"function sliceArray(anim, beginSlice, endSlice) {",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" ",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"var inputAnim = [\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"];",
"sliceArray(inputAnim, 1, 3);"
@@ -911,29 +912,29 @@
"id": "9d7123c8c441eeafaeb5bdef",
"title": "Remove Elements from an Array Using slice Instead of splice",
"description": [
- "A common pattern while working with arrays is when you want to remove items and keep the rest of the array. JavaScript offers the splice
method for this, which takes arguments for the index of where to start removing items, then the number of items to remove. If the second argument is not provided, the default is to remove items through the end. However, the splice
method mutates the original array it is called on. Here's an example:",
- "var cities = [\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"];", - "As we saw in the last challenge, the
cities.splice(3, 1); // Returns \"London\" and deletes it from the cities array
// cities is now [\"Chicago\", \"Delhi\", \"Islamabad\", \"Berlin\"]
slice
method does not mutate the original array, but returns a new one which can be saved into a variable. Recall that the slice
method takes two arguments for the indices to begin and end the slice (the end is non-inclusive), and returns those items in a new array. Using the slice
method instead of splice
helps to avoid any array-mutating side effects.",
+ "使用数组时经常遇到要删除一些元素并保留数组剩余部分的情况。为此,JavaScript 提供了splice
方法,它接收从哪里开始删除项目的索引,和要删除的项目数两个参数。如果没有提供第二个参数,默认情况下是移除到结尾的元素。但splice
方法会改变调用它的原始数组。举个例子:",
+ "var cities = [\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"];", + "正如我们在上一次挑战中看到的那样,
cities.splice(3, 1); // 返回 \"London\" 并将它从 cities 数组删除
// cities 现在是 [\"Chicago\", \"Delhi\", \"Islamabad\", \"Berlin\"]
slice
方法不会改变原始数组,而是返回一个可以保存到变量中的新数组。回想一下,slice
方法接收两个参数,从开始索引开始选取到结束(不包括该元素),并在新数组中返回这些元素。使用slice
方法替代splice
有助于避免数组变化产生的副作用。",
"nonMutatingSplice
by using slice
instead of splice
. It should limit the provided cities
array to a length of 3, and return a new array with only the first three items.",
- "Do not mutate the original array provided to the function."
+ "用slice
代替splice
重写nonMutatingSplice
函数。将cities
数组长度限制为3,并返回一个仅包含前 3 项的新数组。",
+ "不要改变提供给函数的原始数组。"
],
"tests": [
{
- "text": "Your code should use the slice
method.",
- "testString": "assert(code.match(/\\.slice/g), 'Your code should use the slice
method.');"
+ "text": "你应该使用slice
方法。",
+ "testString": "assert(code.match(/\\.slice/g), '你应该使用slice
方法。');"
},
{
- "text": "Your code should not use the splice
method.",
- "testString": "assert(!code.match(/\\.splice/g), 'Your code should not use the splice
method.');"
+ "text": "你不应该使用splice
方法。",
+ "testString": "assert(!code.match(/\\.splice/g), '你不应该使用splice
方法。;"
},
{
- "text": "The inputCities
array should not change.",
- "testString": "assert(JSON.stringify(inputCities) === JSON.stringify([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"]), 'The inputCities
array should not change.');"
+ "text": "不能改变inputCities
数组。",
+ "testString": "assert(JSON.stringify(inputCities) === JSON.stringify([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"]), '不能改变inputCities
数组。');"
},
{
- "text": "nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])
should return [\"Chicago\", \"Delhi\", \"Islamabad\"]
.",
- "testString": "assert(JSON.stringify(nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])) === JSON.stringify([\"Chicago\", \"Delhi\", \"Islamabad\"]), 'nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])
should return [\"Chicago\", \"Delhi\", \"Islamabad\"]
.');"
+ "text": "nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])
应返回[\"Chicago\", \"Delhi\", \"Islamabad\"]
。",
+ "testString": "assert(JSON.stringify(nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])) === JSON.stringify([\"Chicago\", \"Delhi\", \"Islamabad\"]), 'nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])
应返回[\"Chicago\", \"Delhi\", \"Islamabad\"]
。');"
}
],
"solutions": [],
@@ -946,10 +947,10 @@
"name": "index",
"contents": [
"function nonMutatingSplice(cities) {",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" return cities.splice(3);",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"var inputCities = [\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"];",
"nonMutatingSplice(inputCities);"
@@ -963,27 +964,27 @@
"id": "587d7da9367417b2b2512b66",
"title": "Combine Two Arrays Using the concat Method",
"description": [
- "Concatenation
means to join items end to end. JavaScript offers the concat
method for both strings and arrays that work in the same way. For arrays, the method is called on one, then another array is provided as the argument to concat
, which is added to the end of the first array. It returns a new array and does not mutate either of the original arrays. Here's an example:",
- "[1, 2, 3].concat([4, 5, 6]);", + "
// Returns a new array [1, 2, 3, 4, 5, 6]
Concatenation
意思是连接元素到尾部。同理,JavaScript 为字符串和数组提供了concat
方法。对数组来说,在一个数组上调用concat
方法,然后提供另一个数组作为参数添加到第一个数组末尾,返回一个新数组,不会改变任何一个原始数组。举个例子:",
+ "[1, 2, 3].concat([4, 5, 6]);", "
// 返回新数组 [1, 2, 3, 4, 5, 6]
concat
method in the nonMutatingConcat
function to concatenate attach
to the end of original
. The function should return the concatenated array."
+ "在nonMutatingConcat
函数里使用concat
,将attach
拼接到original
尾部,返回拼接后的数组。"
],
"tests": [
{
- "text": "Your code should use the concat
method.",
- "testString": "assert(code.match(/\\.concat/g), 'Your code should use the concat
method.');"
+ "text": "你应该使用concat
方法。",
+ "testString": "assert(code.match(/\\.concat/g), '你应该使用concat
方法。');"
},
{
- "text": "The first
array should not change.",
- "testString": "assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), 'The first
array should not change.');"
+ "text": "不能改变first
数组。",
+ "testString": "assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), '不能改变first
数组。');"
},
{
- "text": "The second
array should not change.",
- "testString": "assert(JSON.stringify(second) === JSON.stringify([4, 5]), 'The second
array should not change.');"
+ "text": "不能改变second
数组。",
+ "testString": "assert(JSON.stringify(second) === JSON.stringify([4, 5]), '不能改变second
数组。');"
},
{
- "text": "nonMutatingConcat([1, 2, 3], [4, 5])
should return [1, 2, 3, 4, 5]
.",
- "testString": "assert(JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), 'nonMutatingConcat([1, 2, 3], [4, 5])
should return [1, 2, 3, 4, 5]
.');"
+ "text": "nonMutatingConcat([1, 2, 3], [4, 5])
应返回[1, 2, 3, 4, 5]
。",
+ "testString": "assert(JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), 'nonMutatingConcat([1, 2, 3], [4, 5])
应返回[1, 2, 3, 4, 5]
。');"
}
],
"solutions": [],
@@ -996,10 +997,10 @@
"name": "index",
"contents": [
"function nonMutatingConcat(original, attach) {",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" ",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"var first = [1, 2, 3];",
"var second = [4, 5];",
@@ -1014,33 +1015,33 @@
"id": "587d7da9367417b2b2512b67",
"title": "Add Elements to the End of an Array Using concat Instead of push",
"description": [
- "Functional programming is all about creating and using non-mutating functions.",
- "The last challenge introduced the concat
method as a way to combine arrays into a new one without mutating the original arrays. Compare concat
to the push
method. Push
adds an item to the end of the same array it is called on, which mutates that array. Here's an example:",
- "var arr = [1, 2, 3];", - "
arr.push([4, 5, 6]);
// arr is changed to [1, 2, 3, [4, 5, 6]]
// Not the functional programming way
Concat
offers a way to add new items to the end of an array without any mutating side effects.",
+ "函数式编程就是创建和使用 non-mutating 函数。",
+ "最后一个挑战介绍了concat
方法,这是一种在不改变原始数组的前提下,将数组组合成新数组的方法。将concat
方法与push
方法做比较,Push
将元素添加到调用它的数组的末尾,这样会改变该数组。举个例子:",
+ "var arr = [1, 2, 3];", + "
arr.push([4, 5, 6]);
// arr 变成了 [1, 2, 3, [4, 5, 6]]
// 不是函数式编程
Concat
方法可以将新项目添加到数组末尾,而不产生任何变更副作用。",
"nonMutatingPush
function so it uses concat
to add newItem
to the end of original
instead of push
. The function should return an array."
+ "修改nonMutatingPush
函数,用concat
替代push
将newItem
添加到original
末尾,该函数应返回一个数组。"
],
"tests": [
{
- "text": "Your code should use the concat
method.",
- "testString": "assert(code.match(/\\.concat/g), 'Your code should use the concat
method.');"
+ "text": "应该使用concat
方法。",
+ "testString": "assert(code.match(/\\.concat/g), '应该使用concat
方法。');"
},
{
- "text": "Your code should not use the push
method.",
- "testString": "assert(!code.match(/\\.push/g), 'Your code should not use the push
method.');"
+ "text": "不能使用push
方法。",
+ "testString": "assert(!code.match(/\\.push/g), '不能使用push
方法。');"
},
{
- "text": "The first
array should not change.",
- "testString": "assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), 'The first
array should not change.');"
+ "text": "不能改变first
数组。",
+ "testString": "assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), '不能改变first
数组。');"
},
{
- "text": "The second
array should not change.",
- "testString": "assert(JSON.stringify(second) === JSON.stringify([4, 5]), 'The second
array should not change.');"
+ "text": "不能改变second
数组。",
+ "testString": "assert(JSON.stringify(second) === JSON.stringify([4, 5]), '不能改变second
数组。');"
},
{
- "text": "nonMutatingPush([1, 2, 3], [4, 5])
should return [1, 2, 3, 4, 5]
.",
- "testString": "assert(JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), 'nonMutatingPush([1, 2, 3], [4, 5])
should return [1, 2, 3, 4, 5]
.');"
+ "text": "nonMutatingPush([1, 2, 3], [4, 5])
应返回[1, 2, 3, 4, 5]
。",
+ "testString": "assert(JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), 'nonMutatingPush([1, 2, 3], [4, 5])
应返回[1, 2, 3, 4, 5]
。');"
}
],
"solutions": [],
@@ -1053,10 +1054,10 @@
"name": "index",
"contents": [
"function nonMutatingPush(original, newItem) {",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" return original.push(newItem);",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"var first = [1, 2, 3];",
"var second = [4, 5];",
@@ -1071,29 +1072,29 @@
"id": "587d7da9367417b2b2512b68",
"title": "Use the reduce Method to Analyze Data",
"description": [
- "Array.prototype.reduce()
, or simply reduce()
, is the most general of all array operations in JavaScript. You can solve almost any array processing problem using the reduce
method.",
- "This is not the case with the filter
and map
methods since they do not allow interaction between two different elements of the array. For example, if you want to compare elements of the array, or add them together, filter
or map
could not process that.",
- "The reduce
method allows for more general forms of array processing, and it's possible to show that both filter
and map
can be derived as a special application of reduce
.",
- "However, before we get there, let's practice using reduce
first.",
+ "reduce()
(即Array.prototype.reduce()
),是 JavaScript 所有数组操作中最通用的方法。几乎可以用reduce
方法解决所有数组处理问题。",
+ "filter
和map
方法不支持对数组中两个不同元素的交互。举个例子,如果你想把数组中的元素拿来比较或者相加,用filter
和map
是做不到的。",
+ "reduce
方法允许更通用的数组处理方式,而且filter
和map
方法都可以当作是reduce
的特殊实现。",
+ "然而,在我们介绍它们的特殊实现之前,我们先来练习使用reduce
。",
"watchList
holds an array of objects with information on several movies. Use reduce
to find the average IMDB rating of the movies directed by Christopher Nolan. Recall from prior challenges how to filter
data and map
over it to pull what you need. You may need to create other variables, but save the final average into the variable averageRating
. Note that the rating values are saved as strings in the object and need to be converted into numbers before they are used in any mathematical operations."
+ "watchList
变量中包含一组存有多部电影信息对象。使用reduce
查找由 Christopher Nolan 导演的电影directed by Christopher Nolan的 IMDB 评级。回想一下之前的挑战,如何filter
数据,以及使用map
来获取你想要的数据。你可能需要创建一些变量,但是请将最后的平均值保存到averageRating
变量中。请注意,评级在对象中是字符串,需要将其转换为数字再用于数学运算。"
],
"tests": [
{
- "text": "The watchList
variable should not change.",
- "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'The watchList
variable should not change.');"
+ "text": "watchList
应保持不变。",
+ "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'watchList
应保持不变。');"
},
{
- "text": "Your code should use the reduce
method.",
- "testString": "assert(code.match(/\\.reduce/g), 'Your code should use the reduce
method.');"
+ "text": "应该使用reduce
方法。",
+ "testString": "assert(code.match(/\\.reduce/g), '应该使用reduce
方法。');"
},
{
- "text": "The averageRating
should equal 8.675.",
- "testString": "assert(averageRating == 8.675, 'The averageRating
should equal 8.675.');"
+ "text": "The averageRating
应等于 8.675。",
+ "testString": "assert(averageRating == 8.675, 'The averageRating
应等于 8.675。');"
},
{
- "text": "Your code should not use a for
loop.",
- "testString": "assert(!code.match(/for\\s*?\\(.*\\)/g), 'Your code should not use a for
loop.');"
+ "text": "不能使用for
循环。",
+ "testString": "assert(!code.match(/for\\s*?\\(.*\\)/g), '不能使用for
循环。');"
}
],
"solutions": [],
@@ -1109,7 +1110,7 @@
"ext": "js",
"name": "index",
"contents": [
- "// the global variable",
+ "// 全局变量",
"var watchList = [",
" { ",
" \"Title\": \"Inception\",",
@@ -1223,11 +1224,11 @@
" }",
"];",
"",
- "// Add your code below this line",
+ "// 请在本行以下添加你的代码",
"",
"var averageRating;",
"",
- "// Add your code above this line",
+ "// 请在本行以上添加你的代码",
"",
"console.log(averageRating); "
],
@@ -1240,29 +1241,29 @@
"id": "587d7da9367417b2b2512b69",
"title": "Sort an Array Alphabetically using the sort Method",
"description": [
- "The sort
method sorts the elements of an array according to the callback function.",
- "For example:",
- "function ascendingOrder(arr) {", - "Note: It's encouraged to provide a callback function to specify how to sort the array items. JavaScript's default sorting method is by string Unicode point value, which may return unexpected results.", + "
return arr.sort(function(a, b) {
return a - b;
});
}
ascendingOrder([1, 5, 2, 3, 4]);
// Returns [1, 2, 3, 4, 5]
function reverseAlpha(arr) {
return arr.sort(function(a, b) {
return a < b;
});
}
reverseAlpha(['l', 'h', 'z', 'b', 's']);
// Returns ['z', 's', 'l', 'h', 'b']
sort
方法可以根据回调函数对数组元素进行排序。",
+ "举个例子:",
+ "function ascendingOrder(arr) {", + "注意:提倡使用回调函数来指定如何对数组项进行排序。JavaScript 的默认排序算法是按照 Unicode 字符编码排序的,所以可能会返回意料之外的结果。", "
return arr.sort(function(a, b) {
return a - b;
});
}
ascendingOrder([1, 5, 2, 3, 4]);
// Returns [1, 2, 3, 4, 5]
function reverseAlpha(arr) {
return arr.sort(function(a, b) {
return a < b;
});
}
reverseAlpha(['l', 'h', 'z', 'b', 's']);
// 返回 ['z', 's', 'l', 'h', 'b']
sort
method in the alphabeticalOrder
function to sort the elements of arr
in alphabetical order."
+ "在alphabeticalOrder
函数中使用sort
方法对arr
中的元素按照字母顺序排列。"
],
"tests": [
{
- "text": "Your code should use the sort
method.",
- "testString": "assert(code.match(/\\.sort/g), 'Your code should use the sort
method.');"
+ "text": "应该使用sort
方法。",
+ "testString": "assert(code.match(/\\.sort/g), '应该使用sort
方法。');"
},
{
- "text": "alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])
should return [\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]
.",
- "testString": "assert(JSON.stringify(alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])) === JSON.stringify([\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]), 'alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])
should return [\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]
.');"
+ "text": "alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])
应返回[\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]
。",
+ "testString": "assert(JSON.stringify(alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])) === JSON.stringify([\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]), 'alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])
应返回[\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]
。');"
},
{
- "text": "alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])
should return [\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]
.",
- "testString": "assert(JSON.stringify(alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])) === JSON.stringify([\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]), 'alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])
should return [\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]
.');"
+ "text": "alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])
应返回[\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]
。",
+ "testString": "assert(JSON.stringify(alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])) === JSON.stringify([\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]), 'alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])
应返回[\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]
。');"
},
{
- "text": "alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])
should return [\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]
.",
- "testString": "assert(JSON.stringify(alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])) === JSON.stringify([\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]), 'alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])
should return [\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]
.');"
+ "text": "alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])
应返回[\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]
。",
+ "testString": "assert(JSON.stringify(alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])) === JSON.stringify([\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]), 'alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])
应返回[\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]
。');"
}
],
"solutions": [],
@@ -1275,10 +1276,10 @@
"name": "index",
"contents": [
"function alphabeticalOrder(arr) {",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" ",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"]);"
],
@@ -1291,26 +1292,26 @@
"id": "587d7da9367417b2b2512b6a",
"title": "Return a Sorted Array Without Changing the Original Array",
"description": [
- "A side effect of the sort
method is that it changes the order of the elements in the original array. In other words, it mutates the array in place. One way to avoid this is to first concatenate an empty array to the one being sorted (remember that concat
returns a new array), then run the sort
method.",
+ "sort
方法会产生改变原始数组中元素顺序的副作用。换句话说,它会改变数组的位置。避免这种情况的一种方法是先将空数组连接到正在排序的数组上(记住concat
返回一个新数组),再用sort
方法。",
"sort
method in the nonMutatingSort
function to sort the elements of an array in ascending order. The function should return a new array, and not mutate the globalArray
variable."
+ "在nonMutatingSort
函数中使用sort
方法对数组中的元素按升序进行排列。函数不能改变globalArray
变量,应返回一个新数组。"
],
"tests": [
{
- "text": "Your code should use the sort
method.",
- "testString": "assert(code.match(/\\.sort/g), 'Your code should use the sort
method.');"
+ "text": "应该使用sort
方法。",
+ "testString": "assert(code.match(/\\.sort/g), '应该使用sort
方法。');"
},
{
- "text": "Your code should use the concat
method.",
- "testString": "assert(code.match(/\\.concat/g), 'Your code should use the concat
method.');"
+ "text": "应该使用concat
方法。",
+ "testString": "assert(code.match(/\\.concat/g), '应该使用concat
方法。');"
},
{
- "text": "The globalArray
variable should not change.",
- "testString": "assert(JSON.stringify(globalArray) === JSON.stringify([5, 6, 3, 2, 9]), 'The globalArray
variable should not change.');"
+ "text": "globalArray
variable 应保持不变。",
+ "testString": "assert(JSON.stringify(globalArray) === JSON.stringify([5, 6, 3, 2, 9]), 'globalArray
variable 应保持不变。');"
},
{
- "text": "nonMutatingSort(globalArray)
should return [2, 3, 5, 6, 9]
.",
- "testString": "assert(JSON.stringify(nonMutatingSort(globalArray)) === JSON.stringify([2, 3, 5, 6, 9]), 'nonMutatingSort(globalArray)
should return [2, 3, 5, 6, 9]
.');"
+ "text": "nonMutatingSort(globalArray)
应返回[2, 3, 5, 6, 9]
。",
+ "testString": "assert(JSON.stringify(nonMutatingSort(globalArray)) === JSON.stringify([2, 3, 5, 6, 9]), 'nonMutatingSort(globalArray)
应返回[2, 3, 5, 6, 9]
。');"
}
],
"solutions": [],
@@ -1324,10 +1325,10 @@
"contents": [
"var globalArray = [5, 6, 3, 2, 9];",
"function nonMutatingSort(arr) {",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" ",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"nonMutatingSort(globalArray);"
],
@@ -1340,29 +1341,29 @@
"id": "587d7daa367417b2b2512b6b",
"title": "Split a String into an Array Using the split Method",
"description": [
- "The split
method splits a string into an array of strings. It takes an argument for the delimiter, which can be a character to use to break up the string or a regular expression. For example, if the delimiter is a space, you get an array of words, and if the delimiter is an empty string, you get an array of each character in the string.",
- "Here are two examples that split one string by spaces, then another by digits using a regular expression:",
+ "split
方法用于把字符串分割成字符串数组。接收一个分隔符参数,分隔符可以是用于分解字符串或正则表达式的字符。举个例子,如果分隔符是空格,你会得到一个单词数组;如果分隔符是空字符串,你会得到一个由字符串中每个字符组成的数组。",
+ "下面是两个用空格分隔一个字符串的例子,另一个是用数字的正则表达式分隔:",
"var str = \"Hello World\";", - "Since strings are immutable, the
var bySpace = str.split(\" \");
// Sets bySpace to [\"Hello\", \"World\"]
var otherString = \"How9are7you2today\";
var byDigits = str.split(/\\d/);
// Sets byDigits to [\"How\", \"are\", \"you\", \"today\"]
split
method makes it easier to work with them.",
+ "因为字符串是固定的,split
方法可以更简单的操作它们。",
"split
method inside the splitify
function to split str
into an array of words. The function should return the array. Note that the words are not always separated by spaces, and the array should not contain punctuation."
+ "在splitify
函数中用split
方法将str
分割成单词数组,这个方法应该返回一个数组。单词不一定都是用空格分隔,所以数组中不应包含标点符号。"
],
"tests": [
{
- "text": "Your code should use the split
method.",
- "testString": "assert(code.match(/\\.split/g), 'Your code should use the split
method.');"
+ "text": "应该使用split
方法。",
+ "testString": "assert(code.match(/\\.split/g), '应该使用split
方法。');"
},
{
- "text": "splitify(\"Hello World,I-am code\")
should return [\"Hello\", \"World\", \"I\", \"am\", \"code\"]
.",
- "testString": "assert(JSON.stringify(splitify(\"Hello World,I-am code\")) === JSON.stringify([\"Hello\", \"World\", \"I\", \"am\", \"code\"]), 'splitify(\"Hello World,I-am code\")
should return [\"Hello\", \"World\", \"I\", \"am\", \"code\"]
.');"
+ "text": "splitify(\"Hello World,I-am code\")
应返回[\"Hello\", \"World\", \"I\", \"am\", \"code\"]
。",
+ "testString": "assert(JSON.stringify(splitify(\"Hello World,I-am code\")) === JSON.stringify([\"Hello\", \"World\", \"I\", \"am\", \"code\"]), 'splitify(\"Hello World,I-am code\")
应返回[\"Hello\", \"World\", \"I\", \"am\", \"code\"]
。');"
},
{
- "text": "splitify(\"Earth-is-our home\")
should return [\"Earth\", \"is\", \"our\", \"home\"]
.",
- "testString": "assert(JSON.stringify(splitify(\"Earth-is-our home\")) === JSON.stringify([\"Earth\", \"is\", \"our\", \"home\"]), 'splitify(\"Earth-is-our home\")
should return [\"Earth\", \"is\", \"our\", \"home\"]
.');"
+ "text": "splitify(\"Earth-is-our home\")
应返回[\"Earth\", \"is\", \"our\", \"home\"]
。",
+ "testString": "assert(JSON.stringify(splitify(\"Earth-is-our home\")) === JSON.stringify([\"Earth\", \"is\", \"our\", \"home\"]), 'splitify(\"Earth-is-our home\")
应返回[\"Earth\", \"is\", \"our\", \"home\"]
。');"
},
{
- "text": "splitify(\"This.is.a-sentence\")
should return [\"This\", \"is\", \"a\", \"sentence\"]
.",
- "testString": "assert(JSON.stringify(splitify(\"This.is.a-sentence\")) === JSON.stringify([\"This\", \"is\", \"a\", \"sentence\"]), 'splitify(\"This.is.a-sentence\")
should return [\"This\", \"is\", \"a\", \"sentence\"]
.');"
+ "text": "splitify(\"This.is.a-sentence\")
应返回[\"This\", \"is\", \"a\", \"sentence\"]
。",
+ "testString": "assert(JSON.stringify(splitify(\"This.is.a-sentence\")) === JSON.stringify([\"This\", \"is\", \"a\", \"sentence\"]), 'splitify(\"This.is.a-sentence\")
应返回[\"This\", \"is\", \"a\", \"sentence\"]
。');"
}
],
"solutions": [],
@@ -1377,10 +1378,10 @@
"name": "index",
"contents": [
"function splitify(str) {",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" ",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"splitify(\"Hello World,I-am code\");"
],
@@ -1393,36 +1394,36 @@
"id": "587d7daa367417b2b2512b6c",
"title": "Combine an Array into a String Using the join Method",
"description": [
- "The join
method is used to join the elements of an array together to create a string. It takes an argument for the delimiter that is used to separate the array elements in the string.",
- "Here's an example:",
+ "join
方法用来把数组中的所有元素放入一个字符串,通过指定的分隔符参数进行分隔。",
+ "举个例子:",
"var arr = [\"Hello\", \"World\"];", "
var str = arr.join(\" \");
// Sets str to \"Hello World\"
join
method (among others) inside the sentensify
function to make a sentence from the words in the string str
. The function should return a string. For example, \"I-like-Star-Wars\" would be converted to \"I like Star Wars\". For this challenge, do not use the replace
method."
+ "在函数sentensify
内用join
方法(及其他方法)用字符串str
中的单词造句,这个函数应返回一个字符串。举个例子,\"I-like-Star-Wars\" 会被转换成 \"I like Star Wars\"。在此挑战中请勿使用replace
方法。"
],
"tests": [
{
- "text": "Your code should use the join
method.",
- "testString": "assert(code.match(/\\.join/g), 'Your code should use the join
method.');"
+ "text": "应该使用join
方法。",
+ "testString": "assert(code.match(/\\.join/g), '应该使用join
方法。');"
},
{
- "text": "Your code should not use the replace
method.",
- "testString": "assert(!code.match(/\\.replace/g), 'Your code should not use the replace
method.');"
+ "text": "不能使用replace
方法。",
+ "testString": "assert(!code.match(/\\.replace/g), '不能使用replace
方法。');"
},
{
- "text": "sentensify(\"May-the-force-be-with-you\")
should return a string.",
- "testString": "assert(typeof sentensify(\"May-the-force-be-with-you\") === \"string\", 'sentensify(\"May-the-force-be-with-you\")
should return a string.');"
+ "text": "sentensify(\"May-the-force-be-with-you\")
应返回一个字符串",
+ "testString": "assert(typeof sentensify(\"May-the-force-be-with-you\") === \"string\", 'sentensify(\"May-the-force-be-with-you\")
应返回一个字符串');"
},
{
- "text": "sentensify(\"May-the-force-be-with-you\")
should return \"May the force be with you\"
.",
- "testString": "assert(sentensify(\"May-the-force-be-with-you\") === \"May the force be with you\", 'sentensify(\"May-the-force-be-with-you\")
should return \"May the force be with you\"
.');"
+ "text": "sentensify(\"May-the-force-be-with-you\")
应返回\"May the force be with you\"
。",
+ "testString": "assert(sentensify(\"May-the-force-be-with-you\") === \"May the force be with you\", 'sentensify(\"May-the-force-be-with-you\")
应返回\"May the force be with you\"
。');"
},
{
- "text": "sentensify(\"The.force.is.strong.with.this.one\")
should return \"The force is strong with this one\"
.",
- "testString": "assert(sentensify(\"The.force.is.strong.with.this.one\") === \"The force is strong with this one\", 'sentensify(\"The.force.is.strong.with.this.one\")
should return \"The force is strong with this one\"
.');"
+ "text": "sentensify(\"The.force.is.strong.with.this.one\")
应返回\"The force is strong with this one\"
。",
+ "testString": "assert(sentensify(\"The.force.is.strong.with.this.one\") === \"The force is strong with this one\", 'sentensify(\"The.force.is.strong.with.this.one\")
应返回\"The force is strong with this one\"
。');"
},
{
- "text": "sentensify(\"There,has,been,an,awakening\")
should return \"There has been an awakening\"
.",
- "testString": "assert(sentensify(\"There,has,been,an,awakening\") === \"There has been an awakening\", 'sentensify(\"There,has,been,an,awakening\")
should return \"There has been an awakening\"
.');"
+ "text": "sentensify(\"There,has,been,an,awakening\")
应返回\"There has been an awakening\"
。",
+ "testString": "assert(sentensify(\"There,has,been,an,awakening\") === \"There has been an awakening\", 'sentensify(\"There,has,been,an,awakening\")
应返回\"There has been an awakening\"
。');"
}
],
"solutions": [],
@@ -1437,10 +1438,10 @@
"name": "index",
"contents": [
"function sentensify(str) {",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" ",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"sentensify(\"May-the-force-be-with-you\");"
],
@@ -1453,40 +1454,40 @@
"id": "587d7dab367417b2b2512b6d",
"title": "Apply Functional Programming to Convert Strings to URL Slugs",
"description": [
- "The last several challenges covered a number of useful array and string methods that follow functional programming principles. We've also learned about reduce
, which is a powerful method used to reduce problems to simpler forms. From computing averages to sorting, any array operation can be achieved by applying it. Recall that map
and filter
are special cases of reduce
.",
- "Let's combine what we've learned to solve a practical problem.",
- "Many content management sites (CMS) have the titles of a post added to part of the URL for simple bookmarking purposes. For example, if you write a Medium post titled \"Stop Using Reduce\", it's likely the URL would have some form of the title string in it (\".../stop-using-reduce\"). You may have already noticed this on the freeCodeCamp site.",
+ "最后几个挑战中涵盖了许多符合函数式编程原则并在处理数组和字符串中非常有用的方法。我们还学习了强大的、可以将问题简化为更简单形式的reduce
方法,从计算平均值到排序,任何数组操作都可以用它来实现。回想一下,map
和filter
方法都是reduce
的特殊实现。",
+ "让我们把学到的知识结合起来解决一个实际问题。",
+ "许多内容管理站点(CMS)为了让添加书签更简单,会将帖子的标题添加到 URL 上。举个例子,如果你写了一篇标题为 \"Stop Using Reduce\" 的帖子,URL很可能会包含某种形式的标题字符串 (如:\".../stop-using-reduce\"),你可能已经在 freeCodeCamp 网站上注意到了这一点。",
"urlSlug
function so it converts a string title
and returns the hyphenated version for the URL. You can use any of the methods covered in this section, and don't use replace
. Here are the requirements:",
- "The input is a string with spaces and title-cased words",
- "The output is a string with the spaces between words replaced by a hyphen (-
)",
- "The output should be all lower-cased letters",
- "The output should not have any spaces"
+ "填写urlSlug
函数,使其转换字符串title
带有连字符号的 URL 版本。您可以使用本节中介绍的任何方法,但不要用replace
方法。以下是本次挑战的要求:",
+ "输入包含空格和标题大小写单词的字符串",
+ "输出字符串,单词之间的空格用连字符(-
)替换",
+ "输出应该是小写字母",
+ "输出不应有任何空格"
],
"tests": [
{
- "text": "The globalTitle
variable should not change.",
- "testString": "assert(globalTitle === \"Winter Is Coming\", 'The globalTitle
variable should not change.');"
+ "text": "globalTitle
变量应保持不变。",
+ "testString": "assert(globalTitle === \"Winter Is Coming\", 'globalTitle
变量应保持不变。');"
},
{
- "text": "Your code should not use the replace
method for this challenge.",
- "testString": "assert(!code.match(/\\.replace/g), 'Your code should not use the replace
method for this challenge.');"
+ "text": "在此挑战中不能使用replace
方法。",
+ "testString": "assert(!code.match(/\\.replace/g), '在此挑战中不能使用replace
方法。');"
},
{
- "text": "urlSlug(\"Winter Is Coming\")
should return \"winter-is-coming\"
.",
- "testString": "assert(urlSlug(\"Winter Is Coming\") === \"winter-is-coming\", 'urlSlug(\"Winter Is Coming\")
should return \"winter-is-coming\"
.');"
+ "text": "urlSlug(\"Winter Is Coming\")
应返回\"winter-is-coming\"
。",
+ "testString": "assert(urlSlug(\"Winter Is Coming\") === \"winter-is-coming\", 'urlSlug(\"Winter Is Coming\")
应返回\"winter-is-coming\"
。');"
},
{
- "text": "urlSlug(\" Winter Is Coming\")
should return \"winter-is-coming\"
.",
- "testString": "assert(urlSlug(\" Winter Is Coming\") === \"winter-is-coming\", 'urlSlug(\" Winter Is Coming\")
should return \"winter-is-coming\"
.');"
+ "text": "urlSlug(\" Winter Is Coming\")
应返回\"winter-is-coming\"
。",
+ "testString": "assert(urlSlug(\" Winter Is Coming\") === \"winter-is-coming\", 'urlSlug(\" Winter Is Coming\")
应返回\"winter-is-coming\"
。');"
},
{
- "text": "urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\")
should return \"a-mind-needs-books-like-a-sword-needs-a-whetstone\"
.",
- "testString": "assert(urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\") === \"a-mind-needs-books-like-a-sword-needs-a-whetstone\", 'urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\")
should return \"a-mind-needs-books-like-a-sword-needs-a-whetstone\"
.');"
+ "text": "urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\")
应返回\"a-mind-needs-books-like-a-sword-needs-a-whetstone\"
。",
+ "testString": "assert(urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\") === \"a-mind-needs-books-like-a-sword-needs-a-whetstone\", 'urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\")
应返回\"a-mind-needs-books-like-a-sword-needs-a-whetstone\"
。');"
},
{
- "text": "urlSlug(\"Hold The Door\")
should return \"hold-the-door\"
.",
- "testString": "assert(urlSlug(\"Hold The Door\") === \"hold-the-door\", 'urlSlug(\"Hold The Door\")
should return \"hold-the-door\"
.');"
+ "text": "urlSlug(\"Hold The Door\")
应返回\"hold-the-door\"
。",
+ "testString": "assert(urlSlug(\"Hold The Door\") === \"hold-the-door\", 'urlSlug(\"Hold The Door\")
应返回\"hold-the-door\"
。');"
}
],
"solutions": [],
@@ -1501,17 +1502,17 @@
"ext": "js",
"name": "index",
"contents": [
- "// the global variable",
+ "// 全局变量",
"var globalTitle = \"Winter Is Coming\";",
"",
- "// Add your code below this line",
+ "// 请在本行以下添加你的代码",
"function urlSlug(title) {",
" ",
" ",
"}",
- "// Add your code above this line",
+ "// 请在本行以上添加你的代码",
"",
- "var winterComing = urlSlug(globalTitle); // Should be \"winter-is-coming\""
+ "var winterComing = urlSlug(globalTitle); // 应为 \"winter-is-coming\""
],
"head": [],
"tail": []
@@ -1522,28 +1523,28 @@
"id": "587d7dab367417b2b2512b6e",
"title": "Use the every Method to Check that Every Element in an Array Meets a Criteria",
"description": [
- "The every
method works with arrays to check if every element passes a particular test. It returns a Boolean value - true
if all values meet the criteria, false
if not.",
- "For example, the following code would check if every element in the numbers
array is less than 10:",
- "var numbers = [1, 5, 8, 0, 10, 11];", + "
numbers.every(function(currentValue) {
return currentValue < 10;
});
// Returns false
every
方法用于检测数组所有元素是否都符合指定条件。如果所有元素满足条件,返回布尔值true
,反之返回false
。",
+ "举个例子,下面的代码检测数组numbers
的所有元素是否都小于 10:",
+ "var numbers = [1, 5, 8, 0, 10, 11];", "
numbers.every(function(currentValue) {
return currentValue < 10;
});
// 返回 false
every
method inside the checkPositive
function to check if every element in arr
is positive. The function should return a Boolean value."
+ "在checkPositive
函数中使用every
方法检查arr
中是否所有元素都是正数,函数应返回一个布尔值。"
],
"tests": [
{
- "text": "Your code should use the every
method.",
- "testString": "assert(code.match(/\\.every/g), 'Your code should use the every
method.');"
+ "text": "应该使用every
方法。",
+ "testString": "assert(code.match(/\\.every/g), '应该使用every
方法。');"
},
{
- "text": "checkPositive([1, 2, 3, -4, 5])
should return false
.",
- "testString": "assert(!checkPositive([1, 2, 3, -4, 5]), 'checkPositive([1, 2, 3, -4, 5])
should return false
.');"
+ "text": "checkPositive([1, 2, 3, -4, 5])
应返回false
。",
+ "testString": "assert(!checkPositive([1, 2, 3, -4, 5]), 'checkPositive([1, 2, 3, -4, 5])
应返回false
。');"
},
{
- "text": "checkPositive([1, 2, 3, 4, 5])
should return true
.",
- "testString": "assert(checkPositive([1, 2, 3, 4, 5]), 'checkPositive([1, 2, 3, 4, 5])
should return true
.');"
+ "text": "checkPositive([1, 2, 3, 4, 5])
应返回true
。",
+ "testString": "assert(checkPositive([1, 2, 3, 4, 5]), 'checkPositive([1, 2, 3, 4, 5])
应返回true
。');"
},
{
- "text": "checkPositive([1, -2, 3, -4, 5])
should return false
.",
- "testString": "assert(!checkPositive([1, -2, 3, -4, 5]), 'checkPositive([1, -2, 3, -4, 5])
should return false
.');"
+ "text": "checkPositive([1, -2, 3, -4, 5])
应返回false
。",
+ "testString": "assert(!checkPositive([1, -2, 3, -4, 5]), 'checkPositive([1, -2, 3, -4, 5])
应返回false
。');"
}
],
"solutions": [],
@@ -1556,10 +1557,10 @@
"name": "index",
"contents": [
"function checkPositive(arr) {",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" ",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"checkPositive([1, 2, 3, -4, 5]);"
],
@@ -1572,28 +1573,28 @@
"id": "587d7dab367417b2b2512b6f",
"title": "Use the some Method to Check that Any Elements in an Array Meet a Criteria",
"description": [
- "The some
method works with arrays to check if any element passes a particular test. It returns a Boolean value - true
if any of the values meet the criteria, false
if not.",
- "For example, the following code would check if any element in the numbers
array is less than 10:",
- "var numbers = [10, 50, 8, 220, 110, 11];", + "
numbers.some(function(currentValue) {
return currentValue < 10;
});
// Returns true
some
方法用于检测数组中任何元素是否满足指定条件。如果有一个元素满足条件,返回布尔值true
,反之返回false
。",
+ "举个例子,下面的代码检测数组numbers
中是否有元素小于10:",
+ "var numbers = [10, 50, 8, 220, 110, 11];", "
numbers.some(function(currentValue) {
return currentValue < 10;
});
// 返回 true
some
method inside the checkPositive
function to check if any element in arr
is positive. The function should return a Boolean value."
+ "在checkPositive
函数值中使用some
检查arr
中是否有元素为正数,函数应返回一个布尔值。"
],
"tests": [
{
- "text": "Your code should use the some
method.",
- "testString": "assert(code.match(/\\.some/g), 'Your code should use the some
method.');"
+ "text": "应该使用some
method.",
+ "testString": "assert(code.match(/\\.some/g), '应该使用some
method.');"
},
{
- "text": "checkPositive([1, 2, 3, -4, 5])
should return true
.",
- "testString": "assert(checkPositive([1, 2, 3, -4, 5]), 'checkPositive([1, 2, 3, -4, 5])
should return true
.');"
+ "text": "checkPositive([1, 2, 3, -4, 5])
应返回true
。",
+ "testString": "assert(checkPositive([1, 2, 3, -4, 5]), 'checkPositive([1, 2, 3, -4, 5])
应返回true
。');"
},
{
- "text": "checkPositive([1, 2, 3, 4, 5])
should return true
.",
- "testString": "assert(checkPositive([1, 2, 3, 4, 5]), 'checkPositive([1, 2, 3, 4, 5])
should return true
.');"
+ "text": "checkPositive([1, 2, 3, 4, 5])
应返回true
。",
+ "testString": "assert(checkPositive([1, 2, 3, 4, 5]), 'checkPositive([1, 2, 3, 4, 5])
应返回true
。');"
},
{
- "text": "checkPositive([-1, -2, -3, -4, -5])
should return false
.",
- "testString": "assert(!checkPositive([-1, -2, -3, -4, -5]), 'checkPositive([-1, -2, -3, -4, -5])
should return false
.');"
+ "text": "checkPositive([-1, -2, -3, -4, -5])
应返回false
。",
+ "testString": "assert(!checkPositive([-1, -2, -3, -4, -5]), 'checkPositive([-1, -2, -3, -4, -5])
应返回false
。');"
}
],
"solutions": [],
@@ -1606,10 +1607,10 @@
"name": "index",
"contents": [
"function checkPositive(arr) {",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" ",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"checkPositive([1, 2, 3, -4, 5]);"
],
@@ -1622,34 +1623,34 @@
"id": "587d7dab367417b2b2512b70",
"title": "Introduction to Currying and Partial Application",
"description": [
- "The arity
of a function is the number of arguments it requires. Currying
a function means to convert a function of N arity
into N functions of arity
1.",
- "In other words, it restructures a function so it takes one argument, then returns another function that takes the next argument, and so on.",
- "Here's an example:",
- "//Un-curried function", - "This is useful in your program if you can't supply all the arguments to a function at one time. You can save each function call into a variable, which will hold the returned function reference that takes the next argument when it's available. Here's an example using the
function unCurried(x, y) {
return x + y;
}
//Curried function
function curried(x) {
return function(y) {
return x + y;
}
}
curried(1)(2) // Returns 3
curried
function in the example above:",
- "// Call a curried function in parts:", - "Similarly,
var funcForY = curried(1);
console.log(funcForY(2)); // Prints 3
partial application
can be described as applying a few arguments to a function at a time and returning another function that is applied to more arguments.",
- "Here's an example:",
- "//Impartial function", + "
function impartial(x, y, z) {
return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // Returns 13
arity
是函数所需的形参的数量。函数Currying
意思是把接受多个arity
的函数变换成接受一个单一arity
的函数。",
+ "换句话说,重构函数让它接收一个参数,然后返回另一个接收下一个参数的函数,依此类推。",
+ "举个例子:",
+ "//Un-curried function", + "如果不能一次为函数提供所有参数,那么在柯里化很有用。可以将每个函数的调用保存到一个变量中,该变量将保存返回的函数引用,该引用在下一个参数可用时接受该参数。下面是一个使用
function unCurried(x, y) {
return x + y;
}
// 柯里化函数
function curried(x) {
return function(y) {
return x + y;
}
}
curried(1)(2) // 返回 3
curried
函数的例子:",
+ "// Call a curried function in parts:", + "类似地,
var funcForY = curried(1);
console.log(funcForY(2)); // 打印 3
partial application
可以描述为一次对一个函数应用几个参数,然后返回另一个应用更多参数的函数。",
+ "举个例子:",
+ "//Impartial function", "
function impartial(x, y, z) {
return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // 返回 13
add
function so it uses currying to add parameters x
, y
, and z
."
+ "填写add
函数主体部分,通过柯里化方式来添加参数x
,y
和z
."
],
"tests": [
{
- "text": "add(10)(20)(30)
should return 60
.",
- "testString": "assert(add(10)(20)(30) === 60, 'add(10)(20)(30)
should return 60
.');"
+ "text": "add(10)(20)(30)
应返回60
。",
+ "testString": "assert(add(10)(20)(30) === 60, 'add(10)(20)(30)
应返回60
。');"
},
{
- "text": "add(1)(2)(3)
should return 6
.",
- "testString": "assert(add(1)(2)(3) === 6, 'add(1)(2)(3)
should return 6
.');"
+ "text": "add(1)(2)(3)
应返回6
。",
+ "testString": "assert(add(1)(2)(3) === 6, 'add(1)(2)(3)
应返回6
。');"
},
{
- "text": "add(11)(22)(33)
should return 66
.",
- "testString": "assert(add(11)(22)(33) === 66, 'add(11)(22)(33)
should return 66
.');"
+ "text": "add(11)(22)(33)
应返回66
。",
+ "testString": "assert(add(11)(22)(33) === 66, 'add(11)(22)(33)
应返回66
。');"
},
{
- "text": "Your code should include a final statement that returns x + y + z
.",
- "testString": "assert(code.match(/[xyz]\\s*?\\+\\s*?[xyz]\\s*?\\+\\s*?[xyz]/g), 'Your code should include a final statement that returns x + y + z
.');"
+ "text": "应包含一个最终返回x + y + z
的代码。",
+ "testString": "assert(code.match(/[xyz]\\s*?\\+\\s*?[xyz]\\s*?\\+\\s*?[xyz]/g), '应包含一个最终返回x + y + z
的代码。');"
}
],
"solutions": [],
@@ -1662,10 +1663,10 @@
"name": "index",
"contents": [
"function add(x) {",
- " // Add your code below this line",
+ " // 请在本行以下添加你的代码",
" ",
" ",
- " // Add your code above this line",
+ " // 请在本行以上添加你的代码",
"}",
"add(10)(20)(30);"
],