# === MASTER SETTINGS (Edit only after "=" sign) === default-country = IN default-theme = light theme-accent = blue default-tab = recent default-view = list home-layout = engadget hero-posts = 5 section-posts = 5 list-show-desc = off card-show-desc = on list-thumb-pos = left list-style = separator card-bg = soft-blue main-topics = Official Letter, Librarian, BPSC TRE, BSEB STET, BSLET, CTET, Tools, Notes list-first-post = list mixed-spotlights = on spot-1-after = auto spot-2-after = auto spot-3-after = auto load-more-after-screens = 8 spot-recent = letter, quiz, tools spot-letter = quiz, tools spot-exam = letter, tools spot-tools = letter, quiz date-groups = on letter-label = Official Letter tools-label = Tools exam-search-label = MCQ quiz-set-size = 25 shuffle-quiz = on post-bg-style = whatsapp posts-per-page = 15 bottom-nav = off promo-banner = off promo-roll-sec = 6 adsense-pub-id = ca-pub-8724031598570565 adsense-ad-slot = 4982909571 quiz-ad-every = 7 quiz-ads = on webp-images = on img-alt-mode = label copy-protect = on copy-words = 40 copy-chars = 150 wa-popup = on wa-popup-delay = 6 wa-popup-title = WhatsApp चैनल से जुड़ें! wa-popup-text = बिहार शिक्षा विभाग पत्र, BPSC TRE, STET व Librarian MCQ Quiz सबसे पहले पाएं। wa-channel-url = https://whatsapp.com/channel/0029Vb9X5qzHFxOvWTftYk3e
# शिक्षा पत्र फ़िल्टर (पद/विभाग व संभाग) dept-list = सभी पद/विभाग=, प्राथमिक शिक्षा=Primary, माध्यमिक शिक्षा=Secondary, उच्च माध्यमिक (+2)=Sr Secondary, प्रधानाध्यापक/HM=Headmaster, पुस्तकालयाध्यक्ष=Librarian, विश्वविद्यालय=University div-list = सभी संभाग=, नियमावली/आदेश=नियमावली, स्थानांतरण/पदस्थापन=Transfer, वेतन/बकाया/भत्ता=वेतन, अवकाश तालिका=अवकाश, प्रशिक्षण/SCERT=प्रशिक्षण, छात्रवृत्ति/योजना=छात्रवृत्ति, परीक्षा/BSEB=परीक्षा # टूल्स फ़िल्टर (Format: tool-01 = टॉपिक > आइटम1=Label1, आइटम2=Label2) tool-01 = Salary Calculator > Pay Slip Generate=Pay Slip, Monthly Pre Receipt=Pre Receipt, Arrear Calculator=Arrear, DA / HRA Calculator=Salary tool-02 = Age Calculator > Retirement Age Calculator=Retirement, Age on Fixed Date=Age Calculator, Service Length Calculator=Service tool-03 = PDF Converter > Image to PDF=Image to PDF, Word to PDF=Word to PDF, Excel to PDF=Excel to PDF, Merge / Split PDF=PDF tool-04 = File & Photo Resize > File Size Compress=File Compress, Photo Size Compress=Photo Resize, Signature Resize=Signature tool-05 = Income Tax Calculator > New vs Old Regime=Income Tax, Tax Slab Calculator=Tax Slab, HRA Exemption=HRA tool-06 = Hindi Typing & Font > KrutiDev to Unicode=Unicode, Unicode to KrutiDev=KrutiDev, Voice to Hindi Text=Hindi Typing
# Format: exam-01 = Exam Name > Class Name > Subject1=Label1, Subject2=Label2 exam-01 = BPSC TRE > Class 1-5 > Common P-1=General, Hindi=Hindi, English=English, Maths=Mathematics, EVS=EVS, Bihar GK=Bihar GK exam-02 = BPSC TRE > Class 6-8 > Common P-1=General, Science=Science, Social Science=Social Science, Hindi=Hindi, English=English, Maths=Mathematics exam-03 = BSEB STET > Class 9-10 > Common P-1=General, Hindi=Hindi, English=English, Mathematics=Mathematics, Science=Science, Social Science=Social Science, Music=Music exam-04 = BSEB STET > Class 11-12 > Common P-1=General, Computer Science=ComputerSc, Library Science=Librarian, Physics=Physics, Chemistry=Chemistry, History=History, Political Science=PolScience, Geography=Geography, Economics=Economics, Hindi=Hindi, English=English exam-05 = Librarian > BSEB LET > Library Science=Librarian, Computer ICT=ComputerSc, General Knowledge=GK-GS exam-06 = Librarian > BPSC LRE > Library Science=Librarian, Computer ICT=ComputerSc, General Knowledge=GK-GS exam-07 = CTET / TET > Paper 1 (1-5) > Child Development=CDP, Hindi=Hindi, English=English, Maths=Mathematics, EVS=EVS exam-08 = UGC NET > Paper 1 & 2 > Paper 1 General=General, Library Science=Librarian, Computer Science=ComputerSc, Education=Education
Bihar Teacher Seniority Clarification 2026: बिहार शिक्षक अन्तर वरीयता निर्धारण पर शिक्षा विभाग का नया स्पष्टीकरण शैक्षणिक सत्र 2025-26 में कतिपय कारणों से शिक्षक प्रशिक्षण में भाग नहीं लेने वाले शिक्षकों के प्रशिक्षण एवं क्षतिपूर्ति अवकाश के संबंध में बिहार बोर्ड इंटर वार्षिक परीक्षा 2027: ऑनलाइन आवेदन पत्र एवं शुल्क भरने की तिथि जारी | BSEB Inter Exam 2027 Online Form Census of India 2027 Household Schedule Questions: MHA Gazette Notification S.O. 4505(E) Bihar STET 2026 Notification: Apply Online, Fee, Eligibility & Syllabus बिहार में विद्यार्थी सहयोग शिविर और पोर्टल का आयोजन पटना हाईकोर्ट का शिक्षकों के तबादले पर स्टेटस को का आदेश | Patna High Court Status Quo on Teacher Transfer Bihar Exam Reform Committee Formed: Bihar Mein Pariksha Sudhar Hetu Visheshagya Samiti Ka Gathan Library Chapter 1 समस्तीपुर के 19 बाढ़ प्रभावित विद्यालयों का संचालन स्थगित | Samastipur School Closed Due to Flood
BPSC TRE 4 0 BPSC TRE 4 BPSC TRE Teacher BPSC TRE-4 TRE 4 TRE 4 0 BPSC_TRE_4 Education Bihar Official Letter शिक्षा विभाग Free Test Series Begusarai Education-Department Link Tools Bihar-Education Education Department Bihar MCQ Technology Bihar Teacher Recruitment BPSC BSEB BSEB Notice BSEB Patna Bihar Board Bihar Government Bihar STET 2026 Bihar Shikshak Bharti Exam Notice Hindi Grammer LRE National-Sports-Day Prompt Sports-Notice Teacher-Transfer pdf books notes ई शिक्षाकोष शिक्षा शिक्षा विभाग बिहार जन शिकायत निवारण बिहार सरकार विद्यार्थी सहयोग 1 5 11 12 2013 2026 2027 9 10 APAAR ID Admission Advt 05 2025 Answer Key Challenge Automation Architecture BBOSE BBOSE Admit Card BBOSE Exam 2026 BBOSE Notice BPSC Advt 14 2026 BPSC Notification 14 2026 BPSC Notification 2026 BPSC Teacher Recruitment BPSC Teacher Recruitment 2026 BPSSC BPSSC Notice BSEB Helpline BSEB Inter Exam 2027 BSEB Inter Registration 2027 BSEB Matric Exam 2027 BSEB Order BSEB STET Notification BSEB Update BSRTC BSRTC Free BSSC Begusarai Education Department Begusarai Handball Trial Begusarai Kabaddi Trial 2026 Begusarai News Begusarai Taekwondo Trial Date Begusarai_Exam_2026 Bihar Board 2027 Bihar Board Exam 2027 Bihar Class 11 Registration Bihar Daroga Recruitment Bihar Education Bihar Education Department Bihar Education News Bihar Govt Orders Bihar News Bihar Open School Bihar Police Bihar School Holiday Bihar School Teacher Vacancy Bihar Secondary Education Bihar Teacher Arrears Bihar Teacher Eligibility Test Bihar Teacher News Bihar Teacher Vacancy Bihar Transport Bihar_Education Bihar_IAS_Transfer Blogger Automation CCL_Form CTET Census 2027 Census of India 2027 Class_11th Class_1_to_8_SOP Computer Science DEO Begusarai DEO Order DEO Patna Order DM_Review_Meeting DPO Establishment Khagaria DPO Secondary Education DPO स्थापना बेगूसराय Document Verification Dummy Registration Card Durable AI Checkpoint E-Shikshakosh East-Champaran Education Department Education Department Begusarai Exam Postponed Exam Reforms Exam Routine Exam_Routine Exams Free Bus Travel GAD_Bihar Gazette Notification Gemini API General Administration Department Google Apps Script Govt Orders Half_Yearly_Exam High Court High School Committee Home Department Bihar Horizontal Reservation Hostel Allocation Household Schedule IAS-Transfer IAS_Posting_Bihar IFA Tablet Data Impersonation JEE NEET Coaching Kendriya Vidyalaya Pattern Khagaria Education News LPG Cylinder Return Librarian Recruitment MDM Bihar MDM Bihar Directives Mid Day Meal Bihar Ministry of Home Affairs NPS Bihar NTA Public Notice National Testing Agency No FIFO System Notice Office of the Registrar General India Official Order PET Exam PR 195 2026 PRAN Generation PTM-Guidelines Patna Patna News Patna Shikshak Deputation Prabandh Samiti Primary-Schools Programming Language Quiz Competition Raksha Bandhan Recruitment Rules SGV144477G STET 2026 STET Exam Date STET Exam Form STET Notification STET Online Application STET Online Form Sangya Savitri High School School Closure School Inspection Report School Librarian School News School Time Table Script Locking Self Declaration Form Sports Youth Welfare Sports Department System Architecture Division Teacher Deputation Cancelled Teacher Eligibility Test Teacher Salary Payment Teacher_Leave Teacher_Salary Tech Directives Training Transfer Order Transfer Policy Transgender Reservation Trends UGC NET Answer Key UGC NET June 2026 Vanshidhar Brajwasi Vidhan Parishad Bihar bihar act 3 1992 bihar open quota selection bihar reservation rules chunav e Shikshakosh Portal eNPS general administration department merit candidate count अधिसूचना अवकाश आवेदन गृह मंत्रालय ज ल श क ष पद ध क र पटन जिला शिक्षा पदाधिकारी नियमावली प रत प्रतियोगिता ब ह र र ज य पथ पर वहन न गम ब ह र व ध न पर षद सच व लय ब ह र सरक र बिहार विद्यालय परीक्षा समिति बिहार शिक्षा बेगूसराय भारत सरकार म त र क ष ग मंडे मेगा क्विज मध्याह्न भोजन योजना मध्याह्न भोजन योजना निदेशालय, शिक्षा विभाग, बिहार माध्यमिक शिक्षा ल इब र र यन न य क त लंबित अवकाश विशिष्ट शिक्षक श क श क ष व भ ग ब ह र शिक्षा विभाग, बिहार सरकार सम क ष ब ठक सरक र सरकारी अनुदेश सरकारी आदेश सरकारी आदेश / दिशा-निर्देश सूचना / निर्देश स्थापना शाखा स्वतंत्रता दिवस 2026 स्वर्ण जयंती जिला केन्द्रीय पुस्तकालय, बेगूसराय
0 matches

