2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: cf1111c1c11feddfaeb8bdef
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 通过索引修改数组中的数据
|
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
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
与字符串的数据不可变不同,数组的数据是可变的,并且可以自由地改变。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
**示例**
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
var ourArray = [50,40,30];
|
|
|
|
ourArray[0] = 15; // equals [15,40,30]
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
**提示**
|
|
|
|
数组名称和方括号之间不应有任何空格,如`array [0]`尽管 JavaScript 能够正确处理,但可能会让看你代码的其他程序员感到困惑。
|
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
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
修改数组`myArray`中索引0上的值为`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
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`myArray`的值应该 [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
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
你应该使用正确的索引修改`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
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
2020-04-29 18:29:13 +08:00
|
|
|
|