Skip to main content
← JavaScript

Lesson 3 of 11

Savings Goal Counter

You save $15 a week for a $120 skateboard. A while loop counts the weeks for you.

What you will learn

  • while
  • The accumulator pattern
  • Counters
  • Stopping a loop
  • break
  • Input validation

Before you start

Times Table Grid. A for loop repeats a set number of times. A while loop repeats until something becomes true. With a while loop you do not know the count in advance.

What this page does

  1. Type what you are saving for, what it costs, and how much you save each week.
  2. Click the button. The page tells you how many weeks it takes.
  3. The page also lists your total after every week.
  4. Try typing 0 for the weekly amount. The page refuses. The next section explains why.

How it works

  • saved is the accumulator. It starts at 0 and grows every week.
  • weeks is the counter. It starts at 0 and adds 1 every week.
  • while (saved < goal) runs the loop body again and again. The loop stops as soon as that condition is false.
let saved = 0;
let weeks = 0;

while (saved < goal) {
    saved = saved + perWeek;   // the accumulator grows
    weeks = weeks + 1;         // the counter grows
    log("Week " + weeks + ": $" + saved);
}

Build it step by step

  1. Make the page

    Make index.html with the three inputs, the button, an #answer paragraph, and a #log area. The controls appear, but nothing happens yet.

  2. Work it out on paper first

    Take a goal of 10 and a weekly amount of 4. Week 1 gives 4. Week 2 gives 8. Week 3 gives 12. That passes the goal, so you stop. You stopped when you reached the goal. That sentence is your while condition.

  3. Write the loop

    In the click handler, read the boxes and turn them into numbers. Then write the while loop with saved, weeks, and one log line per week. Test it with the numbers from step 2. The page should match your paper.

  4. See an infinite loop safely

    Work out what happens if you forget the line saved = saved + perWeek. Write down your answer first. Do not delete the line. Deleting it freezes the tab. Instead, run the safe version at the bottom of savings.js in the console. It uses a safety counter and break to stop the loop after 1000 passes.

    let safety = 0;
    while (saved < goal) {
        // the line that grows "saved" is missing on purpose
        weeks = weeks + 1;
    
        safety = safety + 1;
        if (safety > 1000) {
            console.log("Stopped. This loop was never going to end.");
            break;
        }
    }
  5. Check the input

    Add an isNaN check so the page refuses text. Then add a check that refuses a weekly amount of 0 or less. That second check catches the infinite loop from step 4 before it can start.

Try these changes

Make these changes in the files beside this guide. Each one only uses what this lesson has already covered. Tick each one when it works.

Check yourself

Three quick questions on this lesson. Get them all right to finish the lesson and earn a pack.

Loading your answers

Teacher's versionHow to run this lesson, what to check, and any answers. Students do not need this.

How to teach it

  • Trace the loop on paper before writing any code. Ask "how did we know when to stop?" The student's own answer becomes the while condition.
  • Step 4 is the most important part of the lesson. Ask the student to predict what happens with the missing line. Have them write the prediction down before they run anything.
  • Never delete the line for real. A frozen tab teaches nothing. It also costs five minutes. Use the safe version with the safety counter in the console instead.
  • Link the perWeek <= 0 check straight back to step 4. A weekly amount of zero causes the same infinite loop. Say that out loud.