2018-10-04 14:47:55 +01:00
---
title: Iterate Through an Array with a For Loop
---
2019-07-24 00:59:27 -07:00
# Iterate Through an Array with a For Loop
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
---
## Hints
### Hint 1
2018-10-14 18:27:46 +02:00
Remember the structure of a `for` loop:
`for ([initialization]; [condition]; [final-expression])
statement`
- The `[initialization]` part is executed only once (the first time).
- The `[condition]` is checked on every iteration.
- The `[final-expression]` is executed along the `statement` if `[condition]` resolves to `true` .
2018-10-04 14:47:55 +01:00
2019-07-24 00:59:27 -07:00
### Hint 2
2019-03-08 19:36:08 -03:00
Remember how accumulators work:
`var x += i`
- The variable `x` is going to act as the accumulator.
- The variable `i` is the one which value will be stored (and accumulated) inside `x`
- The expression `+=` is an just abreviation of `x = x + i`
2018-10-04 14:47:55 +01:00
2018-10-14 18:27:46 +02:00
2019-07-24 00:59:27 -07:00
---
## Solutions
2018-10-14 18:27:46 +02:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2018-10-14 18:27:46 +02:00
```javascript
2019-03-08 19:36:08 -03:00
var total = 0;
2018-10-14 18:27:46 +02:00
for (var i = 0; i < myArr.length ; i + + ) {
total += myArr[i];
}
```
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-14 18:27:46 +02:00
- Inititialization: `i` gets a value of `0` and its used as a counter.
- Condition: the subsequent code is executed as long as `i` is lower than the length of `myArr` (which is 5; five numbers but arrays are zero based).
- Final-expression: `i` is incremented by `1` .
- Statement: The function adds `myArr[i]` 's value to `total` until the condition isn't met like so:
2019-07-24 00:59:27 -07:00
```
2018-10-14 18:27:46 +02:00
total + myArr[0] -> 0 + 2 = 2
total + myArr[1] -> 2 + 3 = 5
total + myArr[2] -> 5 + 4 = 9
total + myArr[3] -> 9 + 5 = 14
total + myArr[4] -> 14 + 6 = 20
```
2019-07-24 00:59:27 -07:00
< / details >
2018-10-14 18:27:46 +02:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 2 (Click to Show/Hide)< / summary >
2018-10-14 18:27:46 +02:00
```javascript
for (var y = myArr.length - 1; y >= 0; y--) {
total += myArr[y];
}
```
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-14 18:27:46 +02:00
This works similarly to the last solution but it's faster< sup >< a href = " #cite1 " > 1</ a ></ sup > although it might not meet your requirements if order is important.
- Initialization: `y` gets the `myArr.length` 's value once so the function doesn't need to check it at `condition` every time the loop is executed.
- Condition: the loop is executed as long as `y` is greater than `0` .
- Final-expression: `y` is decremented by `1` .
- Statement: The function adds `myArr[y]` 's value to `total` until the condition isn't met like so:
2019-07-24 00:59:27 -07:00
```
2018-10-14 18:27:46 +02:00
total + myArr[4] -> 0 + 6 = 6
total + myArr[3] -> 6 + 5 = 11
total + myArr[2] -> 11 + 4 = 15
total + myArr[1] -> 15 + 3 = 18
total + myArr[0] -> 18 + 2 = 20
```
2019-07-24 00:59:27 -07:00
#### Relevant Links
2018-10-14 18:27:46 +02:00
< span id = "cite1" > 1</ span > . ["Are loops really faster in reverse?", *stackoverflow.com* ](https://stackoverflow.com/questions/1340589/are-loops-really-faster-in-reverse )
2019-07-24 00:59:27 -07:00
#### Relevant Links
2018-10-14 18:27:46 +02:00
- ["for" - *MDN JavaScript reference* ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for )
2019-07-24 00:59:27 -07:00
< / details >
2018-10-14 18:27:46 +02:00