This is one foundational lesson in the twelve-lesson course.

The question to explore

Build a playlist that knows when to stop

What makes repeated work stop at the right moment?

Build a collection one track at a time. Choose a goal and a stopping rule, then watch what each check allows. Repeated AI work needs stopping rules too.

Local teaching illustration / No AI model call / About 7 minutes / No account needed

Try the experiment

First step: Choose a goal of 4. Step once to check the empty collection, then step again to add one track.

A small collection, built one action at a time

Fill the playlist. Know when to stop.

Eight fictional tracks are waiting. Your rule decides how many enter the collection. No music plays and no AI runs.

The source shelf / eight available tracks
First light
Window seat
Small hours
Green room
Slow tide
Paper moon
Long way home
Last lantern
Current collection0 / 4

0 additions completed

  1. 1. Check the rule
  2. 2. Add one track
  3. 3. Stop

Check before adding: count ≥ 4?

Next: check the stopping rule.

Choose a goal, then run or step.

  1. Empty slot 1
  2. Empty slot 2
  3. Empty slot 3
  4. Empty slot 4
  5. Empty slot 5
  6. Empty slot 6
  7. Empty slot 7
  8. Empty slot 8

One step checks the rule or adds one track. An iteration is one addition. The separate safety cap allows at most eight additions, even with a wrong rule. Changing the goal or rule clears the old run.

Your recipe and exact result
Goal: 4 tracks. Before each addition, stop if count ≥ 4. Otherwise add one track and check again. The collection starts empty.
Every check and addition
  1. No actions yet.
  1. Choose a goal of 4. Step once to check the empty collection, then step again to add one track.
  2. Run the remaining steps or keep stepping. Notice the final check stops the loop without adding another track.
  3. Try the wrong stopping rule with the same goal. Inspect the extra track, repair the rule, then keep the recipe of your result.

The idea

Start with no tracks. Check the goal before adding one. After each addition, check again. One addition is one iteration. If the goal is zero, the right rule stops before any work.

Stopping when the count reaches the goal gives the right length. Waiting until it passes the goal adds one too many. Repair the rule and compare. Eight additions is the safety cap, not proof of success. These fictional tracks have no audio and use no AI.

Worked example: run it yourself

The same playlist loop in about ten lines. It runs both stopping rules for a goal of 4 and a goal of 0, and says whether the rule or the safety cap stopped each run.

Save it as playlist.js and run node playlist.js in a terminal (Node.js 18 or later), or paste it into your browser’s developer console. Download playlist.js

const CAP = 8;
function build(goal, rule) {
  let count = 0;
  while (true) {
    const stop = rule === 'reaches' ? count >= goal : count > goal;
    if (stop) return `goal ${goal}, ${rule}: added ${count}, the rule stopped it`;
    if (count >= CAP) return `goal ${goal}, ${rule}: added ${count}, the safety cap stopped it`;
    count += 1; // add the next track
  }
}
console.log(build(4, 'reaches'));
console.log(build(4, 'passes'));
console.log(build(0, 'reaches'));
console.log(build(0, 'passes'));

Output, as printed by Node.js 25.3.0 on 3 December 2026:

goal 4, reaches: added 4, the rule stopped it
goal 4, passes: added 5, the rule stopped it
goal 0, reaches: added 0, the rule stopped it
goal 0, passes: added 1, the rule stopped it

Try it

Predict the result for a goal of 8 with the passes rule. Then add console.log(build(8, 'passes')) and run it.

Check your answer

goal 8, passes: added 8, the safety cap stopped it. The wrong rule wanted a ninth track and the cap blocked it. The count looks right, but the rule is still broken. A cap limits the work; it does not show the goal was met for the right reason.

Take this with you

What changed?

A loop changes state, checks a condition and repeats. The stopping rule decides when the task is finished; a safety cap only bounds how much work can happen.

Try explaining it.

With a goal of zero, how many tracks should each stopping rule add? Why does the check belong before the addition?

Open a possible explanation

The reaches-goal rule stops at zero without adding anything. The passes-goal rule adds one track before zero is exceeded. Checking before the action lets a loop correctly do no work when its goal is already met.

Choose a next step

Continue the course in order, or open the course map.