You will see short one-instruction branches such as if (ready) count += 1;. They work like the braced branches you already know, but the condition controls only that one instruction. Use braces when writing your own branches if that makes the grouping clearer.
One loop, two questions
An accumulator starts with a useful starting value: zero when you are adding a total. A counter tracks how many items meet a rule. Keep them separate and name them by meaning. Trace both columns on paper before running.
// Start both values once, before the loop.
let total = 0;
let longSessions = 0;
for (let minutes = 10; minutes <= 40; minutes += 10) {
// Add every session to the total.
total += minutes;
// Count only sessions that meet this rule.
if (minutes >= 30) {
longSessions += 1;
}
}
console.log(total);
console.log(longSessions);Check your prediction
100
2The total changes four times, but the conditional count changes only twice.
day++ is shorthand for increasing day by one. In a for-loop update it has the same effect as day = day + 1. Prefer the longer form until you can trace it confidently.
Stop or skip?
break exits the nearest loop. continue skips the rest of the current iteration and proceeds to the next. Use them only when the rule is clearer than an ordinary condition. In a while loop, continue can accidentally skip the update and make the loop infinite. Use bounded for loops while practising these patterns.
Trace task: list the values printed by a loop from 1 through 5 that continues when the value is 3. Then replace continue with break. Explain the difference before running your version. Test a loop that runs zero times: does the total still have a useful value?
A common bug resets the accumulator inside the loop. That forgets earlier iterations. Put initialization before the loop, updates inside it, and the final report afterward.
Trace a running total without losing earlier work
The loop has three phases: initialize before it, update inside it, report after it. The accumulator remembers everything accepted so far. A counter remembers how many times a rule matched. They answer different questions, so changing one must not accidentally replace the other.
Use a trace table for the existing example before reading another solution. After minutes=10, total is 10 and longSessions is 0; after 20 they are 30 and 0; after 30 they are 60 and 1; after 40 they are 100 and 2. The loop’s current value is not the same thing as the total built from all current and earlier values.
let wrong = 0;
let total = 0;
for (let n = 1; n <= 3; n = n + 1) {
wrong = 0;
wrong = wrong + n;
total = total + n;
}
console.log(wrong);
console.log(total);Check your prediction
3
6 Both variables are updated with n, but wrong forgets earlier work at the start of each iteration. The code runs without a syntax error, so you need a multi-iteration test to reveal it. A one-iteration test would make both results look correct.
let skipped = 0;
for (let n = 1; n <= 5; n = n + 1) {
if (n === 3) {
continue;
}
skipped = skipped + n;
}
let stopped = 0;
for (let n = 1; n <= 5; n = n + 1) {
if (n === 3) {
break;
}
stopped = stopped + n;
}
console.log(skipped);
console.log(stopped);Check your prediction
12
3 Continue omits 3 but still allows 4 and 5: 1+2+4+5 is 12. Break omits 3 and every later value, leaving 1+2. The for update still runs after continue. In a while loop, a skipped manual update could prevent progress.
Try the rule on new inputs
Change the skip value to 1, then 5, then 9. Predict both totals each time. For a zero-iteration loop, explain why a starting total of zero remains correct. Before writing the goal exercise, decide whether the final total may exceed the target.
Watch one iteration at a time
A final answer can hide where a loop went wrong. During debugging, print the loop variable and the accumulator immediately after the update. Each row then answers two questions: which iteration just ran, and what has been accumulated so far? Remove the temporary trace when you no longer need it.
let savings = 0;
for (let day = 1; day <= 3; day = day + 1) {
savings = savings + 15;
console.log(day, savings);
}
console.log("finished", savings);Check your prediction
1 15
2 30
3 45
finished 45 The report after the loop runs once. The trace inside runs three times. Printing the final report inside would produce partial answers, which can be useful for tracing but misleading as the program’s finished output.
A bounded search may fail to reach its goal
Break can stop a loop early, but reaching the iteration limit does not prove success. Keep the final state and check whether the goal was met. For ten days at 15 units per day, the maximum possible saving is 150, so a goal of 200 is unreachable within this simulation.
const goal = 200;
let savings = 0;
let days = 0;
for (let day = 1; day <= 10; day = day + 1) {
savings += 15;
days += 1;
if (savings >= goal) {
break;
}
}
console.log(days, savings);
console.log(savings >= goal);Check your prediction
10 150
false Now change only the goal to 15, 16 and 150. Predict both days and savings. The exact-boundary case distinguishes >= from >, while the unreachable case checks that the loop remains bounded rather than running forever.
Save toward a goal
Save 15 units each day until savings reach at least 50. Print the number of days and final savings. Use a bounded loop and break when the goal is met.
let savings = 0;
let days = 0;
// Simulate at most 10 days.Hint 1: the idea
Update savings before checking whether the goal has been reached.
Hint 2: a first step
In a for loop, add 15, increment days, then compare savings with 50.
Show one solution
let savings = 0;
let days = 0;
for (let day = 1; day <= 10; day++) {
savings += 15;
days += 1;
if (savings >= 50) {
break;
}
}
console.log(days);
console.log(savings);4
60Try goals of 15, 16, and 150. For a goal above 150 the bounded simulation ends without reaching it; add a separate message for that case.
Repair a believable total
The next bug is a behavior bug: the code runs but forgets earlier matching values. A test with only one matching value would miss it. Use several matches and check the count separately from their sum.
Count and total the even values
For numbers 1 through 6, calculate how many are divisible by 2 and their total. Print 3 and 12. The starter resets total on every matching iteration; repair it without changing the range or hard-coding the answer.
let count = 0;
let total = 0;
for (let n = 1; n <= 6; n = n + 1) {
if (n % 2 === 0) {
total = 0;
total += n;
count += 1;
}
}
console.log(count);
console.log(total);Check your prediction
3
6 Hint 1: the idea
The counter remembers all three matches, but the total should also remember the earlier matching values. Compare where their initialization happens.
Hint 2: a first step
Keep the starting zero before the loop. Inside the condition, only add the current value and increment the count.
Show one solution
let count = 0;
let total = 0;
for (let n = 1; n <= 6; n = n + 1) {
if (n % 2 === 0) {
total += n;
count += 1;
}
}
console.log(count);
console.log(total);3
12Try a range containing no even values and a range containing just one. Both accumulators still start at zero, and the one-match case explains why the broken implementation could initially look plausible.
Recall
Where do initialization, update, and reporting belong? Why is the final saving allowed to exceed the goal? Write one boundary test before moving on.
Completion is saved in this browser only. It does not measure mastery.