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. Build a small collection by repeating one action. Set a goal, watch the count change, and decide which rule should stop the work. A loop used by an AI helper also needs a stopping rule and a separate safety limit.
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.
0 additions completed
- 1. Check the rule
- 2. Add one track
- 3. Stop
Check before adding: count ≥ 4?
Next: check the stopping rule.
Choose a goal, then run or step.
- Empty slot 1
- Empty slot 2
- Empty slot 3
- Empty slot 4
- Empty slot 5
- Empty slot 6
- Empty slot 7
- 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
- No actions yet.
- Choose a goal of 4. Step once to check the empty collection, then step again to add one track.
- Run the remaining steps or keep stepping. Notice the final check stops the loop without adding another track.
- 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. The collection begins empty. First check whether its count has reached your goal. If not, add the next track, increase the count by one, then return to the check. Each addition is an iteration. This is a pre-test loop, like a while loop: the condition runs before the body. Its invariant is that count always equals the number of tracks collected, because every iteration adds one track and one to count. Checking before adding also handles a goal of zero: there is nothing to do, and the body never runs.
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. “Stop when the count reaches the goal” and “stop after the count passes the goal” produce different collections. The second rule adds one extra track: count > goal where count >= goal was meant, the classic off-by-one error. Repair that rule and try the same goal again. A separate cap of eight additions prevents unbounded work, but reaching that cap is not the same as meeting the goal. The track names and durations are fictional; this local program does not play music or call 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. The loop checks the stopping rule, then the cap, then adds, the same order as the experiment. The passes rule (count > goal) runs one extra iteration for every goal, zero included. Because the rule is checked before the cap, a correct rule always gets the first chance to stop.
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 itTry 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.