# 章序号/节序号/节/笔记序号
codecademy,loop
# 概念阐释
- 在处理数组时,循环可以自动检测到数组中的每一个元素,直到循环结束。
- 循环中的**初始化变量**与**数组变量**形成**条件**或**更新** 之间存在关系。
# 举例子

- 在**条件**一栏要用`.length`属性
- `i = index`
- `animals.length` animals数组中有几个值
- `animals[i]`访问所有数组值
# 类比、比较与对比
# 问题
**1.**
写一个变量,赋值你最想去的3个旅游景点
用loop和array让这三个地方自动print,加上这句话“I would love to visit ...”
Write a `for` loop that iterates through our `vacationSpots` array using `i` as the iterator variable.
Inside the block of the `for` loop, use `console.log()` to log each element in the `vacationSpots` array after the string `'I would love to visit '`. For example, the first round of the loop should print `'I would love to visit Bali'` to the console.
# 问题答案
- 注意标点符号不是`,`而是`;`
```js
const vacationSpots = ['Bali', 'Paris', 'Tulum'];
// Write your code below
for (let i=0; i<vacationSpots.length;/*变量的条件,验证真假值*/ i++/*更新的操作语句*/){
console.log(`I would love to visit ${vacationSpots[i]}`);
}
```
# 备注(经验集/错误集)
## 经验集
## 错误集
**检查这段代码:**
```js
let groceries = ['brown sugar','salt','cranberries','walnuts'];
for(let i = groceries; i<groceries.length; i++){
console.log(i);
}
```
**这段代码有两个问题:**
1. 在 for 循环中,**将 `i` 初始化为 `groceries`,而不是一个数字**,应该更正为:`for (let i = 0; i < groceries.length; i++)`
2. 在 `console.log()` 语句中,需要打印 **`groceries[i]`** 的值,而不是 `i` 的值。应该更正为:`console.log(groceries[i]);`
更正后的代码如下:
```js
let groceries = ['brown sugar', 'salt', 'cranberries', 'walnuts'];
for (let i = 0; i < groceries.length; i++) {
console.log(groceries[i]);
}
```
## 参考资料