37 lines
517 B
Markdown
37 lines
517 B
Markdown
|
---
|
||
|
title: Iterate with JavaScript While Loops
|
||
|
---
|
||
|
## Iterate with JavaScript While Loops
|
||
|
While loops will run as long as the condition inside the ( ) is true.
|
||
|
Example:
|
||
|
```javascript
|
||
|
while(condition){
|
||
|
code...
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## Hint 1:
|
||
|
Use a iterator variable such as i in your condition
|
||
|
```javascript
|
||
|
var i = 0;
|
||
|
while(i <= 4){
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## Spoiler Alert Solution Ahead!
|
||
|
|
||
|
## Solution:
|
||
|
|
||
|
```javascript
|
||
|
// Setup
|
||
|
var myArray = [];
|
||
|
|
||
|
// Only change code below this line.
|
||
|
var i = 0;
|
||
|
while (i <= 4){
|
||
|
myArray.push(i);
|
||
|
i++;
|
||
|
}
|
||
|
|
||
|
```
|