Skip to content

Instantly share code, notes, and snippets.

@matt-winzer
Last active February 13, 2018 07:56
Show Gist options
  • Save matt-winzer/aaf5a7e78e61b432b0fb38601ac1fd6c to your computer and use it in GitHub Desktop.
Save matt-winzer/aaf5a7e78e61b432b0fb38601ac1fd6c to your computer and use it in GitHub Desktop.

JavaScript: Intermediate Loops

Objectives

  • 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

Notes

  • 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
  • FOR loop setup

    1. initializer => starting point for the loop
    2. condition => gets evaluated BEFORE loop runs (every time)
    3. final expression => code that runs AFTER each run of loop
    4. statement => code that actually runs in the loop

Resources:

Sample Arrays

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 },
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment