chore(i8n,curriculum): processed translations (#41490)
Co-authored-by: Crowdin Bot <support+bot@crowdin.com>
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7b8e367417b2b2512b5e
|
||||
title: Avoid Mutations and Side Effects Using Functional Programming
|
||||
title: 使用函数式编程避免变化和副作用
|
||||
challengeType: 1
|
||||
forumTopicId: 301228
|
||||
dashedName: avoid-mutations-and-side-effects-using-functional-programming
|
||||
@ -8,39 +8,39 @@ dashedName: 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.
|
||||
如果你还没想通,上一个挑战的问题出在 `tabClose()` 函数里的 `splice`。 不幸的是,`splice` 修改了调用它的原始数组,所以第二次调用它时是基于修改后的数组,才给出了意料之外的结果。
|
||||
|
||||
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 principles 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.
|
||||
前面的例子没有任何复杂的操作,但是 `splice` 方法改变了原始数组,导致 bug 产生。
|
||||
|
||||
Recall that in functional programming, changing or altering things is called <dfn>mutation</dfn>, and the outcome is called a <dfn>side effect</dfn>. A function, ideally, should be a <dfn>pure function</dfn>, meaning that it does not cause any side effects.
|
||||
回想一下,在函数式编程中,改变或变更叫做 <dfn>mutation</dfn>,这种改变的结果叫做“副作用”(<dfn>side effect</dfn>)。 理想情况下,函数应该是不会产生任何副作用的 <dfn>pure function</dfn>。
|
||||
|
||||
Let's try to master this discipline and not alter any variable or object in our code.
|
||||
让我们尝试掌握这个原则:不要改变代码中的任何变量或对象。
|
||||
|
||||
# --instructions--
|
||||
|
||||
Fill in the code for the function `incrementer` so it returns the value of the global variable `fixedValue` increased by one.
|
||||
填写 `incrementer` 函数的代码,使其返回值为全局变量 `fixedValue` 增加 1 。
|
||||
|
||||
# --hints--
|
||||
|
||||
Your function `incrementer` should not change the value of `fixedValue` (which is `4`).
|
||||
`incrementer` 函数不能改变 `fixedValue` 的值(`4`)。
|
||||
|
||||
```js
|
||||
incrementer();
|
||||
assert(fixedValue === 4);
|
||||
```
|
||||
|
||||
Your `incrementer` function should return a value that is one larger than the `fixedValue` value.
|
||||
`incrementer` 函数应返回比 `fixedValue` 变量更大的值。
|
||||
|
||||
```js
|
||||
const __newValue = incrementer();
|
||||
assert(__newValue === 5);
|
||||
```
|
||||
|
||||
Your `incrementer` function should return a value based on the global `fixedValue` variable value.
|
||||
你的 `incrementer` 函数返回的值应该基于全局变量 `fixedValue` 的值。
|
||||
|
||||
```js
|
||||
(function () {
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7b8f367417b2b2512b62
|
||||
title: Implement map on a Prototype
|
||||
title: 在原型上实现 map 方法
|
||||
challengeType: 1
|
||||
forumTopicId: 301230
|
||||
dashedName: implement-map-on-a-prototype
|
||||
@ -8,25 +8,25 @@ dashedName: 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.
|
||||
之前用到了 `Array.prototype.map()` 方法(即 `map()`),通过 `map` 返回一个与调用它的数组长度相同的数组。 只要它的回调函数不改变原始数组,它就不会改变原始数组。
|
||||
|
||||
In other words, `map` is a pure function, and its output depends solely on its inputs. Plus, it takes another function as its argument.
|
||||
换句话说,`map` 是一个纯函数,它的输出仅取决于输入的数组和作为参数传入的回调函数。 此外,它接收另一个函数作为它的参数。
|
||||
|
||||
You might learn a lot about the `map` method if you implement your own version of it. It is recommended you use a `for` loop or `Array.prototype.forEach()`.
|
||||
实现一个 `map`,加深对它的了解。 你可以用 `for` 循环或者 `Array.prototype.forEach()` 方法。
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write your own `Array.prototype.myMap()`, which should behave exactly like `Array.prototype.map()`. You should not use the built-in `map` method. The `Array` instance can be accessed in the `myMap` method using `this`.
|
||||
写一个和 `Array.prototype.map()` 一样的 `Array.prototype.myMap()`。 不能使用内置的 `map` 方法。 在 `myMap` 方法内,可以使用 `this` 访问 `Array` 实例。
|
||||
|
||||
# --hints--
|
||||
|
||||
`new_s` should equal `[46, 130, 196, 10]`.
|
||||
`new_s` 应该等于 `[46, 130, 196, 10]`。
|
||||
|
||||
```js
|
||||
assert(JSON.stringify(new_s) === JSON.stringify([46, 130, 196, 10]));
|
||||
```
|
||||
|
||||
Your code should not use the `map` method.
|
||||
不能使用 `map` 方法。
|
||||
|
||||
```js
|
||||
assert(!code.match(/\.?[\s\S]*?map/g));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7b8f367417b2b2512b64
|
||||
title: Implement the filter Method on a Prototype
|
||||
title: 在原型上实现 filter 方法
|
||||
challengeType: 1
|
||||
forumTopicId: 301231
|
||||
dashedName: implement-the-filter-method-on-a-prototype
|
||||
@ -8,21 +8,21 @@ dashedName: implement-the-filter-method-on-a-prototype
|
||||
|
||||
# --description--
|
||||
|
||||
You might learn a lot about the `filter` method if you implement your own version of it. It is recommended you use a `for` loop or `Array.prototype.forEach()`.
|
||||
为了加深对 `filter` 的理解,可以自己实现一个。 可以用 `for` 循环或 `Array.prototype.forEach()`。
|
||||
|
||||
# --instructions--
|
||||
|
||||
Write your own `Array.prototype.myFilter()`, which should behave exactly like `Array.prototype.filter()`. You should not use the built-in `filter` method. The `Array` instance can be accessed in the `myFilter` method using `this`.
|
||||
编写一个和 `Array.prototype.filter()` 功能一样的 `Array.prototype.myFilter()` 方法。 不能使用内置的 `filter` 方法。 在 `myFilter` 方法内部,可以使用 `this` 访问 `Array` 实例。
|
||||
|
||||
# --hints--
|
||||
|
||||
`new_s` should equal `[23, 65, 5]`.
|
||||
`new_s` 应该等于 `[23, 65, 5]`。
|
||||
|
||||
```js
|
||||
assert(JSON.stringify(new_s) === JSON.stringify([23, 65, 5]));
|
||||
```
|
||||
|
||||
Your code should not use the `filter` method.
|
||||
不应该使用 `filter` 方法。
|
||||
|
||||
```js
|
||||
assert(!code.match(/\.?[\s\S]*?filter/g));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7dab367417b2b2512b70
|
||||
title: Introduction to Currying and Partial Application
|
||||
title: 函数柯里化
|
||||
challengeType: 1
|
||||
forumTopicId: 301232
|
||||
dashedName: introduction-to-currying-and-partial-application
|
||||
@ -8,74 +8,72 @@ dashedName: introduction-to-currying-and-partial-application
|
||||
|
||||
# --description--
|
||||
|
||||
The <dfn>arity</dfn> of a function is the number of arguments it requires. <dfn>Currying</dfn> a function means to convert a function of N arity into N functions of arity 1.
|
||||
<dfn>arity</dfn> 是函数所需的形参的数量。 函数柯里化(<dfn>Currying</dfn>)意思是把接受多个 arity 的函数变换成接受单一arity 的函数。
|
||||
|
||||
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:
|
||||
举个例子:
|
||||
|
||||
```js
|
||||
//Un-curried function
|
||||
function unCurried(x, y) {
|
||||
return x + y;
|
||||
}
|
||||
|
||||
//Curried function
|
||||
function curried(x) {
|
||||
return function(y) {
|
||||
return x + y;
|
||||
}
|
||||
}
|
||||
//Alternative using ES6
|
||||
|
||||
const curried = x => y => x + y
|
||||
|
||||
curried(1)(2) // Returns 3
|
||||
curried(1)(2)
|
||||
```
|
||||
|
||||
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 curried function in the example above:
|
||||
`curried(1)(2)` 会返回 `3`。
|
||||
|
||||
柯里化在不能一次为函数提供所有参数情况下很有用。 因为它可以将每个函数的调用保存到一个变量中,该变量将保存返回的函数引用,该引用在下一个参数可用时接受该参数。 下面是使用柯里化函数的例子:
|
||||
|
||||
```js
|
||||
// Call a curried function in parts:
|
||||
var funcForY = curried(1);
|
||||
console.log(funcForY(2)); // Prints 3
|
||||
console.log(funcForY(2)); // 3
|
||||
```
|
||||
|
||||
Similarly, <dfn>partial application</dfn> 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:
|
||||
类似地,局部调用( <dfn>partial application</dfn>)的意思是一次对一个函数应用几个参数,然后返回另一个应用更多参数的函数。 这是一个示例:
|
||||
|
||||
```js
|
||||
//Impartial function
|
||||
function impartial(x, y, z) {
|
||||
return x + y + z;
|
||||
}
|
||||
var partialFn = impartial.bind(this, 1, 2);
|
||||
partialFn(10); // Returns 13
|
||||
partialFn(10); // 13
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
Fill in the body of the `add` function so it uses currying to add parameters `x`, `y`, and `z`.
|
||||
填写 `add` 函数主体部分,用柯里化添加参数 `x`,`y` 和 `z`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`add(10)(20)(30)` should return `60`.
|
||||
`add(10)(20)(30)` 应返回 `60`。
|
||||
|
||||
```js
|
||||
assert(add(10)(20)(30) === 60);
|
||||
```
|
||||
|
||||
`add(1)(2)(3)` should return `6`.
|
||||
`add(1)(2)(3)` 应返回 `6`。
|
||||
|
||||
```js
|
||||
assert(add(1)(2)(3) === 6);
|
||||
```
|
||||
|
||||
`add(11)(22)(33)` should return `66`.
|
||||
`add(11)(22)(33)` 应返回 `66`。
|
||||
|
||||
```js
|
||||
assert(add(11)(22)(33) === 66);
|
||||
```
|
||||
|
||||
Your code should include a final statement that returns `x + y + z`.
|
||||
应返回 `x + y + z` 的最终结果。
|
||||
|
||||
```js
|
||||
assert(code.match(/[xyz]\s*?\+\s*?[xyz]\s*?\+\s*?[xyz]/g));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7b8d367417b2b2512b5b
|
||||
title: Learn About Functional Programming
|
||||
title: 学习函数式编程
|
||||
challengeType: 1
|
||||
forumTopicId: 301233
|
||||
dashedName: learn-about-functional-programming
|
||||
@ -8,33 +8,33 @@ dashedName: 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
|
||||
1)功能独立——不依赖于程序的状态(比如可能发生变化的全局变量);
|
||||
|
||||
2) Pure functions - the same input always gives the same output
|
||||
2)纯函数——同一个输入永远能得到同一个输出;
|
||||
|
||||
3) Functions with limited side effects - any changes, or mutations, to the state of the program outside the function are carefully controlled
|
||||
3)有限的副作用——可以严格地限制函数外部对状态的更改。
|
||||
|
||||
# --instructions--
|
||||
|
||||
The members of freeCodeCamp happen to love tea.
|
||||
freeCodeCamp 成员在 love tea 的故事。
|
||||
|
||||
In the code editor, the `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.
|
||||
在代码编辑器中,已经为你定义好了 `prepareTea` 和 `getTea` 函数。 调用 `getTea` 函数为团队准备 40 杯茶,并将它们存储在 `tea4TeamFCC` 变量里。
|
||||
|
||||
# --hints--
|
||||
|
||||
The `tea4TeamFCC` variable should hold 40 cups of tea for the team.
|
||||
`tea4TeamFCC` 变量里应有 40 杯为团队准备的茶。
|
||||
|
||||
```js
|
||||
assert(tea4TeamFCC.length === 40);
|
||||
```
|
||||
|
||||
The `tea4TeamFCC` variable should hold cups of green tea.
|
||||
`tea4TeamFCC` 变量里应有 greenTea。
|
||||
|
||||
```js
|
||||
assert(tea4TeamFCC[0] === 'greenTea');
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7b8f367417b2b2512b60
|
||||
title: Refactor Global Variables Out of Functions
|
||||
title: 在函数中重构全局变量
|
||||
challengeType: 1
|
||||
forumTopicId: 301235
|
||||
dashedName: refactor-global-variables-out-of-functions
|
||||
@ -8,23 +8,23 @@ dashedName: 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. Hint: using something like `var newArr = arrVar`, where `arrVar` is an array will simply create a reference to the existing variable and not a copy. So changing a value in `newArr` would change the value in `arrVar`.
|
||||
1) 不要更改变量或对象 - 创建新变量和对象,并在需要时从函数返回它们。 提示:使用类似 `var newArr = arrVar` 时 `arrVar` 是一个数组,代码只是创建一个对现有变量的引用,而不是副本。 所以更改 `newArr` 中的值会同时更改 `arrVar` 中的值。
|
||||
|
||||
2) Declare function parameters - any computation inside a function depends only on the arguments passed to the function, and not on any global object or variable.
|
||||
2) 声明函数参数 - 函数内的任何计算仅取决于参数,而不取决于任何全局对象或变量。
|
||||
|
||||
Adding one to a number is not very exciting, but we can apply these principles when working with arrays or more complex objects.
|
||||
给数字增加 1 不够刺激,我们可以在处理数组或更复杂的对象时应用这些原则。
|
||||
|
||||
# --instructions--
|
||||
|
||||
Rewrite the code so the global array `bookList` is not changed inside either function. The `add` function should add the given `bookName` to the end of the array passed to it and return a new array (list). The `remove` function should remove the given `bookName` from the array passed to it.
|
||||
重构代码,使全局数组 `bookList` 在函数内部不会被改变。 `add` 函数可以将指定的 `bookName` 增加到数组末尾并返回一个新的数组(列表)。 `remove` 函数可以从数组中移除指定 `bookName`。
|
||||
|
||||
**Note:** Both functions should return an array, and any new parameters should be added before the `bookName` parameter.
|
||||
**注意:** 两个函数都应该返回一个数组,任何新参数都应该在 `bookName` 参数之前添加。
|
||||
|
||||
# --hints--
|
||||
|
||||
`bookList` should not change and still equal `["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"]`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -38,7 +38,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`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"]`.
|
||||
`newBookList` 应等于 `["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -53,7 +53,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`newerBookList` should equal `["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]`.
|
||||
`newerBookList` 应等于 `["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -66,7 +66,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`newestBookList` should equal `["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"]`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 9d7123c8c441eeafaeb5bdef
|
||||
title: Remove Elements from an Array Using slice Instead of splice
|
||||
title: 使用 slice 而不是 splice 从数组中移除元素
|
||||
challengeType: 1
|
||||
forumTopicId: 301236
|
||||
dashedName: remove-elements-from-an-array-using-slice-instead-of-splice
|
||||
@ -8,37 +8,38 @@ dashedName: 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:
|
||||
使用数组时经常遇到要删除一些元素并保留数组剩余部分的情况。 为此,JavaScript 提供了 `splice` 方法,它接收两个参数:从哪里开始删除项目的索引,和要删除的项目数。 如果没有提供第二个参数,默认情况下是移除一直到结尾的所有元素。 但 `splice` 方法会改变调用它的原始数组。 举个例子:
|
||||
|
||||
```js
|
||||
var cities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
|
||||
cities.splice(3, 1); // Returns "London" and deletes it from the cities array
|
||||
// cities is now ["Chicago", "Delhi", "Islamabad", "Berlin"]
|
||||
cities.splice(3, 1);
|
||||
```
|
||||
|
||||
As we saw in the last challenge, the `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.
|
||||
在这里 `splice` 返回字符串 `London` 并从城市数组中删除它。 `cities` 将有值 `["Chicago", "Delhi", "Islamabad", "Berlin"]`。
|
||||
|
||||
正如我们在上一次挑战中看到的那样,`slice` 方法不会改变原始数组,而是返回一个可以保存到变量中的新数组。 回想一下,`slice` 方法接收两个参数,从开始索引开始选取到结束(不包括该元素),并在新数组中返回这些元素。 使用 `slice` 方法替代 `splice` 有助于避免数组变化产生的副作用。
|
||||
|
||||
# --instructions--
|
||||
|
||||
Rewrite the function `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.
|
||||
用 `slice` 代替 `splice` 重写 `nonMutatingSplice` 函数。 将 `cities` 数组长度限制为 3,并返回一个仅包含前 3 项的新数组。
|
||||
|
||||
Do not mutate the original array provided to the function.
|
||||
不要改变提供给函数的原始数组。
|
||||
|
||||
# --hints--
|
||||
|
||||
Your code should use the `slice` method.
|
||||
你的代码中应使用 `slice` 方法。
|
||||
|
||||
```js
|
||||
assert(code.match(/\.slice/g));
|
||||
```
|
||||
|
||||
Your code should not use the `splice` method.
|
||||
不能使用 `splice` 方法。
|
||||
|
||||
```js
|
||||
assert(!code.match(/\.?[\s\S]*?splice/g));
|
||||
```
|
||||
|
||||
The `inputCities` array should not change.
|
||||
不能改变 `inputCities` 数组。
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -47,7 +48,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`nonMutatingSplice(["Chicago", "Delhi", "Islamabad", "London", "Berlin"])` should return `["Chicago", "Delhi", "Islamabad"]`.
|
||||
`nonMutatingSplice(["Chicago", "Delhi", "Islamabad", "London", "Berlin"])` 应返回 `["Chicago", "Delhi", "Islamabad"]`。
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7b90367417b2b2512b65
|
||||
title: Return Part of an Array Using the slice Method
|
||||
title: 使用 slice 方法返回数组的一部分
|
||||
challengeType: 1
|
||||
forumTopicId: 301239
|
||||
dashedName: return-part-of-an-array-using-the-slice-method
|
||||
@ -8,29 +8,30 @@ dashedName: 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.
|
||||
`slice` 方法可以从已有数组中返回指定元素。 它接受两个参数,第一个规定从何处开始选取,第二个规定从何处结束选取(不包括该元素)。 如果没有传参,则默认为从数组的开头开始到结尾结束,这是复制整个数组的简单方式。 `slice` 返回一个新数组,不会修改原始数组。
|
||||
|
||||
Here's an example:
|
||||
举个例子:
|
||||
|
||||
```js
|
||||
var arr = ["Cat", "Dog", "Tiger", "Zebra"];
|
||||
var newArray = arr.slice(1, 3);
|
||||
// Sets newArray to ["Dog", "Tiger"]
|
||||
```
|
||||
|
||||
`newArray` 值为 `["Dog", "Tiger"]`
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use the `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` 数组的一部分。 这个函数应返回一个数组。
|
||||
|
||||
# --hints--
|
||||
|
||||
Your code should use the `slice` method.
|
||||
应该使用 `slice` 方法。
|
||||
|
||||
```js
|
||||
assert(code.match(/\.slice/g));
|
||||
```
|
||||
|
||||
The `inputAnim` variable should not change.
|
||||
不能改变 `inputAnim` 变量。
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -39,7 +40,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`sliceArray(["Cat", "Dog", "Tiger", "Zebra", "Ant"], 1, 3)` should return `["Dog", "Tiger"]`.
|
||||
`sliceArray(["Cat", "Dog", "Tiger", "Zebra", "Ant"], 1, 3)` 应返回 `["Dog", "Tiger"]`。
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -48,7 +49,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`sliceArray(["Cat", "Dog", "Tiger", "Zebra", "Ant"], 0, 1)` should return `["Cat"]`.
|
||||
`sliceArray(["Cat", "Dog", "Tiger", "Zebra", "Ant"], 0, 1)` 应返回 `["Cat"]`。
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -57,7 +58,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`sliceArray(["Cat", "Dog", "Tiger", "Zebra", "Ant"], 1, 4)` should return `["Dog", "Tiger", "Zebra"]`.
|
||||
`sliceArray(["Cat", "Dog", "Tiger", "Zebra", "Ant"], 1, 4)` 应返回 `["Dog", "Tiger", "Zebra"]`。
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
Reference in New Issue
Block a user