Loops | An algorithm's repeat

Loops allow us to automate a repeated task. This class looks at some basic uses for loops.

Prep

Morning Activity: Plan out a repeated action

Focus: Based on any of your brainstorming activities (or look at live websites), find situations where repetition is necessary. todo apps etc are a good example

  • In small groups, take about 5 minutes to review your past ideas for things that could be looped. find 1 simple example

    • tip: consider sites, apps, or anything that you have interest in building
  • Talk through one another’s loops, help consider potential bugs, problems, or code that could be useful

  • Take notes on each other feedback

Class Debrief

  • Everyone will get a couple minutes to talk about their loops and the elements that are looping

For Loops

In Class Activity: Render things with loops

  1. in a code pen, Log text out in a console with a for loop
  2. in the same code pen, render the content onto the web page

For In Loops

  • These can be used to loop through objects

Activity

  1. Create a simple object in a codepen
  2. iterate through and render the keys in a console
  3. iterate through and render the values in a console
  4. iterate through and render both the keys and values onto a page

For Of Loop

  • A simpler and more modern for loop

Activity

  • Create an array of objects
  • use a for of loop to iterate through the array
  • use a for in loop inside the for of loop
  • render values from the object to the console

Other Loops

  • While Loops
    • excel when you don’t know the number of iterations
  • Do While Loops
    • great for when you need to execute someonething once before evaluating the looping condition

Player Randomizer

  • Demo of player randomizer code that makes use of while loops and other javascript language features
  • This will be made available for optimization during lab time

Today's Achievement

End of Day Activity: Optimize the demo code

  • This code can be optimized and improved in a few different ways, pick a feature to add to it or refactor it (for an added challenge) to improve it for:
    • readability
    • feature support
    • performance