2018-09-30 23:01:58 +01:00
---
id: cf1111c1c11feddfaeb8bdef
title: Modify Array Data With Indexes
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/czQM4A8'
2019-07-31 11:32:23 -07:00
forumTopicId: 18241
2021-01-13 03:31:00 +01:00
dashedName: modify-array-data-with-indexes
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2021-10-26 01:55:58 +09:00
Unlike strings, the entries of arrays are < dfn > mutable</ dfn > and can be changed freely, even if the array was declared with `const` .
2020-11-27 19:02:05 +01:00
**Example**
2019-05-17 06:20:30 -07:00
```js
2021-10-26 01:55:58 +09:00
const ourArray = [50, 40, 30];
2021-03-02 16:12:12 -08:00
ourArray[0] = 15;
2019-05-17 06:20:30 -07:00
```
2021-03-02 16:12:12 -08:00
`ourArray` now has the value `[15, 40, 30]` .
**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-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Modify the data stored at index `0` of `myArray` to a value of `45` .
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
2021-10-26 01:55:58 +09:00
`myArray` should now be `[45, 64, 99]` .
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(
(function () {
if (
typeof myArray != 'undefined' & &
myArray[0] == 45 & &
myArray[1] == 64 & &
myArray[2] == 99
) {
return true;
} else {
return false;
}
})()
);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
You should be using correct index to modify the value in `myArray` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
if (code.match(/myArray\[0\]\s*=\s*/g)) {
return true;
} else {
return false;
}
})()
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --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
if(typeof myArray !== "undefined"){(function(){return 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
// Setup
2021-10-26 01:55:58 +09:00
const myArray = [18, 64, 99];
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
// Only change code below this line
2021-10-26 01:55:58 +09:00
2020-11-27 19:02:05 +01:00
```
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
2021-10-26 01:55:58 +09:00
const myArray = [18, 64, 99];
2018-09-30 23:01:58 +01:00
myArray[0] = 45;
```