Project: Modular Quiz App
Build a quiz from three focused modules: a question bank, a validated engine, and an accessible DOM interface.
By the end, you’ll be able to…
- Separate question data, quiz rules, and DOM rendering into modules.
- Implement an engine that tracks answers, score, and completion with clear errors.
- Render questions and feedback through DOM basics and event delegation.
- Validate the question bank and review the finished app with a practical checklist.
01Split the app by responsibility
The quiz app has three jobs: holding question data, enforcing the rules of a quiz, and drawing the interface. Put each job in its own file. quiz-data.js exports the question bank and knows nothing about the DOM. quiz-engine.js imports no browser APIs; it tracks the current question, answers, score, and completion, and throws a clear error when a call is invalid. app.js imports both and owns every querySelector. Build it in four steps: data, engine, render, then completion and review.
This boundary pays off when requirements change. You can add a timer to the engine, reuse it in a different interface, or replace the question source without rewriting the render code. The dependency direction is one-way: app.js imports the engine and data, but neither imports app.js. Keep shared types in small modules only when several files genuinely need them; do not build a framework for a three-file project.
02Build the engine
The engine accepts a question array and validates it in the constructor. Each question needs a string id, a prompt, at least two choices, and an answer index within range. Store the array, set the index and score to zero, and keep an answered flag so one question cannot be scored twice. Private fields keep callers from editing the index or score directly.
The public API stays small: current returns the current question or null, answer(choiceIndex) validates and scores a choice, and next advances after an answer. answer returns a result object with correct, correctIndex, and score so the interface can explain what happened. Every invalid call throws QuizError rather than returning a vague false. Completion is derived from the index, not a separate boolean that can drift.
03Render with DOM basics
app.js creates one engine, then render draws the current question. Set the prompt with textContent, clear the choices container, and build one button per choice. Store the choice index in a data attribute. When an answer is submitted, delegate from the container, call engine.answer, show feedback, disable the choices, and reveal the Next button.
Use a status region with aria-live='polite' for feedback, and give the prompt tabindex='-1' so focus can move there after Next. Buttons should be real buttons with type='button', and the score should update after every answer. If the engine throws a QuizError, show its message in the status region; rethrow anything else so a programming bug is not hidden.
04Finish and restart
When current is null, render a completion state with the final score and no choice buttons. A restart handler creates a fresh engine, clears feedback, and renders again. Because the engine holds all progress, restarting does not require clearing several DOM elements by hand. The data file stays untouched, so the same questions are ready for the new attempt.
05Review checklist
Review with a checklist: answer correctly and incorrectly, confirm the score changes only once per question, reach the last question, restart, and use the quiz with only the keyboard. Temporarily break one answer index to confirm the constructor throws. Read the feedback through a screen reader or the accessibility tree. Finally, remove console logs and confirm that invalid input produces a helpful message rather than a blank screen.
The ideas worth keeping.
- Split data, rules, and rendering into modules with a one-way dependency from the UI to the engine.
- Keep engine state private, validate every public call, and derive completion from the question index.
- Render from engine state, announce feedback in a status region, and review keyboard flow and bad data before shipping.
Make it yours.
Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.
Exercise 1: Restart the quiz
Add a button with id quiz-restart to the page. Write a function restartQuiz() that creates a new QuizEngine from the same questions, clears the feedback text, renders, and moves focus to the prompt. Then connect the button to the function.
Explore a worked solution
const restartButton = document.querySelector("#quiz-restart");function restartQuiz() { engine = new QuizEngine(questions); feedback.textContent = ""; render(); prompt.focus();}restartButton.addEventListener("click", restartQuiz);Replacing the engine resets index, score, answers, and the answered flag in one step. Rendering from that new state avoids manually clearing old feedback or choice buttons.
Exercise 2: Validate a question bank
Create quiz-validate.js and export validateQuestions(questions) from it, importing QuizError from quiz-engine.js. It must throw QuizError when the bank is empty or not an array, when an id is missing or duplicated, or when a question has fewer than two choices. Return true for a valid bank.
Explore a worked solution
import { QuizError } from "./quiz-engine.js";export function validateQuestions(questions) { if (!Array.isArray(questions) || questions.length === 0) { throw new QuizError("Question bank must be a non-empty array"); } const ids = new Set(); for (const question of questions) { if (!question || typeof question !== "object") { throw new QuizError("Each question must be an object"); } if (typeof question.id !== "string" || ids.has(question.id)) { throw new QuizError("Each question needs a unique string id"); } ids.add(question.id); if (!Array.isArray(question.choices) || question.choices.length < 2) { throw new QuizError("Question " + question.id + " needs at least two choices"); } } return true;}The Set tracks ids already seen, so duplicates fail with a message that names the rule. Running this before constructing the engine catches data mistakes with a clearer error than a failed render.
What stayed with you?
Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.
Exercise 1: Restart the quiz
Add a button with id quiz-restart to the page. Write a function restartQuiz() that creates a new QuizEngine from the same questions, clears the feedback text, renders, and moves focus to the prompt. Then connect the button to the function.
Keep exploring.
The official references behind this lesson, when you want a second angle or the full details.
Pause. Put the pieces together.
You’ve reached the end of Modern JavaScript & Modules. Before moving on, use this task to check what you can do independently.
Deliver a modular quiz app whose data, engine, and UI live in separate files and whose invalid input is handled without crashing.
Try it in your own environment and write down the result. This is a self-check; reading completion does not evaluate your code.
Review this moduleOne lesson closer.
Reading is a start. Trying it yourself makes the connection.