AI Hints
Home›JavaScript Tutorial›Quiz App and Timers
JavaScript · Lesson 103 of 138

Practice: A Quiz App

Questions and a score.

⏱ 2 min read▶ Video at 2:03:18🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

Let's build a quiz. It shows one question at a time with answer buttons, keeps score, and shows the result at the end. This time the JavaScript lives in its own file, app dot JS, which is how real projects are organised.

Practice: A Quiz App
From the video: Practice: A Quiz App
  • One question at a time
  • Keep score
  • Script in its own file

The quiz logic

The questions are an array of objects, with the right answer's index. Index and score are the state. Render shows the current question, with one button per option, or the final score when we run out. A single click listener checks the answer, updates the score, and moves to the next question.

app.jsjavascript
const questions = [
  { q: "2 + 2 = ?", options: ["3", "4", "5"], answer: 1 },
  { q: "Capital of France?", options: ["Paris", "Rome", "Oslo"], answer: 0 },
  { q: "typeof []", options: ["array", "list", "object"], answer: 2 },
];
let index = 0, score = 0;
function render() {
  if (index === questions.length)
    return (app.innerHTML = `<p class="big">Score: ${score} / 3</p>`);
  const { q, options } = questions[index];
  app.innerHTML = `<h1>${q}</h1>` +
    options.map((o, i) => `<button data-i="${i}">${o}</button>`).join(" ");
}
app.onclick = (e) => {
  if (!e.target.dataset.i) return;
  if (Number(e.target.dataset.i) === questions[index].answer) score++;
  index++; render();
};
render();

The page

The HTML is tiny. Just a div for the app, and a script tag that loads app dot JS, with defer. We answered all three questions, getting the first two right, and the last one wrong. So the score is two out of three.

index.htmlpython
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="style.css">
  <script src="app.js" defer></script>
</head>
<body>
  <div id="app"></div>
</body>
</html>
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#f7df1e;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} .big{font-size:64px;font-weight:800}
Also uses app.js
const questions = [
  { q: "2 + 2 = ?", options: ["3", "4", "5"], answer: 1 },
  { q: "Capital of France?", options: ["Paris", "Rome", "Oslo"], answer: 0 },
  { q: "typeof []", options: ["array", "list", "object"], answer: 2 },
];
let index = 0, score = 0;
function render() {
  if (index === questions.length)
    return (app.innerHTML = `<p class="big">Score: ${score} / 3</p>`);
  const { q, options } = questions[index];
  app.innerHTML = `<h1>${q}</h1>` +
    options.map((o, i) => `<button data-i="${i}">${o}</button>`).join(" ");
}
app.onclick = (e) => {
  if (!e.target.dataset.i) return;
  if (Number(e.target.dataset.i) === questions[index].answer) score++;
  index++; render();
};
render();
Also uses actions.json
[{"click": "button[data-i=\"1\"]"}, {"click": "button[data-i=\"0\"]"}, {"click": "button[data-i=\"0\"]"}]

Summary

The quiz keeps its state in variables, redraws with render, and loads its script from a separate file. Next, timers on the page, and a live clock.

Key takeaways
  • State: index, score
  • render() per question
  • <script src defer>
Next up: Timers and a Clock — pages that update themselves