chore(i8n,learn): processed translations (#41462)
Co-authored-by: Crowdin Bot <support+bot@crowdin.com>
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 5a661e0f1068aca922b3ef17
|
||||
title: Access an Array's Contents Using Bracket Notation
|
||||
title: 使用方括号访问数组的元素
|
||||
challengeType: 1
|
||||
forumTopicId: 301149
|
||||
dashedName: access-an-arrays-contents-using-bracket-notation
|
||||
@ -8,55 +8,55 @@ dashedName: access-an-arrays-contents-using-bracket-notation
|
||||
|
||||
# --description--
|
||||
|
||||
The fundamental feature of any data structure is, of course, the ability to not only store data, but to be able to retrieve that data on command. So, now that we've learned how to create an array, let's begin to think about how we can access that array's information.
|
||||
所有数据结构的基本特性是,它们不仅可以存储数据,还可以让我们按需访问存放在其中的数据。 我们已经学习了如何创建数组,现在让我们来学习如何访问数组中的数据。
|
||||
|
||||
When we define a simple array as seen below, there are 3 items in it:
|
||||
我们先定义一个包含 3 个元素的数组:
|
||||
|
||||
```js
|
||||
let ourArray = ["a", "b", "c"];
|
||||
```
|
||||
|
||||
In an array, each array item has an <dfn>index</dfn>. This index doubles as the position of that item in the array, and how you reference it. However, it is important to note, that JavaScript arrays are <dfn>zero-indexed</dfn>, meaning that the first element of an array is actually at the ***zeroth*** position, not the first. In order to retrieve an element from an array we can enclose an index in brackets and append it to the end of an array, or more commonly, to a variable which references an array object. This is known as <dfn>bracket notation</dfn>. For example, if we want to retrieve the `"a"` from `ourArray` and assign it to a variable, we can do so with the following code:
|
||||
在数组中,内部的每个元素都有一个与之对应的索引(<dfn>index</dfn>)。 索引既是该元素在数组中的位置,也是我们访问该元素的引用。 需要注意的是,JavaScript 数组的索引是从 0 开始的(这种从 0 开始的规则叫做 <dfn>zero-indexed</dfn>),即数组的第一个元素是在数组中的***第 0 个***位置,而不是第 1 个位置。 要从数组中获取一个元素,我们可以在数组字面量后面加一个用方括号括起来的索引。不过习惯上,我们会通过表示数组的变量名来访问,而不是直接通过字面量。 这种从数组中读取元素的方式叫做方括号表示法(<dfn>bracket notation</dfn>)。 如果我们要从数组 `ourArray` 中取出数据 `a` 并将其赋值给另一个变量,可以这样写:
|
||||
|
||||
```js
|
||||
let ourVariable = ourArray[0];
|
||||
// ourVariable equals "a"
|
||||
```
|
||||
|
||||
In addition to accessing the value associated with an index, you can also *set* an index to a value using the same notation:
|
||||
现在,变量 `ourVariable` 的值也为 `a`。
|
||||
|
||||
通过索引,我们不仅可以获取某个元素值,还可以用类似的写法来*设置*一个索引位置的元素值:
|
||||
|
||||
```js
|
||||
ourArray[1] = "not b anymore";
|
||||
// ourArray now equals ["a", "not b anymore", "c"];
|
||||
```
|
||||
|
||||
Using bracket notation, we have now reset the item at index 1 from `"b"`, to `"not b anymore"`.
|
||||
在上面的代码中,我们用方括号表示法把索引为 1 的元素从 `b` 改成了 `not b anymore`。 现在 `ourArray` 的值是 `["a", "not b anymore", "c"]`。
|
||||
|
||||
# --instructions--
|
||||
|
||||
In order to complete this challenge, set the 2nd position (index `1`) of `myArray` to anything you want, besides `"b"`.
|
||||
在本挑战中,请将 `myArray` 中的第二个元素(索引为 `1`)设置为除了 `b` 以外的任意值。
|
||||
|
||||
# --hints--
|
||||
|
||||
`myArray[0]` should be equal to `"a"`
|
||||
`myArray[0]` 应为 `a`。
|
||||
|
||||
```js
|
||||
assert.strictEqual(myArray[0], 'a');
|
||||
```
|
||||
|
||||
`myArray[1]` should not be equal to `"b"`
|
||||
`myArray[1]` 不应为 `b`。
|
||||
|
||||
```js
|
||||
assert.notStrictEqual(myArray[1], 'b');
|
||||
```
|
||||
|
||||
`myArray[2]` should be equal to `"c"`
|
||||
`myArray[2]` 应为 `c`。
|
||||
|
||||
```js
|
||||
assert.strictEqual(myArray[2], 'c');
|
||||
```
|
||||
|
||||
`myArray[3]` should be equal to `"d"`
|
||||
`myArray[3]` 应为 `d`。
|
||||
|
||||
```js
|
||||
assert.strictEqual(myArray[3], 'd');
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d78b2367417b2b2512b0e
|
||||
title: Add Items to an Array with push() and unshift()
|
||||
title: 使用 push() 和 unshift() 为数组添加元素
|
||||
challengeType: 1
|
||||
forumTopicId: 301151
|
||||
dashedName: add-items-to-an-array-with-push-and-unshift
|
||||
@ -8,28 +8,32 @@ dashedName: add-items-to-an-array-with-push-and-unshift
|
||||
|
||||
# --description--
|
||||
|
||||
An array's length, like the data types it can contain, is not fixed. Arrays can be defined with a length of any number of elements, and elements can be added or removed over time; in other words, arrays are <dfn>mutable</dfn>. In this challenge, we will look at two methods with which we can programmatically modify an array: `Array.push()` and `Array.unshift()`.
|
||||
数组的长度与数组能包含的数据类型一样,都是不固定的。 数组可以包含任意数量的元素,可以不限次数地往数组中添加元素或者从中移除元素。 总之,数组是可变的(<dfn>mutable</dfn>)。 在本挑战中,我们要学习两种修改数组的方法:`Array.push()` 和 `Array.unshift()`。
|
||||
|
||||
Both methods take one or more elements as parameters and add those elements to the array the method is being called on; the `push()` method adds elements to the end of an array, and `unshift()` adds elements to the beginning. Consider the following:
|
||||
这两个方法都接收一个或多个元素作为参数,并会将参数中的元素添加到该数组中。 `push()` 方法会将元素插入到数组的末尾,而 `unshift()` 方法会将元素插入到数组的开头。 请看以下例子:
|
||||
|
||||
```js
|
||||
let twentyThree = 'XXIII';
|
||||
let romanNumerals = ['XXI', 'XXII'];
|
||||
|
||||
romanNumerals.unshift('XIX', 'XX');
|
||||
// now equals ['XIX', 'XX', 'XXI', 'XXII']
|
||||
|
||||
romanNumerals.push(twentyThree);
|
||||
// now equals ['XIX', 'XX', 'XXI', 'XXII', 'XXIII']Notice that we can also pass variables, which allows us even greater flexibility in dynamically modifying our array's data.
|
||||
```
|
||||
|
||||
`romanNumerals` 的值就变成了 `['XIX', 'XX', 'XXI', 'XXII']`。
|
||||
|
||||
```js
|
||||
romanNumerals.push(twentyThree);
|
||||
```
|
||||
|
||||
`romanNumerals` 的值现在就变成了 `['XIX', 'XX', 'XXI', 'XXII', 'XXIII']`。 请注意这里,我们也可以使用变量作为参数,这让我们在动态修改数组数据时更加灵活。
|
||||
|
||||
# --instructions--
|
||||
|
||||
We have defined a function, `mixedNumbers`, which we are passing an array as an argument. Modify the function by using `push()` and `unshift()` to add `'I', 2, 'three'` to the beginning of the array and `7, 'VIII', 9` to the end so that the returned array contains representations of the numbers 1-9 in order.
|
||||
我们已经定义了一个 `mixedNumbers` 函数,它接收一个数组作为参数。 请修改这个函数,使用 `push()` 和 `unshift()` 来将 `'I', 2, 'three'` 插入到数组开头;将 `7, 'VIII', 9` 插入到数组的末尾。最终这个函数的返回值就会是一个依次包含不同形式的 1-9 的数组。
|
||||
|
||||
# --hints--
|
||||
|
||||
`mixedNumbers(["IV", 5, "six"])` should now return `["I", 2, "three", "IV", 5, "six", 7, "VIII", 9]`
|
||||
`mixedNumbers(["IV", 5, "six"])` 应返回 `["I", 2, "three", "IV", 5, "six", 7, "VIII", 9]`。
|
||||
|
||||
```js
|
||||
assert.deepEqual(mixedNumbers(['IV', 5, 'six']), [
|
||||
@ -45,13 +49,13 @@ assert.deepEqual(mixedNumbers(['IV', 5, 'six']), [
|
||||
]);
|
||||
```
|
||||
|
||||
The `mixedNumbers` function should utilize the `push()` method
|
||||
`mixedNumbers` 函数中应调用 `push()` 方法。
|
||||
|
||||
```js
|
||||
assert(mixedNumbers.toString().match(/\.push/));
|
||||
```
|
||||
|
||||
The `mixedNumbers` function should utilize the `unshift()` method
|
||||
`mixedNumbers` 函数中应调用 `unshift()` 方法。
|
||||
|
||||
```js
|
||||
assert(mixedNumbers.toString().match(/\.unshift/));
|
||||
|
Reference in New Issue
Block a user