A useful app is more than a happy-path demonstration. Our finished planner lets a person add a topic and duration, rejects invalid input without adding a record, marks sessions complete, and reports planned and completed minutes. It deliberately does not save data across reloads.
A form's submit event covers both clicking its submit button and pressing Enter in an input. event.preventDefault() prevents the default page navigation. An input's value is text, even for a numeric field. Validate it before putting it into your plan. HTML input constraints help, but JavaScript validation makes the application's rules explicit too.
The app below uses functions you have practiced, plus a few DOM tools. document.createElement creates an element. append adds it to a parent. replaceChildren clears the list before rebuilding it from state. Each row’s click callback remembers the session index from the render call that created it. When a person clicks later, that callback uses the remembered index to select the record. Keeping access to such surrounding local variables is called a closure. Each callback remembers its own row’s index. The list is never reordered here; larger applications should use stable record IDs.
Read the remaining browser tools
<ul>is an unordered list and<li>is one list item.<span>groups text within a row.sessions.forEach((session, index) => { ... })calls the callback once per record. The second parameter is its position, just as with map. Here we create visible rows rather than a returned array.document.createElement("li")creates a list item in memory.list.append(row)places it on the visible page. Creating an element alone does not display it.list.replaceChildren()removes the previous rows. render then builds the current rows from sessions so old rows do not accumulate.() => { ... }is an arrow callback with no parameters. A click handler can ignore the event object when it does not need it.form.reset()clears the form fields to their initial values. It does not erase sessions.topicInput.focus()moves keyboard focus to the topic field for the next entry.
In Day 6 we updated records by producing new arrays. The full app follows that convention: assign the revised array to sessions, then render it. A Complete button is a one-way action here; after completing its row, that button disappears. Adding an undo action would be a separate extension.
Predict before running
Before trying the complete webpage, check its remaining-time calculation. Why must incomplete sessions still count toward planned minutes?
const sessions = [
{ minutes: 25, done: true },
{ minutes: 20, done: false }
];
let remaining = 0;
for (const session of sessions) {
if (!session.done) {
remaining = remaining + session.minutes;
}
}
console.log(remaining);Check your prediction
20Trace each statement in order. If your prediction differed, describe the first place your mental model and the result diverged before trying again.
Verify the whole user journey
Read the app in this order: initial state, validation, state update, render, then event wiring. Trace one successful submission through those stages. Trace a rejected submission separately and confirm it does not add a record. Test empty input, 1 and 240 minutes, 0 and 241, a decimal, and a topic containing angle brackets. Complete one session and confirm that its button disappears while the other incomplete sessions keep their buttons. This version does not offer an undo action.
const sessions = [];
function add(topic, minutes) {
const cleaned = topic.trim();
if (
cleaned === "" ||
cleaned.length > 40 ||
!Number.isInteger(minutes) ||
minutes < 1 ||
minutes > 240
) {
return false;
}
sessions.push({ topic: cleaned, minutes });
return true;
}
console.log(add(" ", 20));
console.log(sessions.length);
console.log(add("Loops", 20));
console.log(sessions.length);Check your prediction
false
0
true
1This smaller model applies the app’s topic and duration rules without any HTML. It mutates its local list with push; the full app instead assigns a new array. Both must reject invalid input before changing the list.
Your turn
Step 1: count pending sessions
The capstone builds one new feature in three steps: count the pending sessions, show the count on the page, then add a Clear plan button. Step 1 needs no webpage. Write countPending(sessions). It should return 1 for the example below and 0 for an empty plan. This is a new summary you can add to the webpage.
const sessions = [{ done: true }, { done: false }];
// Write countPending and print its result.Hint 1: the idea
Count records, not minutes.
Hint 2: a first step
Initialize a count to zero and add one only for !session.done.
Show one solution
function countPending(sessions) {
let count = 0;
for (const session of sessions) {
if (!session.done) {
count = count + 1;
}
}
return count;
}
console.log(countPending([{ done: true }, { done: false }]));
console.log(countPending([]));1
0Counting records means adding 1, not session.minutes; that is the difference from the remaining-minutes loop above. Keeping the function free of HTML means you can test it here and then paste it into the page unchanged in step 2.
What you should be able to explain
- Can you trace submission without skipping a function?
- Does invalid input leave state intact?
- Can you explain the app after closing its source?
Try the webpage
Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.
A manual acceptance check
- Start with an empty plan: all totals should be zero.
- Add Loops for 25 minutes and Arrays for 20: planned is 45, completed is 0.
- Complete Loops: completed is 25, remaining is 20.
- Try an empty topic, zero, 1.5, and 241 minutes: each must show feedback without adding a row.
- Try 1 and 240: both boundaries are accepted.
- Enter a topic containing
<b>hello</b>: it must display as text, not bold HTML. - Submit with Enter and operate the buttons using a keyboard.
The conditional expression condition ? valueIfTrue : valueIfFalse selects the done label, using the same syntax introduced with the planner core on Day 6. To trace a row, follow its creation, its label, its button callback, and its append to the list.
Step 2: show the pending count
Add a line under the summary that reads Pending sessions: N and stays correct after every change. Use your countPending function from step 1. Check this sequence:
| Action | Pending line |
|---|---|
| Page loads | Pending sessions: 0 |
| Add Loops 25 and Arrays 20 | Pending sessions: 2 |
| Try to add a topic with 0 minutes | still Pending sessions: 2 |
| Complete Loops | Pending sessions: 1 |
Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.
Hint 1: the idea
Every change to sessions already ends with a call to render, and render already writes the summary. If the pending line is written in the same place, it can never fall out of date.
Hint 2: a first step
Add <p id="pending"></p> right after the summary paragraph, then find it in the script beside the other querySelector lines and save it as pending. Paste countPending above render. Running the page shows no change yet: the last piece is one line inside render.
Show one solution
Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.
The new line goes at the end of render, after the summary: pending.textContent = ...countPending(sessions). Because the submit handler and every Complete button call render, the count updates on all three changes without touching them. A rejected submission returns before render runs, so the count stays at 2. If you had instead updated the count inside the submit handler only, completing a session would leave it stale.
Step 3: a Clear plan button
Start from your step 2 page (or the step 2 model answer). Add a Clear plan button below the list. Clicking it removes every session, shows the feedback Plan cleared., and resets every total. Check: add two sessions, complete one, click Clear plan. The page should show no rows and:
Plan cleared.
Planned: 0; completed: 0; remaining: 0 minutes
Pending sessions: 0Then add a new session to check the planner still works after clearing.
Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.
Hint 1: the idea
In this app the page is always rebuilt from sessions. So clearing is not about deleting rows from the page: change the state, then let render rebuild everything from it.
Hint 2: a first step
Add <button id="clear" type="button">Clear plan</button> after the <ul>. It is outside the form, but type="button" still makes its purpose clear. Then register a click listener on it next to the form's submit listener, and decide what single assignment the listener must make before calling render.
Show one solution
Press Run webpage to preview. Each run starts fresh. Edits are not saved automatically.
The listener assigns sessions = [], sets the feedback, and calls render. render empties the list with replaceChildren, finds no records to draw, and writes zeros to both the summary and the pending line, so there is no separate "reset the totals" code to forget. Removing li elements one by one would make the page look empty while sessions still held both records, and the next added session would bring them all back.
This version clears without asking. The preview blocks pop-up dialogs, so confirm() cannot be used here; in your downloaded copy, decide whether a confirmation or an "Undo" message suits a two-click mistake better, and explain your choice.
Take it with you
Use Download HTML, then open the downloaded file in a browser. It needs no server or installation. Your edited source is downloaded; the current preview's in-memory sessions are not. No information survives a reload unless you add storage. Do not put private information into practice examples.
Where next
Keep building a small app you actually want. Try the independent brief next before deciding you need more syntax. Then choose intermediate JavaScript for modules and asynchronous applications, or beginner TypeScript to add static checking to the JavaScript you already know. Learning HTML and CSS more deeply helps with either path.
Completion is saved in this browser only. It does not measure mastery.