Skip to content

On Your Own: Reading Budget

The Course
•
25 min study

This is a fresh brief, not another copy of the finished app. Sketch the request and data boundaries first. The suggested time is a first attempt; finish the remaining acceptance cases in another session if needed.

The brief

Build a Reading Budget page. It receives sessions with a title, minutes and a done flag. A query filters titles without regard to case; the result contains the matching titles and the total minutes of unfinished matches.

  • Normalize whitespace around the query; a missing query means all sessions.
  • Do not mutate the sessions, and do not count completed sessions in minutes.
  • Expose the rule through a Next.js page whose query lives in searchParams.
  • Add a small form that submits the query, and make a direct filtered URL work.

The contract workspace checks the data rule. The route and form are your own integration work: test them in the preview and then in a downloaded project.

Acceptance cases

  • Input {"sessions": [{"title": "Read React", "minutes": 20, "done": false}, {"title": "React tests", "minutes": 15, "done": true}], "query": " react "} → {"titles": ["Read React", "React tests"], "minutes": 20}.
  • Input {"sessions": [], "query": ""} → {"titles": [], "minutes": 0}.
Your workspace Next.js · 5 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <strong>Study Library · Practice lab</strong> </header> {children} </body> </html> ); } import Cases from "./Cases.jsx"; export default function Page() { return ( <main> <h1>Independent workspace</h1> <p> Read the input and predict the result before running. Change lib/contract.js for the exercise. </p> <Cases /> </main> ); } import { run, examples } from "@/lib/contract.js"; const text = (value) => JSON.stringify(value) ?? "undefined"; export default async function Cases() { const results = await Promise.all( examples.map(async ([input, expected]) => { // Record the input before running, to show whether run changed it. const before = text(input); const actual = await run(input); return { input: before, expected, actual, kept: text(input) === before, pass: text(actual) === text(expected), }; }), ); return ( <section> <h2>Executable cases</h2> <p> Checks: {results.filter((row) => row.pass).length}/{results.length}{" "} passed </p> <table> <thead> <tr> <th>Input</th> <th>Expected</th> <th>Actual</th> <th>Input kept</th> </tr> </thead> <tbody> {results.map((row, i) => ( <tr key={i}> <td> <code>{row.input}</code> </td> <td> <code>{text(row.expected)}</code> </td> <td className={row.pass ? "pass" : "fail"}> <code>{text(row.actual)}</code> </td> <td className={row.kept ? "pass" : "fail"}> {row.kept ? "yes" : "no: run changed it"} </td> </tr> ))} </tbody> </table> </section> ); } body { font: 16px/1.5 system-ui; margin: 0; color: #182b3c; background: #f4f8fc; } header, main { max-width: 900px; margin: auto; padding: 20px; } header { border-bottom: 1px solid #cbd5e1; } a { color: #075985; } button, input, textarea, select { font: inherit; padding: 8px; margin: 4px; } button { cursor: pointer; } table { border-collapse: collapse; width: 100%; } th, td { padding: 10px; border: 1px solid #cbd5e1; text-align: left; vertical-align: top; } pre, code { white-space: pre-wrap; overflow-wrap: anywhere; } .pass { color: #166534; } .fail, [role="alert"] { color: #a01818; } section { margin: 16px 0; padding: 12px; background: white; border-radius: 8px; } nav { display: flex; gap: 16px; } export function run(input) { // Build the independent rule from the brief. return null; } export const examples = [ [ { sessions: [ { title: "Read React", minutes: 20, done: false }, { title: "React tests", minutes: 15, done: true }, ], query: " react ", }, { titles: ["Read React", "React tests"], minutes: 20 }, ], [ { sessions: [], query: "" }, { titles: [], minutes: 0 }, ], ];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Hint 1: the idea

Separate the data guarantee from its page or HTTP translation. The workspace lets you test the rule before connecting it to the framework.

Hint 2: a first step

Write down the validation order and the fields the result may expose. Use a new collection or draft for updates, and keep failure results explicit.

Show one data-rule solution
One data-rule solution Next.js · 5 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <strong>Study Library · Practice lab</strong> </header> {children} </body> </html> ); } import Cases from "./Cases.jsx"; export default function Page() { return ( <main> <h1>One independent solution</h1> <p> Read the input and predict the result before running. Change lib/contract.js for the exercise. </p> <Cases /> </main> ); } import { run, examples } from "@/lib/contract.js"; const text = (value) => JSON.stringify(value) ?? "undefined"; export default async function Cases() { const results = await Promise.all( examples.map(async ([input, expected]) => { // Record the input before running, to show whether run changed it. const before = text(input); const actual = await run(input); return { input: before, expected, actual, kept: text(input) === before, pass: text(actual) === text(expected), }; }), ); return ( <section> <h2>Executable cases</h2> <p> Checks: {results.filter((row) => row.pass).length}/{results.length}{" "} passed </p> <table> <thead> <tr> <th>Input</th> <th>Expected</th> <th>Actual</th> <th>Input kept</th> </tr> </thead> <tbody> {results.map((row, i) => ( <tr key={i}> <td> <code>{row.input}</code> </td> <td> <code>{text(row.expected)}</code> </td> <td className={row.pass ? "pass" : "fail"}> <code>{text(row.actual)}</code> </td> <td className={row.kept ? "pass" : "fail"}> {row.kept ? "yes" : "no: run changed it"} </td> </tr> ))} </tbody> </table> </section> ); } body { font: 16px/1.5 system-ui; margin: 0; color: #182b3c; background: #f4f8fc; } header, main { max-width: 900px; margin: auto; padding: 20px; } header { border-bottom: 1px solid #cbd5e1; } a { color: #075985; } button, input, textarea, select { font: inherit; padding: 8px; margin: 4px; } button { cursor: pointer; } table { border-collapse: collapse; width: 100%; } th, td { padding: 10px; border: 1px solid #cbd5e1; text-align: left; vertical-align: top; } pre, code { white-space: pre-wrap; overflow-wrap: anywhere; } .pass { color: #166534; } .fail, [role="alert"] { color: #a01818; } section { margin: 16px 0; padding: 12px; background: white; border-radius: 8px; } nav { display: flex; gap: 16px; } export function run({ sessions, query }) { const q = typeof query === "string" ? query.trim().toLowerCase() : ""; const matches = sessions.filter((row) => row.title.toLowerCase().includes(q)); return { titles: matches.map((row) => row.title), minutes: matches .filter((row) => !row.done) .reduce((sum, row) => sum + row.minutes, 0), }; } export const examples = [ [ { sessions: [ { title: "Read React", minutes: 20, done: false }, { title: "React tests", minutes: 15, done: true }, ], query: " react ", }, { titles: ["Read React", "React tests"], minutes: 20 }, ], [ { sessions: [], query: "" }, { titles: [], minutes: 0 }, ], ];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.

The supplied solution checks the data rule. Your framework integration still needs direct-route, form or HTTP checks: the correct model does not prove that the page or handler calls it correctly. Keep that distinction in your own test plan.

Continue independently

Add one rejection case not listed above. Explain why it matters and which assertion would fail if the rule were removed. Then return to the final review after a few days, before opening earlier lessons.

Was this article helpful?

Your response is saved on this device.