Quiz App with HTML, CSS & JavaScript

10 DAYS 10 PROJECT CHALLENGE

Day #05

Project Overview

This project is a Quiz Application built using HTML, CSS, and JavaScript. It allows users to test their knowledge with multiple-choice questions, provides instant navigation through questions, and displays the final score at the end. The quiz app is styled to look clean and user-friendly, making it a great beginner-friendly JavaScript project to learn DOM manipulation, event handling, and conditional logic.

Key Features

  • Start Quiz Button – Users can begin the quiz when ready.
  • Multiple Choice Questions – Each question comes with several answer options.
  • Score Tracking – Tracks correct answers and displays the final score.
  • Next Question Navigation – Users can move to the next question after answering.
  • Play Again Option – Restart the quiz after completion without reloading the page.
  • Responsive Design – Works smoothly on desktop, tablet, and mobile.

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Quiz App</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="quiz-container">
    <h2 id="question">Welcome to the Quiz!</h2>
    <div id="answer-buttons"></div>
    <button id="start-btn">Start Quiz</button>
    <button id="next-btn">Next</button>
    <div id="result"></div>
  </div>

  <script src="script.js"></script>
</body>
</html>

CSS Code

body {
  font-family: Arial, sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #002252;
}

.quiz-container {
  width: 400px;
  background: #eea731;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
  text-align: center;
}

h2 {
  margin-bottom: 20px;
}

.btn {
  display: block;
  width: 100%;
  margin: 8px 0;
  padding: 10px;
  border: none;
  border-radius: 8px;
  background: #007BFF;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s;
}

.btn:hover {
  background: #0056b3;
}

#next-btn, #start-btn {
  display: none;
  margin-top: 10px;
  padding: 10px;
}

#start-btn {
  background: #17a2b8;
}

#next-btn {
  background: #28a745;
}

#result {
  margin-top: 20px;
  font-size: 18px;
  font-weight: bold;
}

Javascript Code

// Questions with answers
const questions = [
  {
    question: "What does HTML stand for?",
    answers: [
      { text: "Hyperlinks and Text Markup Language", correct: false },
      { text: "Home Tool Markup Language", correct: false },
      { text: "HyperText Markup Language", correct: true }
    ]
  },
  {
    question: "Which programming language runs in a web browser?",
    answers: [
      { text: "C++", correct: false },
      { text: "Python", correct: false },
      { text: "JavaScript", correct: true }
    ]
  },
  {
    question: "What year was JavaScript created?",
    answers: [
      { text: "1995", correct: true },
      { text: "2005", correct: false },
      { text: "2015", correct: false }
    ]
  }
];

// DOM elements
const questionElement = document.getElementById("question");
const answerButtons = document.getElementById("answer-buttons");
const startButton = document.getElementById("start-btn");
const nextButton = document.getElementById("next-btn");
const resultElement = document.getElementById("result");

let currentQuestionIndex = 0;
let score = 0;

// Start button click → begins quiz
startButton.addEventListener("click", startQuiz);

// Next button click → move to next question or show result
nextButton.addEventListener("click", () => {
  if (nextButton.innerText === "Play Again") {
    startQuiz(); // Restart quiz
    return;
  }

  currentQuestionIndex++;
  if (currentQuestionIndex < questions.length) {
    showQuestion();
  } else {
    showResult();
  }
});

// Start quiz: reset everything
function startQuiz() {
  startButton.style.display = "none"; // Hide start button
  currentQuestionIndex = 0;
  score = 0;
  resultElement.innerText = ""; // Clear old result
  nextButton.innerText = "Next"; // Reset button text
  showQuestion();
}

// Show current question
function showQuestion() {
  resetState();
  let currentQuestion = questions[currentQuestionIndex];
  questionElement.innerText = currentQuestion.question;

  // Create answer buttons dynamically
  currentQuestion.answers.forEach(answer => {
    const button = document.createElement("button");
    button.innerText = answer.text;
    button.classList.add("btn");
    button.addEventListener("click", () => selectAnswer(answer));
    answerButtons.appendChild(button);
  });
}

// Clear previous buttons
function resetState() {
  nextButton.style.display = "none";
  while (answerButtons.firstChild) {
    answerButtons.removeChild(answerButtons.firstChild);
  }
}

// Handle answer click
function selectAnswer(answer) {
  if (answer.correct) {
    score++;
  }
  nextButton.style.display = "block"; // Show next button
}

// Show quiz result
function showResult() {
  resetState();
  questionElement.innerText = "Quiz Completed!";
  resultElement.innerText = `You scored ${score} out of ${questions.length}`;
  nextButton.innerText = "Play Again"; // Change button text
  nextButton.style.display = "block";
}

// Show start button initially
startButton.style.display = "block";

Your Task

Create a similar project on your own and share your CodePen link in the comments.
I’ll review your work and share my feedback as a reply!

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Projects

Day 7 : Countdown Timer

A countdown timer for events (e.g., New Year countdown).

Concepts: Date object, time calculations.

Day 8 : BMI Calculator

Calculates Body Mass Index from height and weight inputs.

Concepts: Form handling, math operations.

Day 9 : Rock-Paper-Scissors Game

A fun game between the user and computer.

Concepts: Conditionals, random logic.