chore(i8n,curriculum): processed translations (#41490)

Co-authored-by: Crowdin Bot <support+bot@crowdin.com>
This commit is contained in:
camperbot
2021-03-14 21:20:39 -06:00
committed by GitHub
parent 5e563329ad
commit 903a301849
262 changed files with 2871 additions and 2708 deletions

View File

@ -1,6 +1,6 @@
---
id: 587d78b3367417b2b2512b11
title: Add Items Using splice()
title: 使用 splice() 添加元素
challengeType: 1
forumTopicId: 301152
dashedName: add-items-using-splice
@ -8,7 +8,7 @@ dashedName: add-items-using-splice
# --description--
Remember in the last challenge we mentioned that `splice()` can take up to three parameters? Well, you can use the third parameter, comprised of one or more element(s), to add to the array. This can be incredibly useful for quickly switching out an element, or a set of elements, for another.
还记得在上个挑战中我们提到 `splice()` 方法最多可以接收 3 个参数吗? 第三个参数可以是一个或多个元素,这些元素会被添加到数组中。 这样,我们能够便捷地将数组中的一个或多个连续元素换成其他的元素。
```js
const numbers = [10, 11, 12, 12, 15];
@ -16,20 +16,20 @@ const startIndex = 3;
const amountToDelete = 1;
numbers.splice(startIndex, amountToDelete, 13, 14);
// the second entry of 12 is removed, and we add 13 and 14 at the same index
console.log(numbers);
// returns [ 10, 11, 12, 13, 14, 15 ]
```
Here, we begin with an array of numbers. Then, we pass the following to `splice()`: The index at which to begin deleting elements (3), the number of elements to be deleted (1), and the remaining arguments (13, 14) will be inserted starting at that same index. Note that there can be any number of elements (separated by commas) following `amountToDelete`, each of which gets inserted.
`12` 的第二个条目已被删除,我们在同一索引处添加 `13``14``numbers` 数组现在将会是 `[ 10, 11, 12, 13, 14, 15 ]`
在上面的代码中,数组一开始包含了若干数字。 接着,我们调用 `splice()` 方法,索引为 (3) 的地方开始删除元素,删除的元素数量是 (1)。然后,(13, 14) 是在删除位置插入的元素。 可以在 `amountToDelete` 后面传入任意数量的元素(以逗号分隔),每个都会被插入到数组中。
# --instructions--
We have defined a function, `htmlColorNames`, which takes an array of HTML colors as an argument. Modify the function using `splice()` to remove the first two elements of the array and add `'DarkSalmon'` and `'BlanchedAlmond'` in their respective places.
我们已经定义了一个 `htmlColorNames` 函数,它以一个 HTML 颜色的数组作为输入参数。 请修改这个函数,使用 `splice()` 来移除数组中的前两个元素,并在对应的位置上添加 `'DarkSalmon'` `'BlanchedAlmond'`
# --hints--
`htmlColorNames` should return `["DarkSalmon", "BlanchedAlmond", "LavenderBlush", "PaleTurquoise", "FireBrick"]`
`htmlColorNames` 应返回 `["DarkSalmon", "BlanchedAlmond", "LavenderBlush", "PaleTurquoise", "FireBrick"]`
```js
assert.deepEqual(
@ -50,19 +50,19 @@ assert.deepEqual(
);
```
The `htmlColorNames` function should utilize the `splice()` method
`htmlColorNames` 函数中应调用 `splice()` 方法。
```js
assert(/.splice/.test(code));
```
You should not use `shift()` or `unshift()`.
不应使用 `shift()` `unshift()`
```js
assert(!/shift|unshift/.test(code));
```
You should not use array bracket notation.
不应使用数组的方括号表示法。
```js
assert(!/\[\d\]\s*=/.test(code));

View File

@ -1,6 +1,6 @@
---
id: 587d7b7c367417b2b2512b18
title: Add Key-Value Pairs to JavaScript Objects
title: 将键值对添加到对象中
challengeType: 1
forumTopicId: 301153
dashedName: add-key-value-pairs-to-javascript-objects
@ -8,7 +8,7 @@ dashedName: add-key-value-pairs-to-javascript-objects
# --description--
At their most basic, objects are just collections of <dfn>key-value</dfn> pairs. In other words, they are pieces of data (<dfn>values</dfn>) mapped to unique identifiers called <dfn>properties</dfn> (<dfn>keys</dfn>). Take a look at an example:
对象object本质上是键值对<dfn>key-value pair</dfn>)的集合。 或者说,一系列被映射到唯一标识符的数据就是对象;习惯上,唯一标识符叫做属性(<dfn>property</dfn>)或者键(<dfn>key</dfn>);数据叫做值(<dfn>value</dfn>)。 让我们来看一个简单的例子:
```js
const tekkenCharacter = {
@ -18,19 +18,19 @@ const tekkenCharacter = {
};
```
The above code defines a Tekken video game character object called `tekkenCharacter`. It has three properties, each of which map to a specific value. If you want to add an additional property, such as "origin", it can be done by assigning `origin` to the object:
上面的代码定义了一个叫做 `tekkenCharacter` 的“铁拳”游戏人物对象。 它有三个属性,每个属性都对应一个特定的值。 如果我们想为它再添加一个叫做 `origin` 的属性,可以这样写:
```js
tekkenCharacter.origin = 'South Korea';
```
This uses dot notation. If you were to observe the `tekkenCharacter` object, it will now include the `origin` property. Hwoarang also had distinct orange hair. You can add this property with bracket notation by doing:
上面的代码中,我们使用了点号表示法。 如果我们现在输出 `tekkenCharacter` 对象,便可以看到它具有 `origin` 属性。 接下来,因为这个人物在游戏中有着与众不同的橘色头发, 我们可以通过方括号表示法来为它添加这个属性,像这样:
```js
tekkenCharacter['hair color'] = 'dyed orange';
```
Bracket notation is required if your property has a space in it or if you want to use a variable to name the property. In the above case, the property is enclosed in quotes to denote it as a string and will be added exactly as shown. Without quotes, it will be evaluated as a variable and the name of the property will be whatever value the variable is. Here's an example with a variable:
如果要设置的属性中存在空格或者要设置的属性是一个变量那我们必须使用方括号表示法bracket notation来为对象添加属性。 在上面的代码中我们把属性hair color放到引号里以此来表示整个字符串都是需要设置的属性。 如果我们不加上引号,那么中括号里的内容会被当作一个变量来解析,这个变量对应的值就会作为要设置的属性, 请看这段代码:
```js
const eyes = 'eye color';
@ -38,7 +38,7 @@ const eyes = 'eye color';
tekkenCharacter[eyes] = 'brown';
```
After adding all the examples, the object will look like this:
执行以上所有示例代码后,对象会变成这样:
```js
{
@ -53,35 +53,35 @@ After adding all the examples, the object will look like this:
# --instructions--
A `foods` object has been created with three entries. Using the syntax of your choice, add three more entries to it: `bananas` with a value of `13`, `grapes` with a value of `35`, and `strawberries` with a value of `27`.
我们已经为你创建了包含三个项目的 `foods` 对象。 请使用上述任意语法,来为 foods 对象添加如下三个键值对:`bananas` 属性,值为 `13``grapes` 属性,值为 `35``strawberries` 属性,值为 `27`
# --hints--
`foods` should be an object.
`foods` 应为一个对象。
```js
assert(typeof foods === 'object');
```
The `foods` object should have a key `"bananas"` with a value of `13`.
`foods` 应有一个值为 `13` `bananas` 属性。
```js
assert(foods.bananas === 13);
```
The `foods` object should have a key `"grapes"` with a value of `35`.
`foods` 应有一个值为 `35` `grapes` 属性。
```js
assert(foods.grapes === 35);
```
The `foods` object should have a key `"strawberries"` with a value of `27`.
`foods` 应有一个值为 `27` `strawberries` 属性。
```js
assert(foods.strawberries === 27);
```
The key-value pairs should be set using dot or bracket notation.
应使用点号表示法或方括号表示法来设置对象的属性。
```js
assert(

View File

@ -1,6 +1,6 @@
---
id: 587d7b7b367417b2b2512b14
title: Check For The Presence of an Element With indexOf()
title: 使用 indexOf() 检查元素是否存在
challengeType: 1
forumTopicId: 301154
dashedName: check-for-the-presence-of-an-element-with-indexof
@ -8,31 +8,33 @@ dashedName: check-for-the-presence-of-an-element-with-indexof
# --description--
Since arrays can be changed, or *mutated*, at any time, there's no guarantee about where a particular piece of data will be on a given array, or if that element even still exists. Luckily, JavaScript provides us with another built-in method, `indexOf()`, that allows us to quickly and easily check for the presence of an element on an array. `indexOf()` takes an element as a parameter, and when called, it returns the position, or index, of that element, or `-1` if the element does not exist on the array.
由于数组随时都可以修改或发生 *mutated*,我们很难保证某个数据始终处于数组中的特定位置,甚至不能保证该元素是否还存在于该数组中。 好消息是JavaScript 为我们提供了内置方法 `indexOf()`。 这个方法让我们可以方便地检查某个元素是否存在于数组中。 `indexOf()` 方法接受一个元素作为输入参数,并返回该元素在数组中的位置(索引);若该元素不存在于数组中则返回 `-1`
For example:
例如:
```js
let fruits = ['apples', 'pears', 'oranges', 'peaches', 'pears'];
fruits.indexOf('dates'); // returns -1
fruits.indexOf('oranges'); // returns 2
fruits.indexOf('pears'); // returns 1, the first index at which the element exists
fruits.indexOf('dates');
fruits.indexOf('oranges');
fruits.indexOf('pears');
```
`indexOf('dates')` 返回 `-1``indexOf('oranges')` 返回 `2``indexOf('pears')` 返回 `1` (每个元素存在的第一个索引)。
# --instructions--
`indexOf()` can be incredibly useful for quickly checking for the presence of an element on an array. We have defined a function, `quickCheck`, that takes an array and an element as arguments. Modify the function using `indexOf()` so that it returns `true` if the passed element exists on the array, and `false` if it does not.
`indexOf()` 在快速检查一个数组中是否存在某个元素时非常有用。 我们已经定义了一个 `quickCheck` 函数,它接受一个数组和一个元素作为输入参数。 请通过 `indexOf()` 方法修改这个函数,使得当传入的参数在数组中存在时返回 `true`,否则返回 `false`
# --hints--
The `quickCheck` function should return a boolean (`true` or `false`), not a string (`"true"` or `"false"`)
`quickCheck` 函数应返回一个布尔值(`true` `false`),而不是一个字符串(`"true"` `"false"`)。
```js
assert.isBoolean(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
```
`quickCheck(["squash", "onions", "shallots"], "mushrooms")` should return `false`
`quickCheck(["squash", "onions", "shallots"], "mushrooms")` 应返回 `false`
```js
assert.strictEqual(
@ -41,7 +43,7 @@ assert.strictEqual(
);
```
`quickCheck(["onions", "squash", "shallots"], "onions")` should return `true`
`quickCheck(["onions", "squash", "shallots"], "onions")` 应返回 `true`
```js
assert.strictEqual(
@ -50,19 +52,19 @@ assert.strictEqual(
);
```
`quickCheck([3, 5, 9, 125, 45, 2], 125)` should return `true`
`quickCheck([3, 5, 9, 125, 45, 2], 125)` 应返回 `true`
```js
assert.strictEqual(quickCheck([3, 5, 9, 125, 45, 2], 125), true);
```
`quickCheck([true, false, false], undefined)` should return `false`
`quickCheck([true, false, false], undefined)` 应返回 `false`
```js
assert.strictEqual(quickCheck([true, false, false], undefined), false);
```
The `quickCheck` function should utilize the `indexOf()` method
`quickCheck` 函数中应使用 `indexOf()` 方法。
```js
assert.notStrictEqual(quickCheck.toString().search(/\.indexOf\(/), -1);

View File

@ -1,6 +1,6 @@
---
id: 587d7b7d367417b2b2512b1c
title: Check if an Object has a Property
title: 检查对象是否具有某个属性
challengeType: 1
forumTopicId: 301155
dashedName: check-if-an-object-has-a-property
@ -8,21 +8,22 @@ dashedName: check-if-an-object-has-a-property
# --description--
Now we can add, modify, and remove keys from objects. But what if we just wanted to know if an object has a specific property? JavaScript provides us with two different ways to do this. One uses the `hasOwnProperty()` method and the other uses the `in` keyword. If we have an object `users` with a property of `Alan`, we could check for its presence in either of the following ways:
我们已经学习了如何添加、修改和移除对象中的属性。 但如果我们想知道一个对象中是否包含某个属性呢? JavaScript 为我们提供了两种不同的方式来实现这个功能: 一个是通过 `hasOwnProperty()` 方法,另一个是使用 `in` 关键字。 假如我们有一个 `users` 对象,为检查它是否含有 `Alan` 属性,可以这样写:
```js
users.hasOwnProperty('Alan');
'Alan' in users;
// both return true
```
这两者结果都应该为 `true`
# --instructions--
We've created an object, `users`, with some users in it and a function `isEveryoneHere`, which we pass the `users` object to as an argument. Finish writing this function so that it returns `true` only if the `users` object contains all four names, `Alan`, `Jeff`, `Sarah`, and `Ryan`, as keys, and `false` otherwise.
我们已经定义了一个包含若干用户信息的 `users` 对象和一个 `isEveryoneHere` 函数,该函数接收 `users` 对象作为参数。 请完成该函数使其在 `users` 对象中同时包含 `Alan``Jeff``Sarah``Ryan` 四个属性时才返回 `true`,否则返回 `false`
# --hints--
The `users` object should only contain the keys `Alan`, `Jeff`, `Sarah`, and `Ryan`
`users` 对象应该只包含 `Alan``Jeff``Sarah``Ryan` 4 个属性。
```js
assert(
@ -34,13 +35,13 @@ assert(
);
```
The function `isEveryoneHere` should return `true` if `Alan`, `Jeff`, `Sarah`, and `Ryan` are properties on the `users` object
`isEveryoneHere` 函数在 `users` 对象包含 `Alan``Jeff``Sarah``Ryan` 4 个属性时应返回 `true`
```js
assert(isEveryoneHere(users) === true);
```
The function `isEveryoneHere` should return `false` if `Alan` is not a property on the `users` object
`isEveryoneHere` 函数在 `users` 对象不包含 `Alan` 时应返回 `false`
```js
assert(
@ -51,7 +52,7 @@ assert(
);
```
The function `isEveryoneHere` should return `false` if `Jeff` is not a property on the `users` object
`isEveryoneHere` 函数在 `users` 对象不包含 `Jeff` 时应返回 `false`
```js
assert(
@ -62,7 +63,7 @@ assert(
);
```
The function `isEveryoneHere` should return `false` if `Sarah` is not a property on the `users` object
`isEveryoneHere` 函数在 `users` 对象不包含 `Sarah` 时应返回 `false`
```js
assert(
@ -73,7 +74,7 @@ assert(
);
```
The function `isEveryoneHere` should return `false` if `Ryan` is not a property on the `users` object
`isEveryoneHere` 函数在 `users` 对象不包含 `Ryan` 时应返回 `false`
```js
assert(

View File

@ -1,6 +1,6 @@
---
id: 587d7b7b367417b2b2512b17
title: Combine Arrays with the Spread Operator
title: 使用展开运算符合并数组
challengeType: 1
forumTopicId: 301156
dashedName: combine-arrays-with-the-spread-operator
@ -8,30 +8,31 @@ dashedName: combine-arrays-with-the-spread-operator
# --description--
Another huge advantage of the <dfn>spread</dfn> operator, is the ability to combine arrays, or to insert all the elements of one array into another, at any index. With more traditional syntaxes, we can concatenate arrays, but this only allows us to combine arrays at the end of one, and at the start of another. Spread syntax makes the following operation extremely simple:
展开语法(<dfn>spread</dfn>)的另一个重要用途是合并数组,或者将某个数组的所有元素插入到另一个数组的任意位置。 我们也可以使用 ES5 的语法连接两个数组,但只能让它们首尾相接。 而展开语法可以让这样的操作变得极其简单:
```js
let thisArray = ['sage', 'rosemary', 'parsley', 'thyme'];
let thatArray = ['basil', 'cilantro', ...thisArray, 'coriander'];
// thatArray now equals ['basil', 'cilantro', 'sage', 'rosemary', 'parsley', 'thyme', 'coriander']
```
Using spread syntax, we have just achieved an operation that would have been more complex and more verbose had we used traditional methods.
`thatArray` 会有值 `['basil', 'cilantro', 'sage', 'rosemary', 'parsley', 'thyme', 'coriander']`
使用展开语法,我们就可以很方便的实现一个用传统方法会写得很复杂且冗长的操作。
# --instructions--
We have defined a function `spreadOut` that returns the variable `sentence`. Modify the function using the <dfn>spread</dfn> operator so that it returns the array `['learning', 'to', 'code', 'is', 'fun']`.
我们已经定义了一个返回 `sentence` 变量的 `spreadOut` 函数。 请修改这个函数,利用 <dfn>spread</dfn> 使该函数返回数组 `['learning', 'to', 'code', 'is', 'fun']`
# --hints--
`spreadOut` should return `["learning", "to", "code", "is", "fun"]`
`spreadOut` 应返回 `["learning", "to", "code", "is", "fun"]`
```js
assert.deepEqual(spreadOut(), ['learning', 'to', 'code', 'is', 'fun']);
```
The `spreadOut` function should utilize spread syntax
`spreadOut` 函数里应用到展开语法。
```js
assert.notStrictEqual(spreadOut.toString().search(/[...]/), -1);

View File

@ -1,6 +1,6 @@
---
id: 587d7b7b367417b2b2512b13
title: Copy an Array with the Spread Operator
title: 使用展开运算符复制数组
challengeType: 1
forumTopicId: 301157
dashedName: copy-an-array-with-the-spread-operator
@ -8,24 +8,24 @@ dashedName: copy-an-array-with-the-spread-operator
# --description--
While `slice()` allows us to be selective about what elements of an array to copy, among several other useful tasks, ES6's new <dfn>spread operator</dfn> allows us to easily copy *all* of an array's elements, in order, with a simple and highly readable syntax. The spread syntax simply looks like this: `...`
`slice()` 可以让我们从一个数组中选择一些元素来复制到新数组中,而 ES6 中又引入了一个简洁且可读性强的语法:展开运算符(<dfn>spread operator</dfn>),它能让我们方便地复制数组中的*所有*元素。 展开语法写出来是这样:`...`
In practice, we can use the spread operator to copy an array like so:
我们可以用展开运算符来复制数组:
```js
let thisArray = [true, true, undefined, false, null];
let thatArray = [...thisArray];
// thatArray equals [true, true, undefined, false, null]
// thisArray remains unchanged and thatArray contains the same elements as thisArray
```
`thatArray` 等于 `[true, true, undefined, false, null]``thisArray` 保持不变, `thatArray` 包含与 `thisArray` 相同的元素。
# --instructions--
We have defined a function, `copyMachine` which takes `arr` (an array) and `num` (a number) as arguments. The function is supposed to return a new array made up of `num` copies of `arr`. We have done most of the work for you, but it doesn't work quite right yet. Modify the function using spread syntax so that it works correctly (hint: another method we have already covered might come in handy here!).
我们已经定义了一个 `copyMachine` 函数,它接受 `arr`(一个数组)和 `num`(一个数字)作为输入参数。 该函数需要返回一个由 `num``arr` 组成的新的二维数组。 同时,我们写好了大致的流程,只是细节实现还没有写完。 请修改这个函数,使用展开语法,使该函数能正常工作(提示:我们已经学到过的一个方法很适合用在这里)!
# --hints--
`copyMachine([true, false, true], 2)` should return `[[true, false, true], [true, false, true]]`
`copyMachine([true, false, true], 2)` 应返回 `[[true, false, true], [true, false, true]]`
```js
assert.deepEqual(copyMachine([true, false, true], 2), [
@ -34,7 +34,7 @@ assert.deepEqual(copyMachine([true, false, true], 2), [
]);
```
`copyMachine([1, 2, 3], 5)` should return `[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]`
`copyMachine([1, 2, 3], 5)` 应返回 `[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]`
```js
assert.deepEqual(copyMachine([1, 2, 3], 5), [
@ -46,13 +46,13 @@ assert.deepEqual(copyMachine([1, 2, 3], 5), [
]);
```
`copyMachine([true, true, null], 1)` should return `[[true, true, null]]`
`copyMachine([true, true, null], 1)` 应返回 `[[true, true, null]]`
```js
assert.deepEqual(copyMachine([true, true, null], 1), [[true, true, null]]);
```
`copyMachine(["it works"], 3)` should return `[["it works"], ["it works"], ["it works"]]`
`copyMachine(["it works"], 3)` 应返回 `[["it works"], ["it works"], ["it works"]]`
```js
assert.deepEqual(copyMachine(['it works'], 3), [
@ -62,7 +62,7 @@ assert.deepEqual(copyMachine(['it works'], 3), [
]);
```
The `copyMachine` function should utilize the `spread operator` with array `arr`
`copyMachine` 函数中应对 `arr` 使用展开运算符(`spread operator`)。
```js
assert(__helpers.removeJSComments(code).match(/\.\.\.arr/));

View File

@ -1,6 +1,6 @@
---
id: 587d7b7a367417b2b2512b12
title: Copy Array Items Using slice()
title: 使用 slice() 复制数组元素
challengeType: 1
forumTopicId: 301158
dashedName: copy-array-items-using-slice
@ -8,25 +8,25 @@ dashedName: copy-array-items-using-slice
# --description--
The next method we will cover is `slice()`. Rather than modifying an array, `slice()` copies or *extracts* a given number of elements to a new array, leaving the array it is called upon untouched. `slice()` takes only 2 parameters — the first is the index at which to begin extraction, and the second is the index at which to stop extraction (extraction will occur up to, but not including the element at this index). Consider this:
接下来我们要介绍 `slice()` 方法。 `slice()` 不会修改数组,而是会复制,或者说*提取extract*给定数量的元素到一个新数组。 `slice()` 只接收 2 个输入参数:第一个是开始提取元素的位置(索引),第二个是提取元素的结束位置(索引)。 提取的元素中不包括第二个参数所对应的元素。 如下示例:
```js
let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];
let todaysWeather = weatherConditions.slice(1, 3);
// todaysWeather equals ['snow', 'sleet'];
// weatherConditions still equals ['rain', 'snow', 'sleet', 'hail', 'clear']
```
In effect, we have created a new array by extracting elements from an existing array.
`todaysWeather` 值为 `['snow', 'sleet']``weatherConditions` 值仍然为 `['rain', 'snow', 'sleet', 'hail', 'clear']`
在上面的代码中,我们从一个数组中提取了一些元素,并用这些元素创建了一个新数组。
# --instructions--
We have defined a function, `forecast`, that takes an array as an argument. Modify the function using `slice()` to extract information from the argument array and return a new array that contains the elements `'warm'` and `'sunny'`.
我们已经定义了一个 `forecast` 函数,它接受一个数组作为参数。 请修改这个函数,利用 `slice()` 从输入的数组中提取信息,最终返回一个包含元素 `warm` `sunny` 的新数组。
# --hints--
`forecast` should return `["warm", "sunny"]`
`forecast` 应返回 `["warm", "sunny"]`
```js
assert.deepEqual(
@ -35,7 +35,7 @@ assert.deepEqual(
);
```
The `forecast` function should utilize the `slice()` method
`forecast` 函数中应使用 `slice()` 方法。
```js
assert(/\.slice\(/.test(code));

View File

@ -1,6 +1,6 @@
---
id: 587d7b7b367417b2b2512b16
title: Create complex multi-dimensional arrays
title: 创建复杂的多维数组
challengeType: 1
forumTopicId: 301159
dashedName: create-complex-multi-dimensional-arrays
@ -8,52 +8,54 @@ dashedName: create-complex-multi-dimensional-arrays
# --description--
Awesome! You have just learned a ton about arrays! This has been a fairly high level overview, and there is plenty more to learn about working with arrays, much of which you will see in later sections. But before moving on to looking at <dfn>Objects</dfn>, lets take one more look, and see how arrays can become a bit more complex than what we have seen in previous challenges.
很好! 你现在已经学到很多关于数组的知识了, 但这些只是个开始。我们将在接下来的中挑战中学到更多与数组相关的知识。 在继续学习对象(<dfn>Objects</dfn>)之前,让我们再花一点时间了解下更复杂的数组嵌套。
One of the most powerful features when thinking of arrays as data structures, is that arrays can contain, or even be completely made up of other arrays. We have seen arrays that contain arrays in previous challenges, but fairly simple ones. However, arrays can contain an infinite depth of arrays that can contain other arrays, each with their own arbitrary levels of depth, and so on. In this way, an array can very quickly become very complex data structure, known as a <dfn>multi-dimensional</dfn>, or nested array. Consider the following example:
数组的一个强大的特性是,它可以包含其他数组,甚至完全由其他数组组成。 在上一个挑战中,我们已经接触到了包含数组的数组,但它还算是比较简单的。 数组中的数组还可以再包含其他数组,即可以嵌套任意多层数组。 习惯上,我们称这种数据结构为<dfn>多维(multi-dimensional)数组</dfn>或嵌套(nested)数组。 请看如下的示例:
```js
let nestedArray = [ // top, or first level - the outer most array
['deep'], // an array within an array, 2 levels of depth
let nestedArray = [
['deep'],
[
['deeper'], ['deeper'] // 2 arrays nested 3 levels deep
['deeper'], ['deeper']
],
[
[
['deepest'], ['deepest'] // 2 arrays nested 4 levels deep
['deepest'], ['deepest']
],
[
[
['deepest-est?'] // an array nested 5 levels deep
['deepest-est?']
]
]
]
];
```
While this example may seem convoluted, this level of complexity is not unheard of, or even unusual, when dealing with large amounts of data. However, we can still very easily access the deepest levels of an array this complex with bracket notation:
`deep` 数组已嵌套 2 层。 `deeper` 数组嵌套了 3 层。 `deepest` 数组嵌套了 3 层, `deepest-est?` 嵌套了 5 层。
虽然这个例子看起来错综复杂,不过,尤其是在处理大量数据的时候,这种数据结构还是会用到的。 尽管结构复杂,不过我们仍可以通过方括号表示法来访问嵌套得最深的数组:
```js
console.log(nestedArray[2][1][0][0][0]);
// logs: deepest-est?
```
And now that we know where that piece of data is, we can reset it if we need to:
控制台打印的是字符串 `deepest-est?`。 既然我们知道数据的位置,当然,我们也可以修改它:
```js
nestedArray[2][1][0][0][0] = 'deeper still';
console.log(nestedArray[2][1][0][0][0]);
// now logs: deeper still
```
现在控制台打印的是 `deeper still`
# --instructions--
We have defined a variable, `myNestedArray`, set equal to an array. Modify `myNestedArray`, using any combination of <dfn>strings</dfn>, <dfn>numbers</dfn>, and <dfn>booleans</dfn> for data elements, so that it has exactly five levels of depth (remember, the outer-most array is level 1). Somewhere on the third level, include the string `'deep'`, on the fourth level, include the string `'deeper'`, and on the fifth level, include the string `'deepest'`.
我们已经定义了一个叫做 `myNestedArray` 的数组变量。 请修改 `myNestedArray`,使用字符串(<dfn>string</dfn>)、数字(<dfn>number</dfn>)或布尔值(<dfn>boolean</dfn>)的任意组合作为数组的元素,并让 myNestedArray 刚好有 5 层(注意,最外层的数组是第 1 层)。 同时,请在第 3 层的数组中包含字符串 `deep`;在第 4 层的数组中包含字符串 `deeper`,在第 5 层的数组中包含字符串 `deepest`
# --hints--
`myNestedArray` should contain only numbers, booleans, and strings as data elements
`myNestedArray` 中的数据元素应只包含字符串、数字或者布尔值。
```js
assert.strictEqual(
@ -77,7 +79,7 @@ assert.strictEqual(
);
```
`myNestedArray` should have exactly 5 levels of depth
`myNestedArray` 应刚好包含 5 层嵌套数组。
```js
assert.strictEqual(
@ -100,7 +102,7 @@ assert.strictEqual(
);
```
`myNestedArray` should contain exactly one occurrence of the string `"deep"` on an array nested 3 levels deep
`myNestedArray` 中应只有一个字符串 `deep`,并且应出现在第 3 层数组中。
```js
assert(
@ -129,7 +131,7 @@ assert(
);
```
`myNestedArray` should contain exactly one occurrence of the string `"deeper"` on an array nested 4 levels deep
`myNestedArray` 中应只有一个字符串 `deeper`,并且应出现在第 4 层数组中。
```js
assert(
@ -158,7 +160,7 @@ assert(
);
```
`myNestedArray` should contain exactly one occurrence of the string `"deepest"` on an array nested 5 levels deep
`myNestedArray` 中应只有一个字符串 `deepest`,并且应出现在第 5 层数组中。
```js
assert(

View File

@ -1,6 +1,6 @@
---
id: 587d7b7b367417b2b2512b15
title: Iterate Through All an Array's Items Using For Loops
title: 使用 for 循环遍历数组中的全部元素
challengeType: 1
forumTopicId: 301161
dashedName: iterate-through-all-an-arrays-items-using-for-loops
@ -8,9 +8,9 @@ dashedName: iterate-through-all-an-arrays-items-using-for-loops
# --description--
Sometimes when working with arrays, it is very handy to be able to iterate through each item to find one or more elements that we might need, or to manipulate an array based on which data items meet a certain set of criteria. JavaScript offers several built in methods that each iterate over arrays in slightly different ways to achieve different results (such as `every()`, `forEach()`, `map()`, etc.), however the technique which is most flexible and offers us the greatest amount of control is a simple `for` loop.
使用数组时,我们经常需要遍历数组的所有元素来找出我们需要的一个或多个元素,抑或是对数组执行一些特定的操作。 JavaScript 为我们提供了几个内置的方法,它们以不同的方式遍历数组,以便我们可以用于不同的场景(如 `every()``forEach()``map()` 等等)。 然而,最简单的 `for` 循环不仅能实现上述这些方法的功能,而且相比之下也会更加灵活。
Consider the following:
请看以下的例子:
```js
function greaterThanTen(arr) {
@ -24,18 +24,17 @@ function greaterThanTen(arr) {
}
greaterThanTen([2, 12, 8, 14, 80, 0, 1]);
// returns [12, 14, 80]
```
Using a `for` loop, this function iterates through and accesses each element of the array, and subjects it to a simple test that we have created. In this way, we have easily and programmatically determined which data items are greater than `10`, and returned a new array containing those items.
在这个函数中,我们用一个 `for` 循环来遍历数组,逐一对其中的元素进行判断。 通过上面的代码,我们可以找出数组中大于 `10` 的所有元素,并返回一个包含这些元素的新数组 `[12, 14, 80]`
# --instructions--
We have defined a function, `filteredArray`, which takes `arr`, a nested array, and `elem` as arguments, and returns a new array. `elem` represents an element that may or may not be present on one or more of the arrays nested within `arr`. Modify the function, using a `for` loop, to return a filtered version of the passed array such that any array nested within `arr` containing `elem` has been removed.
我们已经定义了 `filteredArray` 函数,它接受一个嵌套的数组 `arr` 和一个 `elem` 作为参数,并要返回一个新数组。 `arr` 数组中嵌套的数组里可能包含 `elem` 元素,也可能不包含。 请修改该函数,用一个 `for` 循环来做筛选,使函数返回一个由 `arr` 中不包含 `elem` 的数组所组成的新数组。
# --hints--
`filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)` should return `[ [10, 8, 3], [14, 6, 23] ]`
`filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)` 应返回 `[[10, 8, 3], [14, 6, 23]]`
```js
assert.deepEqual(
@ -54,7 +53,7 @@ assert.deepEqual(
);
```
`filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)` should return `[ ["flutes", 4] ]`
`filteredArray([["trumpets", 2], ["flutes", 4], ["saxophones", 2]], 2)` 应返回 `[["flutes", 4]]`
```js
assert.deepEqual(
@ -70,7 +69,7 @@ assert.deepEqual(
);
```
`filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")` should return `[ ["amy", "beth", "sam"] ]`
`filteredArray([["amy", "beth", "sam"], ["dave", "sean", "peter"]], "peter")` 应返回 `[["amy", "beth", "sam"]]`
```js
assert.deepEqual(
@ -85,7 +84,7 @@ assert.deepEqual(
);
```
`filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)` should return `[ ]`
`filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)` 应返回 `[]`
```js
assert.deepEqual(
@ -102,7 +101,7 @@ assert.deepEqual(
);
```
The `filteredArray` function should utilize a `for` loop
`filteredArray` 函数中应使用 `for` 循环。
```js
assert.notStrictEqual(filteredArray.toString().search(/for/), -1);

View File

@ -1,6 +1,6 @@
---
id: 587d7b7d367417b2b2512b1d
title: Iterate Through the Keys of an Object with a for...in Statement
title: 使用 for...in 语句遍历对象
challengeType: 1
forumTopicId: 301162
dashedName: iterate-through-the-keys-of-an-object-with-a-for---in-statement
@ -8,25 +8,23 @@ dashedName: iterate-through-the-keys-of-an-object-with-a-for---in-statement
# --description--
Sometimes you may need to iterate through all the keys within an object. This requires a specific syntax in JavaScript called a <dfn>for...in</dfn> statement. For our `users` object, this could look like:
如果我们想要遍历对象中的所有属性, 只需要使用 JavaScript 中的 <dfn>for...in</dfn> 语句即可。 以遍历 `users` 对象的属性为例:
```js
for (let user in users) {
console.log(user);
}
// logs:
Alan
Jeff
Sarah
Ryan
```
In this statement, we defined a variable `user`, and as you can see, this variable was reset during each iteration to each of the object's keys as the statement looped through the object, resulting in each user's name being printed to the console. **NOTE:** Objects do not maintain an ordering to stored keys like arrays do; thus a key's position on an object, or the relative order in which it appears, is irrelevant when referencing or accessing that key.
这将在控制台打印 `Alan``Jeff``Sarah``Ryan` - 每个值占一行。
在上面的代码中,我们定义了一个 `user` 变量。 可以观察到,这个变量在遍历对象的语句执行过程中会一直被重置并赋予新值,结果就是不同的用户名打印到了 console 中。
**注意:**对象中的键是无序的,这与数组不同。 因此,一个对象中某个属性的位置,或者说它出现的相对顺序,在引用或访问该属性时是不确定的。
# --instructions--
We've defined a function `countOnline` which accepts one argument (a users object). Use a <dfn>for...in</dfn> statement within this function to loop through the users object passed into the function and return the number of users whose `online` property is set to `true`. An example of a users object which could be passed to `countOnline` is shown below. Each user will have an `online` property with either a `true` or `false` value.
我们已经定义了一个 `countOnline` 函数,它接收一个 users 对象参数。 请在其中使用 <dfn>for...in</dfn> 语句来遍历传入函数的 users 对象中的用户,并返回 `online` 属性为 `true` 的用户数量。 以下是一个传入 `countOnline` 函数的对象示例, 注意每个用户都有 `online` 属性,其属性值为 `true` `false`
```js
{
@ -44,7 +42,7 @@ We've defined a function `countOnline` which accepts one argument (a users objec
# --hints--
The function `countOnline` should use a `for in` statement to iterate through the object keys of the object passed to it.
函数 `countOnline` 中应使用 `for in` 语句遍历传入的对象的对象键。
```js
assert(
@ -54,19 +52,19 @@ assert(
);
```
The function `countOnline` should return `1` when the object `{ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }` is passed to it
当传入 `{ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }` 时,函数 `countOnline` 应该返回 `1`
```js
assert(countOnline(usersObj1) === 1);
```
The function `countOnline` should return `2` when the object `{ Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }` is passed to it
当传入 `{ Alan: { online: true }, Jeff: { online: false }, Sarah: { online: true } }` 时,函数 `countOnline` 应该返回 `2`
```js
assert(countOnline(usersObj2) === 2);
```
The function `countOnline` should return `0` when the object `{ Alan: { online: false }, Jeff: { online: false }, Sarah: { online: false } }` is passed to it
当传入 `{ Alan: { online: false }, Jeff: { online: false }, Sarah: { online: false } }` 时,函数 `countOnline` 应该返回 `0`
```js
assert(countOnline(usersObj3) === 0);

View File

@ -1,6 +1,6 @@
---
id: 587d78b2367417b2b2512b0f
title: Remove Items from an Array with pop() and shift()
title: 使用 pop() shift() 从数组中删除元素
challengeType: 1
forumTopicId: 301165
dashedName: remove-items-from-an-array-with-pop-and-shift
@ -8,35 +8,39 @@ dashedName: remove-items-from-an-array-with-pop-and-shift
# --description--
Both `push()` and `unshift()` have corresponding methods that are nearly functional opposites: `pop()` and `shift()`. As you may have guessed by now, instead of adding, `pop()` *removes* an element from the end of an array, while `shift()` removes an element from the beginning. The key difference between `pop()` and `shift()` and their cousins `push()` and `unshift()`, is that neither method takes parameters, and each only allows an array to be modified by a single element at a time.
`push()` `unshift()` 都有一个与它们作用相反的函数:`pop()` `shift()`。 与插入元素相反,`pop()` 会从数组的末尾*移除*一个元素,而 `shift()` 会从数组的开头移除一个元素。 `pop()` `shift()` `push()` `unshift()` 的关键区别在于,用于删除元素的方法不接收参数,而且每次只能删除数组中的一个元素。
Let's take a look:
让我们来看以下的例子:
```js
let greetings = ['whats up?', 'hello', 'see ya!'];
greetings.pop();
// now equals ['whats up?', 'hello']
greetings.shift();
// now equals ['hello']
```
We can also return the value of the removed element with either method like this:
`greetings` 值为 `['whats up?', 'hello']`
```js
greetings.shift();
```
`greetings` 值为 `['hello']`
这些用于删除数组元素的方法会返回被删除的元素:
```js
let popped = greetings.pop();
// returns 'hello'
// greetings now equals []
```
`greetings` 值为 `[]``popped` 值为 `hello`
# --instructions--
We have defined a function, `popShift`, which takes an array as an argument and returns a new array. Modify the function, using `pop()` and `shift()`, to remove the first and last elements of the argument array, and assign the removed elements to their corresponding variables, so that the returned array contains their values.
我们已经定义了一个 `popShift` 函数,它接收一个数组作为输入参数并返回一个新的数组。 请修改这个函数,使用 `pop()` `shift()` 来移除输入的数组中的第一个元素和最后一个元素,并将这两个被移除的元素分别赋值给对应的变量,使得最终返回的数组里包含这两个值。
# --hints--
`popShift(["challenge", "is", "not", "complete"])` should return `["challenge", "complete"]`
`popShift(["challenge", "is", "not", "complete"])` 应返回 `["challenge", "complete"]`
```js
assert.deepEqual(popShift(['challenge', 'is', 'not', 'complete']), [
@ -45,13 +49,13 @@ assert.deepEqual(popShift(['challenge', 'is', 'not', 'complete']), [
]);
```
The `popShift` function should utilize the `pop()` method
`popShift` 函数中应使用 `pop()` 方法。
```js
assert.notStrictEqual(popShift.toString().search(/\.pop\(/), -1);
```
The `popShift` function should utilize the `shift()` method
`popShift` 函数中应使用 `shift()` 方法。
```js
assert.notStrictEqual(popShift.toString().search(/\.shift\(/), -1);

View File

@ -1,6 +1,6 @@
---
id: 587d78b2367417b2b2512b10
title: Remove Items Using splice()
title: 使用 splice() 删除元素
challengeType: 1
forumTopicId: 301166
dashedName: remove-items-using-splice
@ -8,34 +8,35 @@ dashedName: remove-items-using-splice
# --description--
Ok, so we've learned how to remove elements from the beginning and end of arrays using `shift()` and `pop()`, but what if we want to remove an element from somewhere in the middle? Or remove more than one element at once? Well, that's where `splice()` comes in. `splice()` allows us to do just that: **remove any number of consecutive elements** from anywhere in an array.
在之前的挑战中,我们已经学习了如何用 `shift()` `pop()` 从数组的开头或末尾移除元素。 但如果我们想删除数组中间的一个元素, 或者想一次删除多个元素,该如何操作呢? 这时候我们就需要使用 `splice()` 方法了, `splice()` 可以让我们从数组中的任意位置**连续删除任意数量的元素**。
`splice()` can take up to 3 parameters, but for now, we'll focus on just the first 2. The first two parameters of `splice()` are integers which represent indexes, or positions, of the array that `splice()` is being called upon. And remember, arrays are *zero-indexed*, so to indicate the first element of an array, we would use `0`. `splice()`'s first parameter represents the index on the array from which to begin removing elements, while the second parameter indicates the number of elements to delete. For example:
`splice()` 最多可以接受 3 个参数,但现在我们先关注前两个。 `splice()` 接收的前两个参数是整数,表示正在调用 的`splice()` 数组中的元素的索引或位置。 别忘了,数组的索引是*从 0 开始的*,所以我们要用 `0` 来表示数组中的第一个元素。 `splice()` 的第一个参数代表从数组中的哪个索引开始移除元素,而第二个参数表示要从数组中的这个位置开始删除多少个元素。 例如:
```js
let array = ['today', 'was', 'not', 'so', 'great'];
array.splice(2, 2);
// remove 2 elements beginning with the 3rd element
// array now equals ['today', 'was', 'great']
```
`splice()` not only modifies the array it's being called on, but it also returns a new array containing the value of the removed elements:
这里我们移除 2 个元素,首先是第三个元素(索引为 2`array` 会有值 `['today', 'was', 'great']`
`splice()` 不仅会修改调用该方法的数组,还会返回一个包含被移除元素的数组:
```js
let array = ['I', 'am', 'feeling', 'really', 'happy'];
let newArray = array.splice(3, 2);
// newArray equals ['really', 'happy']
```
`newArray` 值为 `['really', 'happy']`
# --instructions--
We've initialized an array `arr`. Use `splice()` to remove elements from `arr`, so that it only contains elements that sum to the value of `10`.
我们已经定义了数组 `arr`。 请使用 `splice()` `arr` 里移除元素,使剩余的元素之和为 `10`
# --hints--
You should not change the original line of `const arr = [2, 4, 5, 1, 7, 5, 2, 1];`.
不应修改这一行 `const arr = [2, 4, 5, 1, 7, 5, 2, 1];`
```js
assert(
@ -43,7 +44,7 @@ assert(
);
```
`arr` should only contain elements that sum to `10`.
`arr` 的剩余元素之和应为 `10`
```js
assert.strictEqual(
@ -52,13 +53,13 @@ assert.strictEqual(
);
```
Your code should utilize the `splice()` method on `arr`.
应对 `arr` 调用 `splice()` 方法。
```js
assert(__helpers.removeWhiteSpace(code).match(/arr\.splice\(/));
```
The splice should only remove elements from `arr` and not add any additional elements to `arr`.
splice 应只删除 `arr` 里面的元素,不能给 `arr` 添加元素。
```js
assert(

View File

@ -1,6 +1,6 @@
---
id: 587d7b7e367417b2b2512b20
title: Use an Array to Store a Collection of Data
title: 使用数组存储不同类型的数据
challengeType: 1
forumTopicId: 301167
dashedName: use-an-array-to-store-a-collection-of-data
@ -8,15 +8,16 @@ dashedName: use-an-array-to-store-a-collection-of-data
# --description--
The below is an example of the simplest implementation of an array data structure. This is known as a <dfn>one-dimensional array</dfn>, meaning it only has one level, or that it does not have any other arrays nested within it. Notice it contains <dfn>booleans</dfn>, <dfn>strings</dfn>, and <dfn>numbers</dfn>, among other valid JavaScript data types:
以下是最简单的数组Array示例 这是一个一维数组(<dfn>one-dimensional array</dfn>),它只有一层,或者说它里面没有包含其它数组。 可以观察到,这个数组中只包含了布尔值(<dfn>booleans</dfn>)、字符串(<dfn>strings</dfn>)、数字(<dfn>numbers</dfn>)以及 JavaScript 中的其他数据类型:
```js
let simpleArray = ['one', 2, 'three', true, false, undefined, null];
console.log(simpleArray.length);
// logs 7
```
All arrays have a length property, which as shown above, can be very easily accessed with the syntax `Array.length`. A more complex implementation of an array can be seen below. This is known as a <dfn>multi-dimensional array</dfn>, or an array that contains other arrays. Notice that this array also contains JavaScript <dfn>objects</dfn>, which we will examine very closely in our next section, but for now, all you need to know is that arrays are also capable of storing complex objects.
调用 `console.log` 显示 `7`
所有数组都有一个表示长度的属性,我们可以通过 `Array.length` 来访问它。 下面是一个关于数组的更复杂的例子。 这是一个多维数组 <dfn>multi-dimensional Array</dfn>),或者说是一个包含了其他数组的数组。 可以注意到,在它的内部还包含了 JavaScript 中的对象(<dfn>objects</dfn>)结构。 我们会在后面的小节中讨论该数据结构,但现在你只需要知道数组能够存储复杂的对象类型数据。
```js
let complexArray = [
@ -45,35 +46,35 @@ let complexArray = [
# --instructions--
We have defined a variable called `yourArray`. Complete the statement by assigning an array of at least 5 elements in length to the `yourArray` variable. Your array should contain at least one <dfn>string</dfn>, one <dfn>number</dfn>, and one <dfn>boolean</dfn>.
我们已经定义了一个名为 `yourArray` 的变量。 请修改代码,将一个含有至少 5 个元素的数组赋值给 `yourArray` 变量。 你的数组中应包含至少一个 <dfn>string</dfn> 类型的数据、一个 <dfn>number</dfn> 类型的数据和一个 <dfn>boolean</dfn> 类型的数据。
# --hints--
`yourArray` should be an array.
`yourArray` 应为数组。
```js
assert.strictEqual(Array.isArray(yourArray), true);
```
`yourArray` should be at least 5 elements long.
`yourArray` 应包含至少 5 个元素。
```js
assert.isAtLeast(yourArray.length, 5);
```
`yourArray` should contain at least one `boolean`.
`yourArray` 应包含至少一个 `boolean`
```js
assert(yourArray.filter((el) => typeof el === 'boolean').length >= 1);
```
`yourArray` should contain at least one `number`.
`yourArray` 应包含至少一个 `number`
```js
assert(yourArray.filter((el) => typeof el === 'number').length >= 1);
```
`yourArray` should contain at least one `string`.
`yourArray` 应包含至少一个 `string`
```js
assert(yourArray.filter((el) => typeof el === 'string').length >= 1);