- Explain some benefits of loops
- Construct a FOR loop
- List the 4 parts of a FOR loop
- Loop through an array of values
- Loop through an array of objects
- Modify a loop to use conditional logic
-
Explain some benefits of loops
- DRY: Don't Repeat Yourself
- Prevents you from repeating yourself
- Iterate through multiple times if needed without RY
- Easily lets you set parameters
- Flexible, dynamic code
- Easy to adjust
- DRY: Don't Repeat Yourself
-
FOR loop setup
- initializer => starting point for the loop
- condition => gets evaluated BEFORE loop runs (every time)
- final expression => code that runs AFTER each run of loop
- statement => code that actually runs in the loop
var numArray = [1, 3, 5, 7, 9];
var objArray = [
{ product: 'tennis ball', price: 3 },
{ product: 'baseball', price: 5 },
{ product: 'basketball', price: 10 },
{ product: 'volleyball', price: 15 },
{ product: 'football', price: 20 },
];