One count per group
One morning, eighteen slips are in the basket. A slip is a small paper note for one child's plate. It names the child's class. The cook wants a count per class.
Loading lab…
- Six groups: 6A, 6B, 7A, 7B, 8A, 8B. Every count starts at 0.
- The slips are counted. 7A gets a row too — count 0.
- The six counts add back to 18 slips.
The rule
A tally is a list of counts, one per group. It is built one add at a time. A group with nothing in it still gets a row. That row says 0.
const groups = ["6A", "6B", "7A", "7B", "8A", "8B"];
const counts = [0, 0, 0, 0, 0, 0];
groups holds the six class names. counts holds one count for each class, all starting at 0.
One walk over the slips adds one to the right count. The counts end as 5, 3, 0, 4, 3, 3. In order, 6A has 5, 6B has 3 and 7A has 0. Then 7B has 4, 8A has 3 and 8B has 3. Check: 5 + 3 + 0 + 4 + 3 + 3 = 18.
7A was on a trip, so no slip says 7A. The program still lists 7A. So 7A keeps a row, and the row says 0.
The same slips group another way. The first character is the year: 6, 7 or 8. The year counts are 8, 4 and 6. Check: 5 + 3 = 8, 0 + 4 = 4, and 3 + 3 = 6.