mcq7

Create a Quiz App with Timer using HTML CSS & JavaScript

105

Hello readers, Today in this blog you’ll learn how to Create a Quiz Application with Timer using HTML CSS & JavaScript. Earlier I’ve shared a blog on how to Create a Responsive Personal Portfolio Website and now it’s time to create Quiz Web App using JavaScript.

In this program [Quiz App with Timer], there are three layers or boxes, and these boxes shown one by one on a particular button clicked. At first, on the webpage, there is shown a button labeled as “Start Quiz” and when you clicked on that button, then the info box appears with popup animation.

In this infobox, there are some rules of the quiz and two buttons labeled as “Exit” and “Continue”. When you clicked on the Exit button, the info box will be hidden but when you clicked on the Continue button, then the Quiz Box appears.
Create a Quiz App with Timer using HTML CSS & JavaScript
In the Quiz Box, there is a header with a title on the left side and a timer box on the right side. This timer starts decrement from 15 to 0 sec and there is also shown a timeline indicator that is sliding from the left to right side according to the timer. If the user selects an option between 15 to 0 sec, the timer will be stopped and all available options will be disabled.

If the user selected option is correct, the selected option color, background color changed to green and there is also shown the tick icon to inform the user that the selected answer is correct. If the user selects an option that is incorrect, the selected option color, background color changed to red and there is shown the cross icon to inform the user that the selected option is incorrect and the correct option will be automatically selected.


