The interface should explain what is happening
A blank list can mean no search yet, work in progress, no matches, or a failed request. Those are different states. Represent them explicitly rather than inferring them from unrelated booleans. Our model uses status values idle, loading, ready, and error; an empty results array with ready means a successful empty search.
Derive the displayed message from state. Avoid a second variable containing an old message that must be updated in every branch. Render should be a projection of the current state, not another source of application truth.
In HTML, a paragraph with role=status can announce changed feedback without moving keyboard focus. aria-busy on the results region communicates ongoing work. Keep a submit button available if the design permits a newer query to replace an old one. Disable Cancel when no request is pending. Choose the behavior deliberately instead of disabling the entire page.
The console function below is pure and has no DOM dependency. In the browser, assign its returned string to textContent. The complete app additionally creates list rows from results; the same status rule applies. A failure message should offer a useful next action while retaining enough detail for the learner to diagnose the controlled scenario.
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.
function statusText(state) {
if (state.status === "loading") {
return "Searching…";
}
if (state.status === "error") {
return state.error;
}
if (state.status === "ready") {
return `${state.results.length} result(s)`;
}
return "Enter a topic";
}
console.log(statusText({ status: "idle" }));
console.log(statusText({ status: "loading" }));
console.log(statusText({ status: "ready", results: [] }));
console.log(statusText({ status: "error", error: "Try again" }));Check your prediction
Enter a topic
Searching…
0 result(s)
Try againEmpty results belong to ready, not error. A successful request can legitimately return no matches.
An empty success is different from no response
Model what happened rather than inferring it from the list length. An empty successful result, a request that has not started and a failed request can all have an empty list. A status field carries the missing distinction.
State transitions also decide whether earlier data remains visible while refreshing. That is a user-facing policy, not something an if statement can choose for you. Keep error text scoped to the state that owns it so a later success does not display an old failure.
function message(state) {
if (state.status === "idle") {
return "Start a search";
}
if (state.status === "loading") {
return "Loading";
}
if (state.status === "error") {
return state.message;
}
return state.items.length === 0 ? "No matches" : "Results ready";
}
console.log(message({ status: "idle", items: [] }));
console.log(message({ status: "ready", items: [] }));Check your prediction
Start a search
No matchesThe same items value supports two distinct messages because the status identifies what happened. An items.length-only renderer would collapse them.
const before = { status: "error", items: [], message: "Offline" };
const after = { ...before, status: "ready", items: ["Notes"], message: "" };
console.log(after.status);
console.log(after.message === "");
console.log(before.message);Check your prediction
ready
true
OfflineThe success transition clears error state in the new record without mutating the previous one. Rendering only the right branch helps, but clearing incompatible fields also keeps the model understandable.
Try the rule on new inputs
Write a table of idle, loading, ready-empty, ready-nonempty and error messages. Then decide whether a loading state should retain old data, and make the corresponding transition explicit.
Build it yourself
Render every status
Write render(view, state), which sets every piece of the page from the state alone. view holds stand-ins for three elements with the properties a real render assigns: the status paragraph's textContent, the results list's rows and ariaBusy, and the Cancel button's disabled. The rules: Cancel is enabled and the list is busy only while loading; rows show result titles only when ready; the message is Enter a topic., Searching…, No matches., N result(s), or the error followed by Try again.. The loop renders the five states into the same view, one after another, and should print:
idle | Enter a topic. | rows: - | cancel disabled: true | busy: false
loading | Searching… | rows: - | cancel disabled: false | busy: true
ready | No matches. | rows: - | cancel disabled: true | busy: false
ready | 2 result(s) | rows: Notes, The Web | cancel disabled: true | busy: false
error | Offline. Try again. | rows: - | cancel disabled: true | busy: false// Stand-ins for the page's elements: the same properties a real render sets.
const page = {
status: { textContent: "" },
results: { ariaBusy: "false", rows: [] },
cancel: { disabled: true }
};
function render(view, state) {
// Set view.status.textContent, view.results.rows,
// view.results.ariaBusy ("true"/"false") and view.cancel.disabled.
}
const states = [
{ status: "idle", results: [], error: "" },
{ status: "loading", results: [], error: "" },
{ status: "ready", results: [], error: "" },
{ status: "ready", results: [{ title: "Notes" }, { title: "The Web" }], error: "" },
{ status: "error", results: [], error: "Offline." }
];
for (const state of states) {
render(page, state);
const rows = page.results.rows.join(", ") || "-";
console.log(
`${state.status.padEnd(7)} | ${page.status.textContent} | rows: ${rows} | cancel disabled: ${page.cancel.disabled} | busy: ${page.results.ariaBusy}`
);
}Hint 1: the idea
Because one view is reused, render must set every property on every call. Any property it sets only in some branches will keep whatever an earlier state left there, which is the bug this exercise is built to expose.
Hint 2: a first step
Start with const loading = state.status === "loading"; and set the two properties that depend only on it, cancel.disabled and results.ariaBusy. Run it: the last two columns are now right for all five rows. Then add the rows, and the message last.
Show one solution
// Stand-ins for the page's elements: the same properties a real render sets.
const page = {
status: { textContent: "" },
results: { ariaBusy: "false", rows: [] },
cancel: { disabled: true }
};
function render(view, state) {
const loading = state.status === "loading";
view.cancel.disabled = !loading;
view.results.ariaBusy = String(loading);
view.results.rows = state.status === "ready" ? state.results.map(book => book.title) : [];
if (loading) {
view.status.textContent = "Searching…";
} else if (state.status === "error") {
view.status.textContent = `${state.error} Try again.`;
} else if (state.status === "ready") {
view.status.textContent = state.results.length === 0 ? "No matches." : `${state.results.length} result(s)`;
} else {
view.status.textContent = "Enter a topic.";
}
}
const states = [
{ status: "idle", results: [], error: "" },
{ status: "loading", results: [], error: "" },
{ status: "ready", results: [], error: "" },
{ status: "ready", results: [{ title: "Notes" }, { title: "The Web" }], error: "" },
{ status: "error", results: [], error: "Offline." }
];
for (const state of states) {
render(page, state);
const rows = page.results.rows.join(", ") || "-";
console.log(
`${state.status.padEnd(7)} | ${page.status.textContent} | rows: ${rows} | cancel disabled: ${page.cancel.disabled} | busy: ${page.results.ariaBusy}`
);
}idle | Enter a topic. | rows: - | cancel disabled: true | busy: false
loading | Searching… | rows: - | cancel disabled: false | busy: true
ready | No matches. | rows: - | cancel disabled: true | busy: false
ready | 2 result(s) | rows: Notes, The Web | cancel disabled: true | busy: false
error | Offline. Try again. | rows: - | cancel disabled: true | busy: falseThree properties are computed outside any if, so each render assigns them whatever the status. If rows were set only in the ready branch, the error row would still show "Notes, The Web" from the state before it: stale results next to an error message. The message chain distinguishes the two ready rows by result count, which is the "empty success is not no response" rule from above. In the real app the same function body writes to document.querySelector(...) elements instead of these stand-ins.
Explain before moving on
Why is an empty array insufficient to identify the UI state? When should rendering move keyboard focus?
Completion is saved in this browser only. It does not measure mastery.