Build a reusable label function for a study session. This time the result must be returned to the caller, so it could later be displayed on a page or included in a larger report. Printing inside the function would tie it to one output mechanism.
Plan the contract first: what is the record called, which properties are required, which one is optional, and what does the function return? Write the answers in comments before implementation.
Your function should work for any valid record of that shape, not just the example. A useful check is to create a second record with different values without editing the function. Use the compiler to catch mismatched fields, then use the output to check the formatting rule.
Keep this checkpoint to the features already introduced. You do not need classes, assertions, generics or a special framework. Reuse ordinary JavaScript template literals and the fallback operator from the previous lesson.
Predict, check, then run
Before running, write down the output and explain which information the checker knows. Click Check types first, then Check & run.
type Session = {
topic: string;
minutes: number;
};
function total(session: Session, count = 1): number {
return session.minutes * count;
}
console.log(total({ topic: "Records", minutes: 15 }, 2));Enable JavaScript to edit and type-check this example.
Check your prediction
30Acceptance cases before you code
Before you write the function, predict the line it should return for each call below.
| Case to try | What your result should demonstrate |
|---|---|
| Missing optional note | Use the documented fallback |
| Omit note and supply an explicit empty note | Absence uses No note; an empty string remains present under this contract |
| Wrong required field type | The public caller fails checking |
Try writing the fallback with || instead of ?? (session.note || "No note"): it still compiles, and only the explicit-empty-note case shows the change in behavior. The wrong-type case is the only one the checker reports for you.
Your turn
Checkpoint: Session Labels: practice
Create Session with topic:string, minutes:number and optional note:string. Write sessionLabel returning "Topic: N min — Note"; absent note uses "No note". Print Types: 25 min — No note and Loops: 10 min — Review.
// Define the contract, implement sessionLabel, and check two records.Enable JavaScript to edit and type-check this example.
Hint 1: the idea
The function should have one record parameter and return a string.
Hint 2: a first step
Compute the note fallback separately if the template literal becomes hard to read.
Show one solution
type Session = {
topic: string;
minutes: number;
note?: string;
};
function sessionLabel(session: Session): string {
const note = session.note ?? "No note";
return `${session.topic}: ${session.minutes} min — ${note}`;
}
console.log(sessionLabel({ topic: "Types", minutes: 25 }));
console.log(sessionLabel({ topic: "Loops", minutes: 10, note: "Review" }));Enable JavaScript to edit and type-check this example.
Types: 25 min — No note
Loops: 10 min — Reviewnote?: string lets the first caller omit the note, while topic and minutes stay required: leaving out minutes is a compile error at the call. session.note ?? "No note" is the only runtime decision, and it treats just a missing note as absent, so note: "" prints an empty note rather than "No note". Returning the string instead of printing it lets a caller put the label anywhere.
Change it and explain it
Test an empty note and zero minutes. Explain whether either should be rejected and where such a decision belongs. Then deliberately pass minutes as text and use the diagnostic to locate the broken contract. Redo the task tomorrow with book records instead of sessions.
Completion is saved in this browser only. It does not measure mastery.