Aral Code

Going Further Lesson 30 of 30

JavaScript Final Project — Build a Quiz Game

Put everything together and build a complete quiz game with questions stored in an array of objects, a score, a timer, feedback and a results screen.

Time to put it all together. In this final project, you'll build a complete quiz game about the Philippines. It uses almost everything from this course: variables, arrays of objects, functions, the DOM, events, timers and conditions.

The plan

Before coding, programmers break a project into small steps:

  1. Data: store the questions in an array of objects.
  2. State: keep track of the current question number, the score and the time left.
  3. Render: a function that shows the current question and its choices.
  4. Events: when a choice is clicked, check it, update the score, and move on.
  5. End: show the final score and a "Play again" button.

Step 1: the data

const questions = [
  { q: "What is the capital of the Philippines?", choices: ["Cebu", "Manila", "Davao", "Baguio"], answer: 1 },
  { q: "How many islands does the Philippines have (approximately)?", choices: ["641", "1,700", "7,641", "17,000"], answer: 2 },
  // ...more questions
];

Each question is an object. answer holds the index of the correct choice, so answer: 1 means "Manila".

Step 2: the state

let current = 0;   // which question we're on
let score = 0;

The complete game

Here's the finished game. Play it first, then read the code. Every part has a comment explaining what it does.

