1. Reference Guide

Term Definition Example
Iteration One pass through the body of a loop i goes 1, 2, 3…
Initialization Sets the counter once, before the loop starts let i = 0
Condition Checked before each pass. false ends the loop i < 5
Increment Updates the counter after each pass i++ or i += 2
Infinite loop A loop whose condition never becomes false while (true) { }
Key The name of a property in an object name in {name: "Casey"}

Picking a Loop

Goal Loop
Repeat a set number of times for (let i = 0; i < 5; i++)
Visit every item in an array for (let i = 0; i < arr.length; i++)
Visit every key of an object for (let key in obj)
Repeat until something changes while (condition)

Count by 2s with i += 2. Count down with i--.


2. LxD Cycle Process

Empathize: Students copy and paste the same line five times instead of writing a loop, and when they do write one it runs one time too many, too few, or never stops. Even students who pass programming tests often hold wrong mental models of how loops execute (Bayman & Mayer, 1983).

Define:

  • POV: Students need to see every loop as “start, check, change,” because a missing part causes the bug.
  • Learning Goal: Students will write for, for...in, and while loops over numbers, arrays, and objects, and control where a loop starts, stops, and how it counts.

Ideate: How might we get students to ask “when does this stop?” before they run a loop? Activity: loop over games and player stats, then finish a self-checking coin total loop.

Prototype: A reference guide, two live examples, two popcorn hacks, and a homework hack that checks itself with PASS/FIX lines.

Test: Peers finish the popcorn hacks without extra explanation while we watch whether they set the condition on purpose or guess. We revise any unclear step, then revise again after teaching and grading.


3. CS111 Course Requirements

MiraCosta College’s CS 111 accepts JavaScript as a substitute for Java (MiraCosta College, n.d.). The CS 111 alignment rubric lists Iteration under Control Structures (“Use loops for game object arrays, animation frames”), and Arrays and Objects (JSON) under Data Types (Open Coding Society, 2025).

How this lesson matches: Example A and Popcorn Hack 2 practice for and for...in loops over arrays and objects. Example B, Popcorn Hack 1, and the homework practice while loops and a loop with n in the condition. forEach is not covered.


4. Lesson Plan

Learning Objective: Use iteration to repeat code, and choose between a for loop and a while loop.

Success Criteria:

You can… Practice Graded in
Set a loop’s start, condition, and change to count up, down, or by 2s Tech Talk, Example B Popcorn 1, Popcorn 2
Loop through an array or an object Example A Not graded
Write a loop with n in the condition and no function Homework Hack Homework loop, Homework sum

Tech Talk (5 minutes)

An iteration repeats a block of code. Every loop needs a start, a condition, and a change.

for (let i = 1; i <= 5; i++) {   // start; condition; change
    console.log("Level " + i);
}

let level = 1;                   // start
while (level <= 5) {             // condition
    console.log("Level " + level);
    level++;                     // change
}

Both print Level 1 to Level 5. Use for when you know how many times to repeat, and while when you only know the rule for stopping. If the change is missing, you get an infinite loop.


5. Code Examples

Example A: Arrays and Objects

A for loop visits each array element by index i, stopping at videoGames.length. for...in visits each key of an object, and playerStats[key] gets its value.

Try it: add two of your own games and change the stats to your own gamer profile.

Code Runner Challenge

Run the loops, then add two of your own video games and change the stats to your own gamer profile

View IPYNB Source
%%js
// CODE_RUNNER: Run the loops, then add two of your own video games and change the stats to your own gamer profile

let videoGames = ["Minecraft", "Roblox", "Fortnite"];

for (let i = 0; i < videoGames.length; i++) {
    console.log(videoGames[i]);
}

const playerStats = { // Define an object
    name: "Sriansh",
    level: 14,
    favoriteGame: "Greenville",
};

