Build a JavaScript Quiz App
In this project, you will learn how to create an interactive Quiz App using JavaScript. The application will display questions, allow users to select answers, calculate scores and show the final result.
Project Introduction
A Quiz App presents a series of questions to the user. The user selects an answer and JavaScript checks whether the selected answer is correct.
What You Will Learn
- JavaScript arrays
- JavaScript objects
- DOM manipulation
- Click events
- Conditional statements
- Score calculation
- Dynamic HTML
Create the HTML Structure
First, create a container for the quiz.
<div class="quiz">
<h1 id="question">
Question
</h1>
<div id="options">
</div>
<button id="nextButton">
Next
</button>
</div>
Create Question Data
Store questions inside an array of objects.
const questions = [
{
question:
"What does HTML stand for?",
options: [
"Hyper Text Markup Language",
"High Text Machine Language",
"Home Tool Markup Language",
"Hyperlinks Text Mark Language"
],
answer: 0
}
];
The answer property stores the index of the correct option.
Create Answer Options
JavaScript can create buttons dynamically for every answer option.
questions[currentQuestion]
.options
.forEach(function(option, index){
const button =
document.createElement("button");
button.textContent =
option;
button.onclick =
function(){
checkAnswer(index);
};
optionsContainer
.appendChild(button);
});
Display the Question
Use the current question index to display the correct question.
questionElement.textContent =
questions[currentQuestion].question;
Current Question Variable
let currentQuestion = 0;
The first question starts at index 0.
Check the Answer
When the user selects an option, compare its index with the correct answer.
function checkAnswer(selected){
const correct =
questions[currentQuestion].answer;
if(selected === correct){
console.log("Correct!");
}
else{
console.log("Wrong!");
}
}
Create the Score System
Create a variable to store the user's score.
let score = 0;
Increase the score when the selected answer is correct.
if(selected === correct){
score++;
}
Move to the Next Question
After answering a question, increase the current question index.
currentQuestion++;
Then display the next question.
currentQuestion++; loadQuestion();
Show Quiz Progress
Users should know which question they are currently answering.
const progress =
currentQuestion + 1;
console.log(
"Question " +
progress +
" of " +
questions.length
);
For example:
Question 3 of 10
Display the Final Result
When all questions have been answered, show the user's final score.
const total =
questions.length;
resultElement.textContent =
"You scored " +
score +
" out of " +
total;
Calculate Percentage
const percentage =
(score / total) * 100;
Complete Project Flow
Complete Quiz Project
You have now learned the main concepts required to build an interactive JavaScript Quiz App.
Tutorial Completed! ðŸ§
Now open the complete working Quiz App and test your JavaScript knowledge.
Run Complete Project Learn JavaScript Arrays