Building Up a Total While Repeating
Introduction
Our loops can repeat a message or use the counter in a calculation. Another common job is to keep a running total: start with zero, add an amount during each pass, and report the final value after the loop.
The variable that remembers the total must be allowed to change, so we will declare it with let before the loop. Inside the loop, we will reassign it using its current value plus the new amount. We will finish the path with a small sales report that chooses a rate, repeats a sale, and checks whether the day was great or quiet.
Start a Total Before the Loop
Suppose the cafe sells five cups at four dollars and fifty cents each. We begin with a total of zero, then add the price once for every cup:
The loop runs five times. Each time, JavaScript works out the right side, then stores that value back into total. After the loop ends, the final print shows the amount from all five cups:
We declare total before the loop because the loop needs the same variable on every pass, and we print it after the loop because that is when all five additions are complete. If we declared total inside the block, each pass would start a fresh variable and the earlier amount would not carry over.
This pattern is called accumulation: each pass contributes a new amount to a value that already contains everything from earlier passes. We can read total = total + pricePerCup in two steps. First JavaScript looks up the current total and adds the price; then it stores the result back under total. After the first pass it is 4.5, after the second it is 9, and after the fifth it is 22.5. The let declaration is necessary because the value changes as the program runs.
See the Running Total
We can also print the amount inside the loop to watch it grow. The final print after the loop is still useful because it gives us one clear summary:
Each pass updates total first, then prints the updated value. After all three passes, the program prints the final total once more:
If the print came before the reassignment inside the loop, it would show the old amount on each pass. The order of those two lines matters: add first, then report the running value.
The print inside the loop is optional; it helps us see the steps but is not needed to calculate the final amount. The final print is outside the loop, so it runs once. We can compare the last running line with the final line and expect the amounts to match. If they do not, we can check whether the total was updated before it was printed and whether the loop ran the expected number of times.
Choose a Rate Before the Loop
The same structure can use a rate selected by a condition. At happy hour, each cup costs one dollar less. We start rate at the regular price, then change it when the happy-hour flag is true:
Since the flag is true, the rate becomes 3.5:
We decide on the rate once, before the loop starts, because the example charges the same rate for every cup. Then each pass adds that selected amount to the running total. The flag could be changed to false, in which case the if block is skipped and rate remains 4.5.
The declaration let rate = pricePerCup gives rate an initial value before we consider the discount. If happy hour is on, the block reassigns it to the lower value. If not, nothing changes and the original value is ready to use. We choose a single rate before the loop because the flag and price do not change during this sale. Then the loop can focus on repeating the same addition rather than deciding the price again for every cup.
These examples use decimal numbers for prices because the chosen values produce clear results for our practice calculations. JavaScript can represent some decimal fractions imprecisely, so programs handling real payments commonly store amounts as integer cents or use a decimal-money representation. Our examples focus on the control flow and accumulation pattern, not payment accounting.
Complete the Day's Report
Now we can transfer the pattern to a bakery. Four muffins cost 3.5 dollars each, and the discount flag is false. The program selects the rate, adds it once per muffin, shows the running amount, and reports the finished sale. The final choice checks whether the completed total is above 15:
The false flag leaves the rate at 3.5. Four passes bring the total to 14, so the closing choice takes its else branch:
The rate is chosen before the loop, the running total changes inside it, and the summary and final decision happen after it. A total equal to fifteen would also take the else branch, because > 15 means strictly above fifteen. The practice will use the cafe inputs and the true discount branch, so we can apply the same structure in a different setting.
Predicting Different Inputs
When we change the inputs, the order of the work stays the same. The rate is selected first, the loop adds that rate once per item, the summary reports the final values, and the last choice evaluates the completed total. We can read a small program from top to bottom by identifying fixed values, following the one that changes, and noticing where each decision happens.
Conclusion and Next Steps
We have built a running total by declaring it before a loop and reassigning it on every pass. Printing inside the loop shows the amount as it grows; printing after the loop gives the final result. We then used an if to choose a rate before the loop and another if / else to describe the day afterward.
That completes Making Decisions and Repeating Work in JavaScript and the path. We can now make a program choose between outcomes, handle several cases, repeat work a fixed number of times, and accumulate values as the loop runs. Keep experimenting with the starting values and conditions to see how the program responds.
