Thinking in Code
Math · Foundational · Lesson 8 of 9

Loops with Memory

Counters, totals and off-by-one

An interactive lesson with 7 steps, about 6 minutes: you predict, drag and solve, with instant feedback on every answer. It runs in your browser and needs JavaScript.

What you’ll learn

  • An accumulator starts at 0 and a loop keeps adding to it.
  • range(a, b) runs from a up to b − 1: b − a values.
  • Off-by-one (fencepost) errors hide in loop limits; test small cases.

Steps

  1. Running totals
  2. Adding squares
  3. Reaching 55
  4. Fenceposts
  5. How many times?
  6. The biggest so far
  7. Counting multiples