Summing with an Accumulator
Two jobs a loop does over and over
Nearly every loop you will write does one of two jobs: it adds things up, or it counts how many things fit a rule. This set is about adding up. Counting comes next, and once you have seen both you will notice they are the same shape with a different line in the middle.That shape needs one variable that lives outside the loop and changes inside it. A variable used this way is called an accumulator.
The three positions
Adding the whole numbers from 1 to 100 by hand is a long afternoon. With a loop it is four lines, and every accumulator you will ever write uses the same three positions:• Before the loop: create the variable and set it to 0.
• Inside the loop: add the current value to it.
• After the loop: read it.
Get the positions right and the name does not matter at all. total, sum, running, bill: pick whichever one describes what you are adding.
Three lines that look alike
Only one of these actually accumulates.| The line | What it does to the running total |
|---|---|
total = total + i | keeps what is already there and adds i on top |
total += i | exactly the same thing, in fewer characters |
total = i | throws the running total away and stores i instead |
The third line is one character away from the first, and it is the most common accumulator bug there is. If the number you print at the end is always the last item you looked at, a missing total on the right-hand side is why.
The program below adds the numbers 5 through 9 and prints the accumulator on every pass. Run it, then make each change below and run it again. Press Reset before each change, so that each one starts from the original program.
• Add the line
sum = 0; inside the loop, as its first line.• Change
sum = sum + i; to sum = i;.• Change the header to
for (let i = 1; i <= 20; i++).let sum = 0;
for (let i = 5; i <= 9; i++) {
sum = sum + i;
console.log("i is " + i + " and sum is " + sum);
}
console.log("Final sum: " + sum);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Adding only some of the values
Often only certain values belong in the sum. Put an if inside the loop. The loop still visits everything, and the if decides which passes contribute anything.A function that hands the sum back
An accumulator is just as useful inside a function. Build the total in the loop, then return it.In the program below,
sumOver adds only the values above a threshold and returns the total. Run it, then make each change below and run it again. Press Reset before each change, so that each one starts from the original program.• Change
cutoff to 60.• Add another day of 75 minutes to the end of the array.
• Cut the line
return running; and paste it directly above the closing brace of the loop.function sumOver(list, threshold) {
let running = 0;
for (const value of list) {
if (value > threshold) {
running += value;
}
}
return running;
}
const minutes = [64, 23, 57, 12, 88];
const cutoff = 50;
const total = sumOver(minutes, cutoff);
console.log("Minutes on days over " + cutoff + ": " + total);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.