Counting the groups in the answer
Show on a big screenA report needs two steps, not one. First ask the query. Then count the kept rows by group. A group here is a section, such as 9-A. A tally is one count for each group.
- Three groups: 9-A, 9-B, 10-C.
- 9-A grows to 4 kept rows.
- 9-B grows to 4 kept rows.
- 10-C stays 0 — a real, honest count.
The rule
Count the answer, not the table. This report lists its groups first. So a group with no answer rows still shows a 0.
Here sectionAt is the position of the section column in a row. at is a variable name, not the English word. It holds the position of the row's section in groups.
const groups = ['9-A', '9-B', '10-C'];const tally = [0, 0, 0];for (const row of answer) {const at = groups.indexOf(row[sectionAt]);tally[at] = tally[at] + 1;}
Line 2 starts every count at 0. Line 5 adds 1 to the count of the row's group.
The query "due at least 1000" keeps 8 of the 12 records. Counted per section, that gives 9-A four, 9-B four, and 10-C zero.
A list built from kept rows alone would leave out 10-C.
The lab has four cards: open, ask, count, show. Try them in different orders.
Loading lab…
Ask what list a report counted, before you trust its numbers.
Try the lab to finish this lesson.