If the user doesn’t select an option between 15 to 0 sec, the timer will be stopped once it comes in 0 and the correct option of that question will be selected automatically. After that, there is the next button to show the next question, and there is a total of five questions on this Quiz.

In the end, the result box will be appeared and shown the user score and two buttons [Replay Quiz, Quit Quiz], if the user clicked on the replay quiz button, the quiz will again start with the number 1 question, and the score of the user will be 0 but if the user clicked on the quit quiz button, the current window will be reloaded and the quiz starts from the begin.

Video Tutorial of Create a Quiz App with Timer

 
In the video, you have seen the actual preview of the Quiz Application with Timer and I hope you’ve understood the codes behind creating this Quiz Box layout. In this video, I’ve only written the HTML & CSS codes, and in the second part of this video, I’ve completed the JavaScript codes of this program. If you haven’t watched part 2 of this video, click here to watch it now.

You can use this Quiz and also you can add more questions to this Quiz. In the source codes, I’ve written many comments on how you can add more questions and explained each JavaScript line of this Quiz. If you’re a beginner, you may have difficulties understanding the JavaScript codes but you can easily understand the HTML & CSS codes and I’ve tried to explain all JavaScript line with comments, hope you will understand.

To create this program (Quiz Application with Timer). First, you need to create four Files one HTML File, CSS File and the other two are JavaScript files. After creating these files just paste the following codes in your file.

