2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: bd7993c9c69feddfaeb8bdef
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/crZQWAm'
|
|
|
|
forumTopicId: 18309
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 使用 JavaScript 数组将多个值存储在一个变量中
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='description'>
|
|
|
|
使用<code>数组</code>,我们可以在一个地方存储多个数据。
|
|
|
|
以左方括号<code>[</code>开始定义一个数组,以右方括号<code>]</code>结束,里面每个元素之间用逗号隔开,例如:
|
|
|
|
<code>var sandwich = ["peanut butter", "jelly", "bread"]</code>.
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
创建一个包含<code>字符串</code>和<code>数字</code>的数组<code>myArray</code>。
|
|
|
|
<strong>提示</strong><br>如果你遇到困难,请参考文本编辑器中的示例代码。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>myArray</code>应该是一个<code>数组</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(typeof myArray == 'object');
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>myArray</code>数组的第一个元素应该是一个<code>字符串</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(typeof myArray[0] !== 'undefined' && typeof myArray[0] == 'string');
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>myArray</code>数组的第二个元素应该是一个<code>数字</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(typeof myArray[1] !== 'undefined' && typeof myArray[1] == 'number');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Example
|
|
|
|
var ourArray = ["John", 23];
|
|
|
|
|
|
|
|
// Only change code below this line.
|
|
|
|
var myArray = [];
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
(function(z){return z;})(myArray);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
var myArray = ["The Answer", 42];
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|