2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: bd7993c9c69feddfaeb8bdef
|
|
|
|
title: Store Multiple Values in one Variable using JavaScript Arrays
|
|
|
|
challengeType: 1
|
2019-02-14 12:24:02 -05:00
|
|
|
videoUrl: 'https://scrimba.com/c/crZQWAm'
|
2019-07-31 11:32:23 -07:00
|
|
|
forumTopicId: 18309
|
2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --description--
|
|
|
|
|
|
|
|
With JavaScript `array` variables, we can store several pieces of data in one place.
|
|
|
|
|
2018-10-08 01:01:53 +01:00
|
|
|
You start an array declaration with an opening square bracket, end it with a closing square bracket, and put a comma between each entry, like this:
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`var sandwich = ["peanut butter", "jelly", "bread"]`.
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
|
|
Modify the new array `myArray` so that it contains both a `string` and a `number` (in that order).
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
**Hint**
|
|
|
|
Refer to the example code in the text editor if you get stuck.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --hints--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`myArray` should be an `array`.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
2020-11-27 19:02:05 +01:00
|
|
|
assert(typeof myArray == 'object');
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
The first item in `myArray` should be a `string`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(typeof myArray[0] !== 'undefined' && typeof myArray[0] == 'string');
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
The second item in `myArray` should be a `number`.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(typeof myArray[1] !== 'undefined' && typeof myArray[1] == 'number');
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
## --after-user-code--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
2018-10-20 21:02:47 +03:00
|
|
|
(function(z){return z;})(myArray);
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
## --seed-contents--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
// Only change code below this line
|
|
|
|
var myArray = [];
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --solutions--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
var myArray = ["The Answer", 42];
|
|
|
|
```
|