Checkpoint: Attendance

The Course
•
4 min read

Work from a brief

Spend two minutes writing the contract, about eight implementing and testing, and five explaining a changed input. These are starting study budgets, not a speed requirement. Close the earlier worked answers before starting.

You receive an array of attendee records. Each needs a nonblank string email. Return a new array of trimmed, lowercase emails in first-seen order. Treat addresses differing only by surrounding spaces or case as duplicates for this exercise. Reject a malformed record rather than silently losing it. This simple normalization rule is not a general email-address validator.

You may use arrays, Set, string methods, and short-circuit guards from today. Preserve the input records. Write ordinary, duplicate, empty-list, and invalid-field cases. The warm-up below checks only membership; it is not the main implementation.

Predict and trace

Write the output before running. Follow the values through each call; if your prediction differs, identify the first step that surprised you.

Predict, then run JavaScript
const ids = new Set(["a", "a", "b"]);

console.log(ids.size);
Check your prediction
2

The Set removes duplicate strings, not malformed records.

Acceptance cases before you code

Write a small test plan before implementing attendance normalization. These cases extend the brief; they are not a second worked solution. Predict the expected behavior independently, then test through the public function. A good ordinary example and a good boundary example should catch different possible mistakes.

Case to tryWhat your result should demonstrate
Empty listProduce the documented empty result
Duplicate normalized emailDo not create a second accepted identity
Nontext required fieldReject before calling a string method

After the core task passes, deliberately break one rule and identify the case that notices it. Restore the implementation and explain the failure in terms of the contract, not the number of assertions. If a case requires a concept you have not recalled, revisit the relevant lesson before opening the solution.

Build it yourself

Exercise

Checkpoint: Attendance: practise

Implement attendance(records). Print ada@example.test, lin@example.test for the supplied duplicate case, then an empty count and a caught invalid-field message.

Your working space JavaScript
const records = [
  { email: " Ada@example.test " },
  { email: "ada@example.test" },
  { email: "lin@example.test" }
];
Hint 1: the idea

Validate each record before reading string methods, then normalize before checking uniqueness.

Hint 2: a first step

A Set preserves the order in which cleaned values were first added.

Show one solution
function attendance(records) {
  if (!Array.isArray(records)) {
    throw new Error("Expected a list");
  }

  const emails = new Set();

  for (const record of records) {
    if (
      record === null ||
      typeof record !== "object" ||
      Array.isArray(record) ||
      typeof record.email !== "string" ||
      !record.email.trim()
    ) {
      throw new Error("Invalid email");
    }

    emails.add(record.email.trim().toLowerCase());
  }

  return [...emails];
}

console.log(
  attendance([
    { email: " Ada@example.test " },
    { email: "ada@example.test" },
    { email: "lin@example.test" }
  ]).join(", ")
);
console.log(attendance([]).length);

try {
  attendance([{ email: 42 }]);
} catch (error) {
  console.log(error.message);
}
ada@example.test, lin@example.test
0
Invalid email

Test a blank field and null record. If the product instead needs duplicates reported, describe a new return contract before changing the code.

Test a changed requirement

Test a blank field and null record. If the product instead needs duplicates reported, describe a new return contract before changing the code.

Explain before moving on

Which normalization decision came from the brief rather than JavaScript? Did the original email text stay intact?

Was this article helpful?

Your response is saved on this device.