Asking the register one question
Show on a big screenThe 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.
- The condition: section is 9-A.
- Three rows keep; two are dropped.
- 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.
const at = headers.indexOf('due');const answer = [];for (const row of rows) {if (row[at] === 1450) {answer.push(row);}}
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.