Skip to content
Your progress

Checking your sign-in…

Who's learning?

Each profile keeps its own read lessons, badges, streak, and practice scores on this device.

Lesson 7 of 11

Counting the groups in the answer

Show on a big screen

8 min

Text size

A 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.
Step 1 of 4
  1. Three groups: 9-A, 9-B, 10-C.
  2. 9-A grows to 4 kept rows.
  3. 9-B grows to 4 kept rows.
  4. 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.

  1. const groups = ['9-A', '9-B', '10-C'];
  2. const tally = [0, 0, 0];
  3. for (const row of answer) {
  4. const at = groups.indexOf(row[sectionAt]);
  5. tally[at] = tally[at] + 1;
  6. }

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.