lecture 10 of 150 completed

Loops

for...of runs one check per row or per case. A loop over zero rows runs zero checks, and the test still passes.

What you'll learn

  • How for...of visits every item
  • Why a loop over an empty list checks nothing
  • How to run one check for each row of a case table

A test checks that every row in the Shelf loans table has a due date. One day the table stops loading and shows no rows. The test passes.

const dueDates = [];

for (const dueDate of dueDates) {
  if (dueDate === "") {
    throw new Error("A row has no due date");
  }
}
console.log("all rows have a due date");
all rows have a due date

A loop runs the same block of code several times. for...of runs its block once for each item in an array. On each turn, dueDate holds the next item.

Three items mean three turns. Zero items mean zero turns. The block never runs, so the check inside it never runs. The code reaches the last line and reports success.

The message is false. The test checked no rows at all.

Check the count before the loop

The test knows its data. It created a reader with loans, so the table must have rows. Say that first.

const dueDates = [];

if (dueDates.length === 0) {
  throw new Error("The loans table has no rows");
}
for (const dueDate of dueDates) {
  if (dueDate === "") {
    throw new Error("A row has no due date");
  }
}
console.log("all rows have a due date");
Error: The loans table has no rows

This is a common way for a broken page to reach production. The list is empty because of a bug, every test that loops over the list passes, and nobody looks.

When you know the exact number of rows your test data creates, compare with that number. It is stronger than checking for "more than zero".

One check for every case

Loops are also how you run the same check with different data. Here is the fee table from the last lecture, and the helper it tests.

function lateFee(daysLate) {
  return daysLate * 0.5;
}

const feeCases = [
  { daysLate: 0, fee: 0 },
  { daysLate: 4, fee: 2 },
  { daysLate: 10, fee: 5 },
];

for (const { daysLate, fee } of feeCases) {
  const result = lateFee(daysLate) === fee ? "pass" : "fail";
  console.log(`${daysLate} days late: ${result}`);
}
0 days late: pass
4 days late: pass
10 days late: pass
  1. for (const { daysLate, fee } of feeCases) takes one case at a time and destructures it.
  2. lateFee(daysLate) === fee compares the real result with the expected one.
  3. condition ? a : b gives a when the condition is true and b when it is false.

To add a case, you add one line to the table. The checking code stays the same.

The loop with a counter

Older code and some tutorials use a loop with a counter. You need it when you want the position as well as the item.

const titles = ["Dune", "Emma"];

for (let i = 0; i <= titles.length; i++) {
  console.log(i + ": " + titles[i]);
}
0: Dune
1: Emma
2: undefined

This loop has the mistake from the arrays lecture. i <= titles.length allows i to reach 2, and there is no item at index 2. The condition should be i < titles.length. for...of cannot make this mistake, so use it when you do not need the position.

Remember this

for...of runs its block once per item, and not at all for an empty list. Check that the list has what you expect before you loop. Put your cases in an array and let one loop check them all.

Reference: Loops and iteration in the MDN JavaScript Guide.

now prove it

Reading is the setup. Solve this exercise in the editor. We run your code on each case and compare what it prints with the expected output.