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
- Type what you are saving for, what it costs, and how much you save each week.
- Click the button. The page tells you how many weeks it takes.
- The page also lists your total after every week.
- Try typing 0 for the weekly amount. The page refuses. The next section explains why.
How it works
savedis the accumulator. It starts at 0 and grows every week.weeksis 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
Make the page
Make
index.htmlwith the three inputs, the button, an#answerparagraph, and a#logarea. The controls appear, but nothing happens yet.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
whilecondition.Write the loop
In the click handler, read the boxes and turn them into numbers. Then write the
whileloop withsaved,weeks, and one log line per week. Test it with the numbers from step 2. The page should match your paper.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 ofsavings.jsin the console. It uses asafetycounter andbreakto 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; } }Check the input
Add an
isNaNcheck 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
whilecondition. - 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
safetycounter in the console instead. - Link the
perWeek <= 0check straight back to step 4. A weekly amount of zero causes the same infinite loop. Say that out loud.