2018-10-10 18:03:03 -04:00
---
id: cf1111c1c11feddfaeb8bdef
2021-02-06 04:42:36 +00:00
title: Modify Array Data With Indexes
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/czQM4A8'
forumTopicId: 18241
2021-01-13 03:31:00 +01:00
dashedName: modify-array-data-with-indexes
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
Unlike strings, the entries of arrays are < dfn > mutable< / dfn > and can be changed freely.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
**Example**
2020-04-29 18:29:13 +08:00
```js
var ourArray = [50,40,30];
ourArray[0] = 15; // equals [15,40,30]
```
2021-02-06 04:42:36 +00:00
**Note**
There shouldn't be any spaces between the array name and the square brackets, like `array [0]` . Although JavaScript is able to process this correctly, this may confuse other programmers reading your code.
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-02-06 04:42:36 +00:00
Modify the data stored at index `0` of `myArray` to a value of `45` .
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-02-06 04:42:36 +00:00
`myArray` should now be [45,64,99].
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
(function () {
if (
typeof myArray != 'undefined' & &
myArray[0] == 45 & &
myArray[1] == 64 & &
myArray[2] == 99
) {
return true;
} else {
return false;
}
})()
);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
You should be using correct index to modify the value in `myArray` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
(function () {
if (code.match(/myArray\[0\]\s*=\s*/g)) {
return true;
} else {
return false;
}
})()
);
2018-10-10 18:03:03 -04:00
```
2021-01-13 03:31:00 +01:00
# --seed--
## --after-user-code--
```js
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
```
## --seed-contents--
```js
// Setup
var myArray = [18,64,99];
// Only change code below this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
```js
var myArray = [18,64,99];
myArray[0] = 45;
```