First, create an HTML file with the name of index.html and paste the given codes in your HTML file. Remember, you’ve to create a file with .html extension.

<!DOCTYPE html>
<!-- Created By CodingNepal - www.codingnepalweb.com -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Awesome Quiz App | CodingNepal</title>
<link rel="stylesheet" href="style.css">
<!-- FontAweome CDN Link for Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"/>
</head>
<body>
<!-- start Quiz button -->
<div class="start_btn"><button>Start Quiz</button></div>
<!-- Info Box -->
<div class="info_box">
<div class="info-title"><span>Some Rules of this Quiz</span></div>
<div class="info-list">
<div class="info">1. You will have only <span>15 seconds</span> per each question.</div>
<div class="info">2. Once you select your answer, it can't be undone.</div>
<div class="info">3. You can't select any option once time goes off.</div>
<div class="info">4. You can't exit from the Quiz while you're playing.</div>
<div class="info">5. You'll get points on the basis of your correct answers.</div>
</div>
<div class="buttons">
<button class="quit">Exit Quiz</button>
<button class="restart">Continue</button>
</div>
</div>
<!-- Quiz Box -->
<div class="quiz_box">
<header>
<div class="title">Awesome Quiz Application</div>
<div class="timer">
<div class="time_left_txt">Time Left</div>
<div class="timer_sec">15</div>
</div>
<div class="time_line"></div>
</header>
<section>
<div class="que_text">
<!-- Here I've inserted question from JavaScript -->
</div>
<div class="option_list">
<!-- Here I've inserted options from JavaScript -->
</div>
</section>
<!-- footer of Quiz Box -->
<footer>
<div class="total_que">
<!-- Here I've inserted Question Count Number from JavaScript -->
</div>
<button class="next_btn">Next Que</button>
</footer>
</div>
<!-- Result Box -->
<div class="result_box">
<div class="icon">
<i class="fas fa-crown"></i>
</div>
<div class="complete_text">You've completed the Quiz!</div>
<div class="score_text">
<!-- Here I've inserted Score Result from JavaScript -->
</div>
<div class="buttons">
<button class="restart">Replay Quiz</button>
<button class="quit">Quit Quiz</button>
</div>
</div>
<!-- Inside this JavaScript file I've inserted Questions and Options only -->
<script src="js/questions.js"></script>
<!-- Inside this JavaScript file I've coded all Quiz Codes -->
<script src="js/script.js"></script>
</body>
</html>
</html>

Second, create a CSS file with the name of style.css and paste the given codes in your CSS file. Remember, you’ve to create a file with .css extension.

