2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: bd7993c9c69feddfaeb8bdef
|
2021-03-14 21:20:39 -06:00
|
|
|
title: 使用 JavaScript 数组将多个值存储在一个变量中
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/crZQWAm'
|
|
|
|
forumTopicId: 18309
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: store-multiple-values-in-one-variable-using-javascript-arrays
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
使用数组(`array`),我们可以在一个地方存储多个数据。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
以左方括号开始定义一个数组,以右方括号结束,里面每个元素之间用逗号隔开,例如:
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-04-06 00:04:04 +09:00
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const sandwich = ["peanut butter", "jelly", "bread"];
|
2021-04-06 00:04:04 +09:00
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-06-14 23:47:03 +09:00
|
|
|
创建一个包含字符串和数字(按照字符串和数字的顺序)的数组 `myArray`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-06-14 23:47:03 +09:00
|
|
|
`myArray` 应为数组。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(typeof myArray == 'object');
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-06-14 23:47:03 +09:00
|
|
|
`myArray` 数组的第一个元素应该是一个字符串。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(typeof myArray[0] !== 'undefined' && typeof myArray[0] == 'string');
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-06-14 23:47:03 +09:00
|
|
|
`myArray` 数组的第二个元素应该是一个数字。
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(typeof myArray[1] !== 'undefined' && typeof myArray[1] == 'number');
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
(function(z){return z;})(myArray);
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Only change code below this line
|
2021-10-27 15:10:57 +00:00
|
|
|
const myArray = [];
|
2021-01-13 03:31:00 +01:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const myArray = ["The Answer", 42];
|
2021-01-13 03:31:00 +01:00
|
|
|
```
|