Lesson 7.6 β Quiz Application Project
π Building a Complete Interactive Quiz System Using HTML, CSS & JavaScript
π― Learning Objectives
After completing this lesson, students will be able to:
β Build a Quiz Application
β Display Questions Dynamically
β Check Answers
β Calculate Score
β Use Arrays & Objects
β Use DOM Manipulation
β Build Real-World Educational Applications
π Introduction
Quiz systems are widely used in:
β Online Exams
β School Tests
β College Assessments
β Competitive Exam Platforms
β Learning Management Systems (LMS)
Examples:
Question:
What does HTML stand for?
A. Hyper Text Markup Language
B. High Text Machine Language
C. Hyper Transfer Markup Language
D. Home Tool Markup Language
[ Submit ]
Today we will build:
π§ Quiz Application Project
π Project Preview
----------------------------------
JavaScript Quiz
Question 1 of 3
What does HTML stand for?
( ) Option A
( ) Option B
( ) Option C
( ) Option D
[ Next Question ]
----------------------------------
After the last question:
Quiz Completed
Your Score: 3 / 3
π― Concepts Used
| Concept | Used |
|---|---|
| Arrays | β |
| Objects | β |
| Functions | β |
| Events | β |
| DOM Manipulation | β |
| Conditional Statements | β |
ποΈ Project Files
Create:
index.html
style.css
script.js
π― Step 1 β Create HTML Structure
index.html
<!DOCTYPE html>
<html>
<head>
<title>Quiz Application</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>JavaScript Quiz</h1>
<h2 id="question"></h2>
<div id="options"></div>
<button onclick="nextQuestion()">
Next Question
</button>
<h2 id="result"></h2>
</div>
<script src="script.js"></script>
</body>
</html>
π― Step 2 β Design the Interface
style.css
body{
font-family:Arial;
background:#f4f4f4;
display:flex;
justify-content:center;
align-items:center;
height:100vh;
}
.container{
background:white;
padding:30px;
width:600px;
border-radius:10px;
box-shadow:0 0 15px lightgray;
text-align:center;
}
button{
padding:10px 20px;
background:#0d6efd;
color:white;
border:none;
border-radius:5px;
cursor:pointer;
margin-top:20px;
}
button:hover{
background:#084298;
}
.option{
margin:10px;
padding:10px;
background:#f1f1f1;
border-radius:5px;
text-align:left;
}
π― Step 3 β Create Quiz Questions
script.js
let quiz =
[
{
question:
"What does HTML stand for?",
options:
[
"Hyper Text Markup Language",
"High Text Machine Language",
"Hyper Transfer Markup Language",
"Home Tool Markup Language"
],
answer:
"Hyper Text Markup Language"
},
{
question:
"Which language is used for styling websites?",
options:
[
"HTML",
"CSS",
"Python",
"Java"
],
answer:
"CSS"
},
{
question:
"Which keyword creates a function?",
options:
[
"method",
"function",
"define",
"create"
],
answer:
"function"
}
];
π Understanding Objects
Each question is stored as an object.
Example:
{
question:"What is HTML?",
options:[...],
answer:"Hyper Text Markup Language"
}
π― Create Variables
let currentQuestion = 0;
let score = 0;
π What Do They Store?
currentQuestion
Stores question number.
score
Stores correct answers.
π― Display Question Function
function loadQuestion()
{
}
Get Current Question:
let q =
quiz[currentQuestion];
Display Question:
document.getElementById("question").innerHTML =
q.question;
π― Display Options
let optionsHTML = "";
Loop Through Options
for(let i=0;i<q.options.length;i++)
{
}
Create Radio Button
optionsHTML +=
'<div class="option">' +
'<input type="radio"
name="answer"
value="' + q.options[i] + '">' +
q.options[i] +
'</div>';
Display Options
document.getElementById("options").innerHTML =
optionsHTML;
π Complete loadQuestion()
function loadQuestion()
{
let q = quiz[currentQuestion];
document.getElementById("question").innerHTML =
q.question;
let optionsHTML = "";
for(let i=0;i<q.options.length;i++)
{
optionsHTML +=
'<div class="option">' +
'<input type="radio"
name="answer"
value="' + q.options[i] + '">' +
q.options[i] +
'</div>';
}
document.getElementById("options").innerHTML =
optionsHTML;
}
π― Check Answer
Create Function:
function nextQuestion()
{
}
Get Selected Answer
let selected =
document.querySelector(
'input[name="answer"]:checked'
);
Check Selection
if(!selected)
{
alert("Please Select An Answer");
return;
}
π Compare Answer
if(
selected.value ==
quiz[currentQuestion].answer
)
{
score++;
}
Correct answer increases score.
π― Move To Next Question
currentQuestion++;
π Check Quiz Completion
if(currentQuestion < quiz.length)
{
loadQuestion();
}
else
{
showResult();
}
π― Display Final Result
function showResult()
{
document.getElementById("question").innerHTML =
"Quiz Completed";
document.getElementById("options").innerHTML = "";
document.getElementById("result").innerHTML =
"Your Score: " +
score +
" / " +
quiz.length;
}
π Complete nextQuestion()
function nextQuestion()
{
let selected =
document.querySelector(
'input[name="answer"]:checked'
);
if(!selected)
{
alert("Please Select An Answer");
return;
}
if(
selected.value ==
quiz[currentQuestion].answer
)
{
score++;
}
currentQuestion++;
if(currentQuestion < quiz.length)
{
loadQuestion();
}
else
{
showResult();
}
}
π― Start Quiz Automatically
At end of script:
loadQuestion();
π» Complete JavaScript Code
script.js
let quiz =
[
{
question:"What does HTML stand for?",
options:[
"Hyper Text Markup Language",
"High Text Machine Language",
"Hyper Transfer Markup Language",
"Home Tool Markup Language"
],
answer:"Hyper Text Markup Language"
},
{
question:"Which language is used for styling websites?",
options:[
"HTML",
"CSS",
"Python",
"Java"
],
answer:"CSS"
},
{
question:"Which keyword creates a function?",
options:[
"method",
"function",
"define",
"create"
],
answer:"function"
}
];
let currentQuestion = 0;
let score = 0;
function loadQuestion()
{
let q = quiz[currentQuestion];
document.getElementById("question").innerHTML =
q.question;
let optionsHTML = "";
for(let i=0;i<q.options.length;i++)
{
optionsHTML +=
'<div class="option">' +
'<input type="radio" name="answer" value="' +
q.options[i] +
'">' +
q.options[i] +
'</div>';
}
document.getElementById("options").innerHTML =
optionsHTML;
}
function nextQuestion()
{
let selected =
document.querySelector(
'input[name="answer"]:checked'
);
if(!selected)
{
alert("Please Select An Answer");
return;
}
if(
selected.value ==
quiz[currentQuestion].answer
)
{
score++;
}
currentQuestion++;
if(currentQuestion < quiz.length)
{
loadQuestion();
}
else
{
showResult();
}
}
function showResult()
{
document.getElementById("question").innerHTML =
"Quiz Completed";
document.getElementById("options").innerHTML = "";
document.getElementById("result").innerHTML =
"Your Score: " +
score +
" / " +
quiz.length;
}
loadQuestion();
π― Expected Output
Question 1:
What does HTML stand for?
User Selects Correct Answer
Question 2:
Which language is used for styling websites?
Question 3:
Which keyword creates a function?
Final Result:
Quiz Completed
Your Score: 3 / 3
π Project Improvements
Add:
β Timer
β Multiple Categories
β Random Questions
β Negative Marking
β Restart Quiz Button
β Quiz Certificate
π« Real World Applications
Quiz Applications are used in:
β Online Exams
β LMS Platforms
β School Assessments
β Coding Tests
β Employee Training
β Certification Platforms
β οΈ Common Beginner Mistakes
Mistake 1
Forgetting to call:
loadQuestion();
Quiz will not start.
Mistake 2
Wrong answer comparison.
Always compare:
selected.value
with
quiz[currentQuestion].answer
Mistake 3
Not validating radio selection.
Users may click Next without selecting an answer.
π Best Practices
β Keep questions inside arrays.
β Use objects for question storage.
β Validate user input.
β Keep UI simple.
β Display final score clearly.
π Project Challenge
Modify the project and add:
β 10 Questions
β Timer
β Restart Quiz Button
β Percentage Calculation
β Pass/Fail Status
π Practical Activity
Create:
Computer Fundamentals Quiz
Questions:
- Input Device
- Output Device
- CPU
- RAM
- Operating System
Display score at the end.
π Assignment
Assignment 7.6
- What is a Quiz Application?
- Why are Arrays used?
- What is an Object?
- How is score calculated?
- Create a Quiz Application.
- Add 5 new questions.
- Add a Restart Quiz button.
π§ Quiz
Q1. Which structure stores multiple questions?
A. Variable
B. Array
C. Loop
D. Event
β Answer: B
Q2. Each question is stored as?
A. Function
B. Variable
C. Object
D. Loop
β Answer: C
Q3. Which method selects checked radio button?
A. getElementById()
B. querySelector()
C. createElement()
D. innerHTML
β Answer: B
Q4. Which variable stores correct answers count?
A. currentQuestion
B. answer
C. score
D. result
β Answer: C
Q5. Quiz Application uses?
A. Arrays
B. Objects
C. DOM Manipulation
D. All of These
β Answer: D
π Lesson Summary
β Built a complete Quiz Application.
β Learned Arrays and Objects.
β Used DOM Manipulation.
β Calculated score dynamically.
β Created a real-world educational project.