Example Pinoy Trivia Quiz Game
<style>
  body { font-family: Arial, sans-serif; background: #f3f6f4; margin: 0; padding: 14px; }
  .game { max-width: 460px; margin: 0 auto; background: white; border-radius: 16px; padding: 20px; box-shadow: 0 6px 20px rgba(0,0,0,.08); }
  .top { display: flex; justify-content: space-between; color: #5b6662; font-size: 14px; }
  .bar { height: 6px; background: #e3f1ec; border-radius: 9px; margin: 10px 0 16px; overflow: hidden; }
  .bar div { height: 100%; background: #0f5c4d; transition: width .3s; }
  h2 { font-size: 19px; margin: 0 0 14px; }
  .choice { display: block; width: 100%; text-align: left; padding: 12px 14px; margin: 8px 0; font-size: 16px; border: 2px solid #dfe5e2; border-radius: 12px; background: white; cursor: pointer; }
  .choice:hover:enabled { border-color: #0f5c4d; }
  .right { background: #dff3e6; border-color: #1d7a43; }
  .wrong { background: #fbe3e0; border-color: #b4372b; }
  .next, .again { margin-top: 12px; padding: 12px 18px; font-size: 16px; border: 0; border-radius: 12px; background: #0f5c4d; color: white; cursor: pointer; }
  .timer.low { color: #b4372b; font-weight: bold; }
</style>

<div class="game" id="game"></div>

<script>
  // ---------- 1. DATA ----------
  const questions = [
    { q: "What is the capital of the Philippines?", choices: ["Cebu", "Manila", "Davao", "Baguio"], answer: 1 },
    { q: "About how many islands does the Philippines have?", choices: ["641", "1,700", "7,641", "17,000"], answer: 2 },
    { q: "Who wrote Noli Me Tángere?", choices: ["Andres Bonifacio", "Jose Rizal", "Apolinario Mabini", "Emilio Jacinto"], answer: 1 },
    { q: "What is the national bird of the Philippines?", choices: ["Maya", "Philippine Eagle", "Kalaw", "Tarictic"], answer: 1 },
    { q: "When is Philippine Independence Day?", choices: ["June 12", "August 21", "December 30", "November 30"], answer: 0 },
  ];
  const SECONDS = 15;

  // ---------- 2. STATE ----------
  let current = 0;
  let score = 0;
  let timeLeft = SECONDS;
  let timerId = null;
  const game = document.getElementById("game");

  // ---------- 3. RENDER A QUESTION ----------
  function showQuestion() {
    const item = questions[current];
    const progress = (current / questions.length) * 100;
    game.innerHTML = `
      <div class="top"><span>Question ${current + 1} of ${questions.length}</span>
      <span class="timer" id="timer">⏳ ${SECONDS}s</span></div>
      <div class="bar"><div style="width:${progress}%"></div></div>
      <h2></h2>
      <div id="choices"></div>
      <p id="feedback"></p>`;
    game.querySelector("h2").textContent = item.q;

    const box = document.getElementById("choices");
    item.choices.forEach((text, i) => {
      const btn = document.createElement("button");
      btn.className = "choice";
      btn.textContent = text;
      btn.addEventListener("click", () => choose(i));
      box.append(btn);
    });
    startTimer();
  }

  // ---------- 4. TIMER ----------
  function startTimer() {
    clearInterval(timerId);
    timeLeft = SECONDS;
    timerId = setInterval(() => {
      timeLeft--;
      const t = document.getElementById("timer");
      t.textContent = `⏳ ${timeLeft}s`;
      t.classList.toggle("low", timeLeft <= 5);
      if (timeLeft === 0) choose(-1);   // time's up counts as wrong
    }, 1000);
  }

  // ---------- 5. CHECK AN ANSWER ----------
  function choose(i) {
    clearInterval(timerId);
    const item = questions[current];
    const buttons = document.querySelectorAll(".choice");
    buttons.forEach((b) => (b.disabled = true));
    buttons[item.answer].classList.add("right");

    const feedback = document.getElementById("feedback");
    if (i === item.answer) {
      score++;
      feedback.textContent = "✅ Tama!";
    } else {
      if (i >= 0) buttons[i].classList.add("wrong");
      feedback.textContent = i < 0 ? "⏰ Time's up!" : "❌ Mali.";
    }

    const next = document.createElement("button");
    next.className = "next";
    next.textContent = current < questions.length - 1 ? "Next →" : "See results";
    next.addEventListener("click", () => {
      current++;
      current < questions.length ? showQuestion() : showResults();
    });
    feedback.after(next);
  }

  // ---------- 6. RESULTS ----------
  function showResults() {
    const pct = Math.round((score / questions.length) * 100);
    const message = pct === 100 ? "Perfect! Ang galing mo! 🏆" : pct >= 60 ? "Nice job! 👏" : "Practice pa tayo! 💪";
    game.innerHTML = `<h2>Your score: ${score} / ${questions.length} (${pct}%)</h2><p>${message}</p>
      <button class="again">Play again</button>`;
    game.querySelector(".again").addEventListener("click", () => {
      current = 0;
      score = 0;
      showQuestion();
    });
  }

  showQuestion();
</script>

How it works

  • Data and state are separate from the display. The game logic only changes current and score; the render functions draw whatever the state says.
  • textContent for the question keeps it safe even if a question contains special characters.
  • The timer is always cleared before a new one starts, so two timers never run at once.
  • Buttons are disabled after answering, so a student can't click twice and score twice.
  • The correct answer is always highlighted, so students learn from mistakes.

Challenges: make it your own

  1. Add five more questions about your favorite subject.
  2. Shuffle the questions so the order changes each game. Hint: questions.sort(() => Math.random() - 0.5).
  3. Give bonus points for fast answers: add timeLeft to the score when correct.
  4. Show a list of the questions the player got wrong on the results screen.
  5. Save the high score in localStorage (test it as a real .html file, since the Try-it frame blocks storage).

What's next?

Congratulations, you've finished the JavaScript course! 🎉 You now know the core of the language. From here you can:

  • Learn React to build bigger web apps from components.
  • Learn jQuery to read and maintain older websites.
  • Try Python: many ideas (variables, loops, functions, lists) work the same way.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. In the game, what does answer: 1 mean?
2. Why are the choice buttons disabled after answering?
3. Why is clearInterval called before starting a new timer?
4. Which data structure stores the questions?

By Sir Reyel · Last updated 2026-12-04