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 4 of 11

Asking the register one question

Show on a big screen

8 min

Text size

The register answers one plain question at a time. Such a question is a query. Its test, such as "section is 9-A", is the condition. To walk the rows is to read them one by one.

Five rows, the register's own order.
Step 1 of 4
  1. Five rows, the register's own order.
  2. The condition: section is 9-A.
  3. Three rows keep; two are dropped.
  4. The answer: 3 of 5 kept.

The rule

A query walks the rows. It keeps rows where one field meets the condition. The answer is whatever the walk kept — including nothing.

Here at is a variable name, not the English word. It holds the position of the due column. The sign === means is equal to.

  1. const at = headers.indexOf('due');
  2. const answer = [];
  3. for (const row of rows) {
  4. if (row[at] === 1450) {
  5. answer.push(row);
  6. }
  7. }

Line 2 makes answer an empty list. Line 3 takes each row in turn. Line 4 tests its due. Line 5 adds the whole row to answer.

The condition here is a due of 1450 rupees. This walk keeps four whole records: A-103, A-106, A-108 and A-111. That is four records, not just four dues.

Ask for section 10-C, and the walk keeps nothing. No row passes the test, so the answer is an empty list. That answer is correct, not broken — no record carries 10-C yet.

Tap a question below. Watch which rows are kept.

Loading lab…

An empty answer is still an answer.

Try the lab to finish this lesson.