2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 587d7b86367417b2b2512b3b
|
2021-03-15 03:20:39 +00:00
|
|
|
|
title: 捕获使用索引的时候出现的错误
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 1
|
2020-09-07 08:09:54 +00:00
|
|
|
|
forumTopicId: 301189
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: catch-off-by-one-errors-when-using-indexing
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
当试图访问字符串或数组的特定索引(分割或访问一个片段)或循环索引时,有时会出现 <dfn>Off by one errors</dfn> 错误(有时称为 OBOE)。 JavaScript 索引从 0 开始,而不是 1,这意味着最后一个索引总会比字符串或数组的长度少 1。 如果尝试访问等于长度的索引,程序可能会抛出“索引超出范围”引用错误或打印出 `undefined`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
当使用将索引范围作为参数的字符串或数组方法时,阅读相关的文档并了解参数中的索引的包含性(即是否考虑进返回值中)很重要。 以下是一些错误的示例:
|
2020-09-07 08:09:54 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let alphabet = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
let len = alphabet.length;
|
|
|
|
|
for (let i = 0; i <= len; i++) {
|
|
|
|
|
console.log(alphabet[i]);
|
|
|
|
|
}
|
|
|
|
|
for (let j = 1; j < len; j++) {
|
|
|
|
|
console.log(alphabet[j]);
|
|
|
|
|
}
|
|
|
|
|
for (let k = 0; k < len; k++) {
|
|
|
|
|
console.log(alphabet[k]);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
第一个例子多了一次循环,第二个例子少了一次循环(漏掉了索引 0 处的字符), 第三个例子是正确的。
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
修复以下函数中的两个索引错误,将 1 到 5 之间(包含 1 和 5)的所有数字打印到控制台。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
应该设置循环的初始条件,使循环从第一个索引开始。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(code.match(/i\s*?=\s*?0\s*?;/g).length == 1);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
应修复循环的初始条件,使循环从索引 0 开始。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(!code.match(/i\s?=\s*?1\s*?;/g));
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
应该设置循环的终止条件,使循环在最后一个索引处停止。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/i\s*?<\s*?len\s*?;/g).length == 1);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
应该修复循环的终止条件,使循环在索引为字符串长度减 1 的位置停止。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(!code.match(/i\s*?<=\s*?len;/g));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function countToFive() {
|
|
|
|
|
let firstFive = "12345";
|
|
|
|
|
let len = firstFive.length;
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
for (let i = 1; i <= len; i++) {
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
console.log(firstFive[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
countToFive();
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
2020-09-07 08:09:54 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
function countToFive() {
|
|
|
|
|
let firstFive = "12345";
|
|
|
|
|
let len = firstFive.length;
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
console.log(firstFive[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
countToFive();
|
|
|
|
|
```
|