/* importing google fonts */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body{
background: #007bff;
}
::selection{
color: #fff;
background: #007bff;
}
.start_btn,
.info_box,
.quiz_box,
.result_box{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2),
0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.info_box.activeInfo,
.quiz_box.activeQuiz,
.result_box.activeResult{
opacity: 1;
z-index: 5;
pointer-events: auto;
transform: translate(-50%, -50%) scale(1);
}
.start_btn button{
font-size: 25px;
font-weight: 500;
color: #007bff;
padding: 15px 30px;
outline: none;
border: none;
border-radius: 5px;
background: #fff;
cursor: pointer;
}
.info_box{
width: 540px;
background: #fff;
border-radius: 5px;
transform: translate(-50%, -50%) scale(0.9);
opacity: 0;
pointer-events: none;
transition: all 0.3s ease;
}
.info_box .info-title{
height: 60px;
width: 100%;
border-bottom: 1px solid lightgrey;
display: flex;
align-items: center;
padding: 0 30px;
border-radius: 5px 5px 0 0;
font-size: 20px;
font-weight: 600;
}
.info_box .info-list{
padding: 15px 30px;
}
.info_box .info-list .info{
margin: 5px 0;
font-size: 17px;
}
.info_box .info-list .info span{
font-weight: 600;
color: #007bff;
}
.info_box .buttons{
height: 60px;
display: flex;
align-items: center;
justify-content: flex-end;
padding: 0 30px;
border-top: 1px solid lightgrey;
}
.info_box .buttons button{
margin: 0 5px;
height: 40px;
width: 100px;
font-size: 16px;
font-weight: 500;
cursor: pointer;
border: none;
outline: none;
border-radius: 5px;
border: 1px solid #007bff;
transition: all 0.3s ease;
}
.quiz_box{
width: 550px;
background: #fff;
border-radius: 5px;
transform: translate(-50%, -50%) scale(0.9);
opacity: 0;
pointer-events: none;
transition: all 0.3s ease;
}
.quiz_box header{
position: relative;
z-index: 2;
height: 70px;
padding: 0 30px;
background: #fff;
border-radius: 5px 5px 0 0;
display: flex;
align-items: center;
justify-content: space-between;
box-shadow: 0px 3px 5px 1px rgba(0,0,0,0.1);
}
.quiz_box header .title{
font-size: 20px;
font-weight: 600;
}
.quiz_box header .timer{
color: #004085;
background: #cce5ff;
border: 1px solid #b8daff;
height: 45px;
padding: 0 8px;
border-radius: 5px;
display: flex;
align-items: center;
justify-content: space-between;
width: 145px;
}
.quiz_box header .timer .time_left_txt{
font-weight: 400;
font-size: 17px;
user-select: none;
}
.quiz_box header .timer .timer_sec{
font-size: 18px;
font-weight: 500;
height: 30px;
width: 45px;
color: #fff;
border-radius: 5px;
line-height: 30px;
text-align: center;
background: #343a40;
border: 1px solid #343a40;
user-select: none;
}
.quiz_box header .time_line{
position: absolute;
bottom: 0px;
left: 0px;
height: 3px;
background: #007bff;
}
section{
padding: 25px 30px 20px 30px;
background: #fff;
}
section .que_text{
font-size: 25px;
font-weight: 600;
}
section .option_list{
padding: 20px 0px;
display: block;
}
section .option_list .option{
background: aliceblue;
border: 1px solid #84c5fe;
border-radius: 5px;
padding: 8px 15px;
font-size: 17px;
margin-bottom: 15px;
cursor: pointer;
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: space-between;
}
section .option_list .option:last-child{
margin-bottom: 0px;
}
section .option_list .option:hover{
color: #004085;
background: #cce5ff;
border: 1px solid #b8daff;
}
section .option_list .option.correct{
color: #155724;
background: #d4edda;
border: 1px solid #c3e6cb;
}
section .option_list .option.incorrect{
color: #721c24;
background: #f8d7da;
border: 1px solid #f5c6cb;
}
section .option_list .option.disabled{
pointer-events: none;
}
section .option_list .option .icon{
height: 26px;
width: 26px;
border: 2px solid transparent;
border-radius: 50%;
text-align: center;
font-size: 13px;
pointer-events: none;
transition: all 0.3s ease;
line-height: 24px;
}
.option_list .option .icon.tick{
color: #23903c;
border-color: #23903c;
background: #d4edda;
}
.option_list .option .icon.cross{
color: #a42834;
background: #f8d7da;
border-color: #a42834;
}
footer{
height: 60px;
padding: 0 30px;
display: flex;
align-items: center;
justify-content: space-between;
border-top: 1px solid lightgrey;
}
footer .total_que span{
display: flex;
user-select: none;
}
footer .total_que span p{
font-weight: 500;
padding: 0 5px;
}
footer .total_que span p:first-child{
padding-left: 0px;
}
footer button{
height: 40px;
padding: 0 13px;
font-size: 18px;
font-weight: 400;
cursor: pointer;
border: none;
outline: none;
color: #fff;
border-radius: 5px;
background: #007bff;
border: 1px solid #007bff;
line-height: 10px;
opacity: 0;
pointer-events: none;
transform: scale(0.95);
transition: all 0.3s ease;
}
footer button:hover{
background: #0263ca;
}
footer button.show{
opacity: 1;
pointer-events: auto;
transform: scale(1);
}
.result_box{
background: #fff;
border-radius: 5px;
display: flex;
padding: 25px 30px;
width: 450px;
align-items: center;
flex-direction: column;
justify-content: center;
transform: translate(-50%, -50%) scale(0.9);
opacity: 0;
pointer-events: none;
transition: all 0.3s ease;
}
.result_box .icon{
font-size: 100px;
color: #007bff;
margin-bottom: 10px;
}
.result_box .complete_text{
font-size: 20px;
font-weight: 500;
}
.result_box .score_text span{
display: flex;
margin: 10px 0;
font-size: 18px;
font-weight: 500;
}
.result_box .score_text span p{
padding: 0 4px;
font-weight: 600;
}
.result_box .buttons{
display: flex;
margin: 20px 0;
}
.result_box .buttons button{
margin: 0 10px;
height: 45px;
padding: 0 20px;
font-size: 18px;
font-weight: 500;
cursor: pointer;
border: none;
outline: none;
border-radius: 5px;
border: 1px solid #007bff;
transition: all 0.3s ease;
}
.buttons button.restart{
color: #fff;
background: #007bff;
}
.buttons button.restart:hover{
background: #0263ca;
}
.buttons button.quit{
color: #007bff;
background: #fff;
}
.buttons button.quit:hover{
color: #fff;
background: #007bff;
}