for (let key in playerStats) { // Notice we are using for... in... instead of for... of...
    console.log(key + ": " + playerStats[key]);
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Example B: while Loops

A while loop repeats as long as its condition is true. Use it when you only know the rule for when to stop. It still needs a start, a condition, and a change inside the loop.

Try it: change the starting value, the condition, and the increment (e.g., coins += 2), then make it count down.

Code Runner Challenge

Run the loop, then change the starting value, the condition, and the increment

View IPYNB Source
%%js
// CODE_RUNNER: Run the loop, then change the starting value, the condition, and the increment

let coins = 0;

while (coins < 5) {
  console.log("Coins collected:", coins);
  coins += 1;
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

6. Hacks & Practice Tasks

How to Submit

This lesson is submitted as code. Run each hack, save each runner with its 💾 button, then press Submit Code below the lesson. In the Notes box, paste:

Lesson: JavaScript Iterations
Popcorn 1: countdown from 10 to 1 then GO! (yes/no)
Popcorn 2: bonus levels 2 to 20 with a for loop (yes/no)
Homework: every check shows PASS (yes/no)

Popcorn Hack 1 (In-Class): Countdown

Write a loop that counts down from 10 to 1, then prints “GO!” once.

Hint: start at 10, keep going while the counter is at least 1, and subtract 1 each pass.

Show answer while (countdown >= 1) { console.log(countdown); countdown--; }

Code Runner Challenge

Finish the countdown loop, then run it

View IPYNB Source
%%js
// CODE_RUNNER: Finish the countdown loop, then run it

let countdown = 10;

// Write your while loop here


console.log("GO!");
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2 (In-Class): Bonus Levels

Every even level is a bonus level. Print the bonus levels from 2 to 20 with a for loop.

Hint: the change is not i++. It adds 2 each pass.

Show answer for (let i = 2; i <= 20; i += 2) { console.log("Bonus level " + i); }

Code Runner Challenge

Finish the for loop so it prints Bonus level 2, 4, 6, ... 20

View IPYNB Source
%%js
// CODE_RUNNER: Finish the for loop so it prints Bonus level 2, 4, 6, ... 20

for (let i = 0; i < 0; i++) {
    console.log("Bonus level " + i);
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework Hack: Challenge Iteration

On level 1 you collect 1 coin, on level 2 you collect 2 coins, and so on. Use a loop (no function) to add up the total coins for levels 1 to n. Test with n = 5 (answer: 15), then change n to your own value.

Hint: use n in your condition. Press Run until every check line says PASS.

Code Runner Challenge

Finish the loop to add up the coins for levels 1 to n, then run it. Keep going until every check says PASS

View IPYNB Source
%%js
// CODE_RUNNER: Finish the loop to add up the coins for levels 1 to n, then run it. Keep going until every check says PASS

let n = 5;
let sum = 0;

// Write your loop here


// ---- You do not need to change anything below this line ----
console.log("Total coins for " + n + " levels: " + sum);
console.log((sum === n * (n + 1) / 2 ? "PASS" : "FIX") + "  sum matches the formula n * (n + 1) / 2 = " + n * (n + 1) / 2);
console.log((n !== 5 ? "PASS" : "FIX") + "  n changed from 5 to a value of your choice");
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Grading Plan (1 Point Total)

Part Points What earns the points
Popcorn 1 0.2 The countdown prints 10 to 1, then “GO!” once.
Popcorn 2 0.2 A for loop prints the bonus levels 2 to 20.
Homework: loop 0.3 Uses a for or while loop with n in the condition, and no function.
Homework: sum 0.3 The check lines all show PASS, for n = 5 and for your own n.
Total 1.0  

7. Lesson Revisions

Revision Made: The first draft was only frontmatter and a stack of loop examples. We rebuilt it in the LxD format with a Reference Guide, LxD Cycle, CS111 requirements, and a grading plan. We swapped the fruits array for video games, removed the function from the homework so it stays about loops, added PASS/FIX self-checks for instant feedback, and added a second popcorn hack for counting down.


8. Feedback Evidence

Feedback Received: Peers said the first draft did not match the other posted lessons, so it was unclear where to start; the fruits example felt disconnected, and video games would be more relatable; the function in the challenge made it feel like a functions lesson; and the point values were hard to find until the bottom of the page.


9. References

MiraCosta College. (n.d.). Computer science course inventory. catalog.miracosta.edu

Open Coding Society. (2025). Sprint 6 plan: CS 111 course alignment rubric [_notebooks/CSSE/SprintObjectives/2025-01-06-sprint6_plan.ipynb]. github.com/Open-Coding-Society/pages

Mozilla. (2026). for statement. MDN Web Docs. developer.mozilla.org

Mozilla. (2026). while statement. MDN Web Docs. developer.mozilla.org

Mozilla. (2026). for…in statement. MDN Web Docs. developer.mozilla.org

Bayman, P., & Mayer, R. E. (1983). A diagnosis of beginning programmers’ misconceptions of BASIC programming statements. Communications of the ACM, 26(9), 677–679. doi.org/10.1145/358172.358408

Submit Assignment

Choose what to hand in: click Add to submission on each runner you want graded (hacks, popcorn hacks), and save it. Leaving out practice runners does not lower your grade, but a required hack you leave out is graded as missing. Your selection is sent as a Gist for automatic review.

    Need to update a submission later? Open the submissions dashboard.