ILILCode
HomeJavaScriptProjectsproject

Project: Quiz app

Score answers from a data structure, then render a question that reacts to clicks.

Project 21 of 23 in JavaScript30 min2 exercises

A quiz is a small state machine: questions, a current index, and a score. The data drives everything, so the same code works for three questions or three hundred.

The data shape
const questions = [
  {
    id: "q1",
    prompt: "Which keyword declares a constant?",
    options: ["let", "const", "var"],
    answer: 1,
  },
];

const answers = { q1: 1 }; // question id -> chosen option index
  1. 01Score — compare the chosen index with the correct one for every question.
  2. 02Render — show the prompt, one button per option, and feedback after a click.
  3. 03In the full app you would track currentIndex, disable buttons after answering, and show a result screen at the end.

Exercises

Checked in your browser · hints and solutions included

01

Exercise

Checkpoint 1 — score the answers

Write scoreAnswers(questions, answers) that returns how many questions were answered correctly. answers maps a question id to the chosen option index. Unanswered questions count as wrong.

exercise.js
console · run your code or check the answer
02

Exercise

Checkpoint 2 — render a question

Write renderQuestion(question) that replaces #app with:

  • <p id="prompt"> containing the prompt
  • one <button class="option"> per option, in order
  • <p id="feedback">, initially empty

When a button is clicked, set the feedback to Correct if its index matches question.answer, otherwise Not quite.

exercise.js
console · run your code or check the answer