Last, create a JavaScript file with the name of questions.js and paste the given codes in your JavaScript file. Remember, you’ve to create a file with .js extension. In this file, we store all questions in an array.

// creating an array and passing the number, questions, options, and answers
let questions = [
{
numb: 1,
question: "What does HTML stand for?",
answer: "Hyper Text Markup Language",
options: [
"Hyper Text Preprocessor",
"Hyper Text Markup Language",
"Hyper Text Multiple Language",
"Hyper Tool Multi Language"
]
},
{
numb: 2,
question: "What does CSS stand for?",
answer: "Cascading Style Sheet",
options: [
"Common Style Sheet",
"Colorful Style Sheet",
"Computer Style Sheet",
"Cascading Style Sheet"
]
},
{
numb: 3,
question: "What does PHP stand for?",
answer: "Hypertext Preprocessor",
options: [
"Hypertext Preprocessor",
"Hypertext Programming",
"Hypertext Preprogramming",
"Hometext Preprocessor"
]
},
{
numb: 4,
question: "What does SQL stand for?",
answer: "Structured Query Language",
options: [
"Stylish Question Language",
"Stylesheet Query Language",
"Statement Question Language",
"Structured Query Language"
]
},
{
numb: 5,
question: "What does XML stand for?",
answer: "eXtensible Markup Language",
options: [
"eXtensible Markup Language",
"eXecutable Multiple Language",
"eXTra Multi-Program Language",
"eXamine Multiple Language"
]
},
// you can uncomment the below codes and make duplicate as more as you want to add question
// but remember you need to give the numb value serialize like 1,2,3,5,6,7,8,9.....
// {
// numb: 6,
// question: "Your Question is Here",
// answer: "Correct answer of the question is here",
// options: [
// "Option 1",
// "option 2",
// "option 3",
// "option 4"
// ]
// },
];

Last, create a JavaScript file with the name of script.js and paste the given codes in your JavaScript file. Remember, you’ve to create a file with .js extension.

