Quiz App with HTML, CSS & JavaScript
10 DAYS 10 PROJECT CHALLENGE
Day #05
Project Overview
Quiz App is a simple beginner-friendly project that uses HTML, CSS, and JavaScript to test the user’s knowledge with a set of multiple-choice questions. The application lets the users choose the answer among the given options, scores their performance, and displays the results. It also includes such details as a question’s current number and total number of questions, the score received for correct answers, and the final result.
This project is designed to help beginners practice important JavaScript concepts while building something interactive. It uses arrays, click events, dynamic content, variable score, and conditionals. It is the simplest task to get familiar with JavaScript basics and build the first application. It is also an excellent opportunity to observe how JavaScript controls HTML and CSS and builds applications.
Key Features
- Start Quiz Button: Users can click the Start Quiz button to begin the quiz. This resets the previous quiz data and displays the first question.
- Multiple-Choice Questions: Each question contains multiple possible answers, and users need to select them, indicating what they think is the right choice, thereby making the game more interactive and testing the user’s own knowledge by answering each question.
- Dynamic Answer Buttons: JavaScript dynamically creates the answer buttons based on the available answers for each question. Therefore, there is a possibility of having different answers for each question while taking a quiz.
- Score Tracking: The app calculates the number of correct answers and the user’s score, indicating how much progress has been made during the game.
- Next Question Navigation: After you have selected the answer, users can click the Next button to move to the following question and presenting questions one after another.
- Quiz Result: Once the user answers all the questions, the app displays a completion message along with the final quiz score.
- Play Again Option: After completing the quiz, users can start it again without refreshing the webpage. The app resets the previous quiz data and allows users to receive a new score.
- Clean and Simple UI: The application has a clean and simple user interface that can provide you with an easy experience while playing the quiz. The questions and buttons are clearly visible at all times.
- Responsive Design: The Quiz App is designed to work across different screen sizes, allowing users to access it comfortably on desktops, tablets, and mobile devices.
What You’ll Learn
- Understand how to store questions and answers using JavaScript arrays and objects.
- Learn how to select and manipulate HTML elements using JavaScript.
- Practice adding event listeners to buttons.
- Learn how to create HTML elements dynamically using JavaScript.
- Understand how to track and update a user’s score.
- Practice using conditional statements to check answers.
- Learn how to display different content based on the current question.
- Understand how to reset and update application state.
- Practice connecting HTML, CSS, and JavaScript to create an interactive application.
HTML Code
First, the HTML code below creates the basic structure for the Quiz App. First, the style.css file is linked inside the <head> section, so that CSS can control the appearance of the quiz. The main content is placed inside the <div> with the quiz-container class, which acts as the main container for the application.
Inside this container, the <h2> element with the id=”question” is used to display the current question, while the answer-buttons will provide space for the answer buttons created by JavaScript. The Start Quiz button is represented by the button with a class of start-btn, while the Next Quiz button is represented by the button with a class of next-btn. The result will display the final score, while the script.js file will be loaded at the bottom of the to enable JavaScript to manipulate the application.
<!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
For the styling, CSS code controls the appearance and layout of the Quiz App. Using Flexbox, we can center the .quiz-container on the body of the page both vertically and horizontally. The .quiz-container itself defines the box of the quiz, giving it a width, padding, border-radius, and a box-shadow.
The .btn class defines the style for the answer buttons. We can see that the buttons are given a width, margin, font-size, background color, and a hover effect. The Start and Next buttons also have unique styles to differentiate them from the rest of the buttons. Lastly, the #result selector defines how the final score is displayed once all questions have been answered.
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
Finally, JavaScript provides the main functionality of the Quiz App. First, the questions array stores the quiz questions along with their answer options and identifies which answer is correct. The code then selects the required HTML elements and uses variables to keep track of the current question and score.
The code also defines the startQuiz() function, which initializes the application and demonstrates the first question. Moreover, the showQuestion() method is responsible for the appearance of the questions and the creation of the buttons with answer options. Furthermore, when the user presses the button with the selected answer, the selectAnswer() method checks if the answer is correct and adds a point if it is correct. The Next button triggers the next question, while the showResult() method displays the final score. The Play Again option closes the result screen and restarts the game.
// 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!
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.
nice your project this project that help to me so thank you