2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Iterate with JavaScript While Loops
|
|
|
|
---
|
2019-07-24 00:59:27 -07:00
|
|
|
# Iterate with JavaScript While Loops
|
|
|
|
|
|
|
|
---
|
|
|
|
## Problem Explanation
|
2018-10-12 15:37:13 -04:00
|
|
|
While loops will run as long as the condition inside the ( ) is true.
|
2019-07-24 00:59:27 -07:00
|
|
|
|
2018-10-12 15:37:13 -04:00
|
|
|
Example:
|
2019-07-24 00:59:27 -07:00
|
|
|
|
2018-10-12 15:37:13 -04:00
|
|
|
```javascript
|
2019-07-24 00:59:27 -07:00
|
|
|
while (condition) {
|
|
|
|
//code...
|
2018-10-12 15:37:13 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Hints
|
|
|
|
|
|
|
|
### Hint 1
|
2018-10-12 15:37:13 -04:00
|
|
|
Use a iterator variable such as i in your condition
|
|
|
|
```javascript
|
|
|
|
var i = 0;
|
2019-07-24 00:59:27 -07:00
|
|
|
while (i <= 4) {}
|
2018-10-12 15:37:13 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Solutions
|
|
|
|
|
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
// Setup
|
|
|
|
var myArray = [];
|
|
|
|
|
|
|
|
// Only change code below this line.
|
|
|
|
var i = 0;
|
2019-07-24 00:59:27 -07:00
|
|
|
while (i <= 4) {
|
|
|
|
myArray.push(i);
|
|
|
|
i++;
|
2018-10-12 15:37:13 -04:00
|
|
|
}
|
|
|
|
```
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|