//selecting all required elements
const start_btn = document.querySelector(".start_btn button");
const info_box = document.querySelector(".info_box");
const exit_btn = info_box.querySelector(".buttons .quit");
const continue_btn = info_box.querySelector(".buttons .restart");
const quiz_box = document.querySelector(".quiz_box");
const result_box = document.querySelector(".result_box");
const option_list = document.querySelector(".option_list");
const time_line = document.querySelector("header .time_line");
const timeText = document.querySelector(".timer .time_left_txt");
const timeCount = document.querySelector(".timer .timer_sec");
// if startQuiz button clicked
start_btn.onclick = ()=>{
info_box.classList.add("activeInfo"); //show info box
}
// if exitQuiz button clicked
exit_btn.onclick = ()=>{
info_box.classList.remove("activeInfo"); //hide info box
}
// if continueQuiz button clicked
continue_btn.onclick = ()=>{
info_box.classList.remove("activeInfo"); //hide info box
quiz_box.classList.add("activeQuiz"); //show quiz box
showQuetions(0); //calling showQestions function
queCounter(1); //passing 1 parameter to queCounter
startTimer(15); //calling startTimer function
startTimerLine(0); //calling startTimerLine function
}
let timeValue = 15;
let que_count = 0;
let que_numb = 1;
let userScore = 0;
let counter;
let counterLine;
let widthValue = 0;
const restart_quiz = result_box.querySelector(".buttons .restart");
const quit_quiz = result_box.querySelector(".buttons .quit");
// if restartQuiz button clicked
restart_quiz.onclick = ()=>{
quiz_box.classList.add("activeQuiz"); //show quiz box
result_box.classList.remove("activeResult"); //hide result box
timeValue = 15;
que_count = 0;
que_numb = 1;
userScore = 0;
widthValue = 0;
showQuetions(que_count); //calling showQestions function
queCounter(que_numb); //passing que_numb value to queCounter
clearInterval(counter); //clear counter
clearInterval(counterLine); //clear counterLine
startTimer(timeValue); //calling startTimer function
startTimerLine(widthValue); //calling startTimerLine function
timeText.textContent = "Time Left"; //change the text of timeText to Time Left
next_btn.classList.remove("show"); //hide the next button
}
// if quitQuiz button clicked
quit_quiz.onclick = ()=>{
window.location.reload(); //reload the current window
}
const next_btn = document.querySelector("footer .next_btn");
const bottom_ques_counter = document.querySelector("footer .total_que");
// if Next Que button clicked
next_btn.onclick = ()=>{
if(que_count < questions.length - 1){ //if question count is less than total question length
que_count++; //increment the que_count value
que_numb++; //increment the que_numb value
showQuetions(que_count); //calling showQestions function
queCounter(que_numb); //passing que_numb value to queCounter
clearInterval(counter); //clear counter
clearInterval(counterLine); //clear counterLine
startTimer(timeValue); //calling startTimer function
startTimerLine(widthValue); //calling startTimerLine function
timeText.textContent = "Time Left"; //change the timeText to Time Left
next_btn.classList.remove("show"); //hide the next button
}else{
clearInterval(counter); //clear counter
clearInterval(counterLine); //clear counterLine
showResult(); //calling showResult function
}
}
// getting questions and options from array
function showQuetions(index){
const que_text = document.querySelector(".que_text");
//creating a new span and div tag for question and option and passing the value using array index
let que_tag = '<span>'+ questions[index].numb + ". " + questions[index].question +'</span>';
let option_tag = '<div class="option"><span>'+ questions[index].options[0] +'</span></div>'
+ '<div class="option"><span>'+ questions[index].options[1] +'</span></div>'
+ '<div class="option"><span>'+ questions[index].options[2] +'</span></div>'
+ '<div class="option"><span>'+ questions[index].options[3] +'</span></div>';
que_text.innerHTML = que_tag; //adding new span tag inside que_tag
option_list.innerHTML = option_tag; //adding new div tag inside option_tag
const option = option_list.querySelectorAll(".option");
// set onclick attribute to all available options
for(i=0; i < option.length; i++){
option[i].setAttribute("onclick", "optionSelected(this)");
}
}
// creating the new div tags which for icons
let tickIconTag = '<div class="icon tick"><i class="fas fa-check"></i></div>';
let crossIconTag = '<div class="icon cross"><i class="fas fa-times"></i></div>';
//if user clicked on option
function optionSelected(answer){
clearInterval(counter); //clear counter
clearInterval(counterLine); //clear counterLine
let userAns = answer.textContent; //getting user selected option
let correcAns = questions[que_count].answer; //getting correct answer from array
const allOptions = option_list.children.length; //getting all option items
if(userAns == correcAns){ //if user selected option is equal to array's correct answer
userScore += 1; //upgrading score value with 1
answer.classList.add("correct"); //adding green color to correct selected option
answer.insertAdjacentHTML("beforeend", tickIconTag); //adding tick icon to correct selected option
console.log("Correct Answer");
console.log("Your correct answers = " + userScore);
}else{
answer.classList.add("incorrect"); //adding red color to correct selected option
answer.insertAdjacentHTML("beforeend", crossIconTag); //adding cross icon to correct selected option
console.log("Wrong Answer");
for(i=0; i < allOptions; i++){
if(option_list.children[i].textContent == correcAns){ //if there is an option which is matched to an array answer
option_list.children[i].setAttribute("class", "option correct"); //adding green color to matched option
option_list.children[i].insertAdjacentHTML("beforeend", tickIconTag); //adding tick icon to matched option
console.log("Auto selected correct answer.");
}
}
}
for(i=0; i < allOptions; i++){
option_list.children[i].classList.add("disabled"); //once user select an option then disabled all options
}
next_btn.classList.add("show"); //show the next button if user selected any option
}
function showResult(){
info_box.classList.remove("activeInfo"); //hide info box
quiz_box.classList.remove("activeQuiz"); //hide quiz box
result_box.classList.add("activeResult"); //show result box
const scoreText = result_box.querySelector(".score_text");
if (userScore > 3){ // if user scored more than 3
//creating a new span tag and passing the user score number and total question number
let scoreTag = '<span>and congrats! , You got <p>'+ userScore +'</p> out of <p>'+ questions.length +'</p></span>';
scoreText.innerHTML = scoreTag; //adding new span tag inside score_Text
}
else if(userScore > 1){ // if user scored more than 1
let scoreTag = '<span>and nice , You got <p>'+ userScore +'</p> out of <p>'+ questions.length +'</p></span>';
scoreText.innerHTML = scoreTag;
}
else{ // if user scored less than 1
let scoreTag = '<span>and sorry , You got only <p>'+ userScore +'</p> out of <p>'+ questions.length +'</p></span>';
scoreText.innerHTML = scoreTag;
}
}
function startTimer(time){
counter = setInterval(timer, 1000);
function timer(){
timeCount.textContent = time; //changing the value of timeCount with time value
time--; //decrement the time value
if(time < 9){ //if timer is less than 9
let addZero = timeCount.textContent;
timeCount.textContent = "0" + addZero; //add a 0 before time value
}
if(time < 0){ //if timer is less than 0
clearInterval(counter); //clear counter
timeText.textContent = "Time Off"; //change the time text to time off
const allOptions = option_list.children.length; //getting all option items
let correcAns = questions[que_count].answer; //getting correct answer from array
for(i=0; i < allOptions; i++){
if(option_list.children[i].textContent == correcAns){ //if there is an option which is matched to an array answer
option_list.children[i].setAttribute("class", "option correct"); //adding green color to matched option
option_list.children[i].insertAdjacentHTML("beforeend", tickIconTag); //adding tick icon to matched option
console.log("Time Off: Auto selected correct answer.");
}
}
for(i=0; i < allOptions; i++){
option_list.children[i].classList.add("disabled"); //once user select an option then disabled all options
}
next_btn.classList.add("show"); //show the next button if user selected any option
}
}
}
function startTimerLine(time){
counterLine = setInterval(timer, 29);
function timer(){
time += 1; //upgrading time value with 1
time_line.style.width = time + "px"; //increasing width of time_line with px by time value
if(time > 549){ //if time value is greater than 549
clearInterval(counterLine); //clear counterLine
}
}
}
function queCounter(index){
//creating a new span tag and passing the question number and total question
let totalQueCounTag = '<span><p>'+ index +'</p> of <p>'+ questions.length +'</p> Questions</span>';
bottom_ques_counter.innerHTML = totalQueCounTag; //adding new span tag inside bottom_ques_counter
}

That’s all, now you’ve successfully created a Create a Quiz App with Timer using HTML CSS & JavaScript. If your code doesn’t work or you’ve faced any error/problem then please download the source code files from the given download button. It’s free and a .zip file will be downloaded then you’ve to extract it.

← Prev Next →