Counting

Counting is the other loop pattern

A loop that adds values up answers the question how much. A loop that counts answers a different question: how many. The two loops are the same shape, and they differ by one line in the middle.
A counter needs the same three positions an accumulator does. Create it and set it to 0 before the loop. Change it inside the loop. Read it after the loop has finished.

Add one, not the value

That middle line is the whole difference. On a pass worth counting, an accumulator adds the value it is looking at, so a large value pushes the answer up further than a small one. A counter adds 1, so every match weighs exactly the same and the size of the values never enters into it.
The line inside the loopWhat you are left with
total = total + scorehow much: the values themselves, added together
hits = hits + 1how many: one for every pass that got this far
hits += 1the same as the line above, written shorter
hits = 1neither: it throws away what hits held and stores 1

Counting characters in a string

A string is a sequence of characters, so a for-of loop walks it one character at a time and the counter works exactly as it does over an array.

Counting factors

A factor of n is a number that divides n exactly, leaving no remainder. To count them, try every candidate from 1 up to n and count the ones where the remainder is 0.
Look hard at the condition d <= n. Every number divides itself exactly, so n is always one of its own factors, and the loop has to get as far as n itself to find it. Change that condition and you change which candidates are ever tried at all, so working out the run of candidates a loop like this actually reaches is the first thing to do before you trust the count it hands back.
The program below runs two counters and an accumulator in one loop over the pages read on six days. 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 goal to 50.
• Add a seventh day of 35 pages to the end of the array.
• Change goalDays = goalDays + 1; to goalDays = goalDays + p;.
const pages = [43, 15, 56, 22, 47, 6];
const goal = 40;
let days = 0;
let total = 0;
let goalDays = 0;
for (const p of pages) {
  days = days + 1;
  total = total + p;
  if (p >= goal) {
    goalDays = goalDays + 1;
  }
}
console.log("Average pages per day: " + total / days);
console.log("Days at the goal or above: " + goalDays);
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.

Counting inside a nested loop

When one loop sits inside another, the counter still lives outside both of them, because it has to survive every pass of both. It is the innermost body that decides how often 1 gets added, so read from the inside out when you are working out what a nested count comes to.

Which one do you want

Before you write the body, say the question out loud. How many of them are there is a counter. How much do they come to is an accumulator. Almost every loop bug at this stage is one of the two written where the other belonged.