Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7eb4227

Browse files
Quiz App Code
1 parent09232a7 commit7eb4227

File tree

3 files changed

+375
-0
lines changed

3 files changed

+375
-0
lines changed

‎Quiz App/index.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<htmllang="en">
3+
<head>
4+
<metacharset="UTF-8">
5+
<metahttp-equiv="X-UA-Compatible"content="IE=edge">
6+
<metaname="viewport"content="width=device-width, initial-scale=1.0">
7+
<linkrel="stylesheet"href="style.css">
8+
<title>Quiz App using JavaScript</title>
9+
</head>
10+
<body>
11+
<divclass="alert">Alert</div>
12+
<divclass="btn startBtn">Start</div>
13+
<divclass="container">
14+
<h1>Let's play a Quiz Game</h1>
15+
<divclass="question"></div>
16+
<divclass="choices"></div>
17+
<buttonclass="btn nextBtn">Next</button>
18+
<divclass="scoreCard"></div>
19+
<divclass="timer"></div>
20+
</div>
21+
22+
<scriptsrc="script.js"></script>
23+
</body>
24+
</html>

‎Quiz App/script.js

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
constcontainer=document.querySelector('.container');
2+
constquestionBox=document.querySelector('.question');
3+
constchoicesBox=document.querySelector('.choices');
4+
constnextBtn=document.querySelector('.nextBtn');
5+
constscoreCard=document.querySelector('.scoreCard');
6+
constalert=document.querySelector('.alert');
7+
conststartBtn=document.querySelector('.startBtn');
8+
consttimer=document.querySelector('.timer');
9+
10+
11+
// Make an array of objects that stores question, choices of question and answer
12+
constquiz=[
13+
{
14+
question:"Q. Which of the following is not a CSS box model property?",
15+
choices:["margin","padding","border-radius","border-collapse"],
16+
answer:"border-collapse"
17+
},
18+
{
19+
question:"Q. Which of the following is not a valid way to declare a function in JavaScript?",
20+
choices:["function myFunction() {}"," let myFunction = function() {};","myFunction: function() {}","const myFunction = () => {};"],
21+
answer:"myFunction: function() {}"
22+
},
23+
{
24+
question:"Q. Which of the following is not a JavaScript data type?",
25+
choices:["string","boolean","object","float"],
26+
answer:"float"
27+
},
28+
{
29+
question:"Q. What is the purpose of the this keyword in JavaScript?",
30+
choices:["It refers to the current function.","It refers to the current object.","It refers to the parent object."," It is used for comments."],
31+
answer:"It refers to the current object."
32+
}
33+
];
34+
35+
// Making Variables
36+
letcurrentQuestionIndex=0;
37+
letscore=0;
38+
letquizOver=false;
39+
lettimeLeft=15;
40+
lettimerID=null;
41+
42+
// Arrow Function to Show Questions
43+
constshowQuestions=()=>{
44+
constquestionDetails=quiz[currentQuestionIndex];
45+
questionBox.textContent=questionDetails.question;
46+
47+
choicesBox.textContent="";
48+
for(leti=0;i<questionDetails.choices.length;i++){
49+
constcurrentChoice=questionDetails.choices[i];
50+
constchoiceDiv=document.createElement('div');
51+
choiceDiv.textContent=currentChoice;
52+
choiceDiv.classList.add('choice');
53+
choicesBox.appendChild(choiceDiv);
54+
55+
choiceDiv.addEventListener('click',()=>{
56+
if(choiceDiv.classList.contains('selected')){
57+
choiceDiv.classList.remove('selected');
58+
}
59+
else{
60+
choiceDiv.classList.add('selected');
61+
}
62+
});
63+
}
64+
65+
if(currentQuestionIndex<quiz.length){
66+
startTimer();
67+
}
68+
}
69+
70+
// Function to check answers
71+
constcheckAnswer=()=>{
72+
constselectedChoice=document.querySelector('.choice.selected');
73+
if(selectedChoice.textContent===quiz[currentQuestionIndex].answer){
74+
// alert("Correct Answer!");
75+
displayAlert("Correct Answer!");
76+
score++;
77+
}
78+
else{
79+
// alert("Wrong answer");
80+
displayAlert(`Wrong Answer!${quiz[currentQuestionIndex].answer} is the Correct Answer`);
81+
}
82+
timeLeft=15;
83+
currentQuestionIndex++;
84+
if(currentQuestionIndex<quiz.length){
85+
showQuestions();
86+
}
87+
else{
88+
stopTimer();
89+
showScore();
90+
}
91+
}
92+
93+
// Function to show score
94+
constshowScore=()=>{
95+
questionBox.textContent="";
96+
choicesBox.textContent="";
97+
scoreCard.textContent=`You Scored${score} out of${quiz.length}!`;
98+
displayAlert("You have completed this quiz!");
99+
nextBtn.textContent="Play Again";
100+
quizOver=true;
101+
timer.style.display="none";
102+
}
103+
104+
// Function to Show Alert
105+
constdisplayAlert=(msg)=>{
106+
alert.style.display="block";
107+
alert.textContent=msg;
108+
setTimeout(()=>{
109+
alert.style.display="none";
110+
},2000);
111+
}
112+
113+
// Function to Start Timer
114+
conststartTimer=()=>{
115+
clearInterval(timerID);// Check for any exist timers
116+
timer.textContent=timeLeft;
117+
118+
constcountDown=()=>{
119+
timeLeft--;
120+
timer.textContent=timeLeft;
121+
if(timeLeft===0){
122+
constconfirmUser=confirm("Time Up!!! Do you want to play the quiz again");
123+
if(confirmUser){
124+
timeLeft=15;
125+
startQuiz();
126+
}
127+
else{
128+
startBtn.style.display="block";
129+
container.style.display="none";
130+
return;
131+
}
132+
}
133+
}
134+
timerID=setInterval(countDown,1000);
135+
}
136+
137+
// Function to Stop Timer
138+
conststopTimer=()=>{
139+
clearInterval(timerID);
140+
}
141+
142+
// Function to shuffle question
143+
constshuffleQuestions=()=>{
144+
for(leti=quiz.length-1;i>0;i--){
145+
constj=Math.floor(Math.random()*(i+1));
146+
[quiz[i],quiz[j]]=[quiz[j],quiz[i]];
147+
}
148+
currentQuestionIndex=0;
149+
showQuestions();
150+
}
151+
152+
// Function to Start Quiz
153+
conststartQuiz=()=>{
154+
timeLeft=15;
155+
timer.style.display="flex";
156+
shuffleQuestions();
157+
}
158+
159+
// Adding Event Listener to Start Button
160+
startBtn.addEventListener('click',()=>{
161+
startBtn.style.display="none";
162+
container.style.display="block";
163+
startQuiz();
164+
});
165+
166+
nextBtn.addEventListener('click',()=>{
167+
constselectedChoice=document.querySelector('.choice.selected');
168+
if(!selectedChoice&&nextBtn.textContent==="Next"){
169+
// alert("Select your answer");
170+
displayAlert("Select your answer");
171+
return;
172+
}
173+
if(quizOver){
174+
nextBtn.textContent="Next";
175+
scoreCard.textContent="";
176+
currentQuestionIndex=0;
177+
quizOver=false;
178+
score=0;
179+
startQuiz();
180+
}
181+
else{
182+
checkAnswer();
183+
}
184+
});

‎Quiz App/style.css

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
* {
2+
margin:0;
3+
padding:0;
4+
box-sizing: border-box;
5+
font-family: Verdana, Geneva, Tahoma, sans-serif;
6+
}
7+
8+
body {
9+
width:100%;
10+
height:100vh;
11+
background:linear-gradient(to right bottom,#08203e,#557c93);
12+
color:#fff;
13+
display: grid;
14+
place-items: center;
15+
}
16+
17+
.container {
18+
width:80%;
19+
max-width:850px;
20+
padding:30px;
21+
text-align: center;
22+
display: none;
23+
}
24+
25+
.containerh1 {
26+
font-size:36px;
27+
margin-bottom:40px;
28+
text-decoration: underline;
29+
text-underline-offset:8px;
30+
}
31+
32+
.container .question {
33+
font-size:26px;
34+
margin-top:20px;
35+
}
36+
37+
.container .choices {
38+
margin-top:20px;
39+
}
40+
41+
.choice {
42+
font-size:20px;
43+
background-color:#eee;
44+
color:#000;
45+
margin:10px auto;
46+
padding:10px;
47+
border-radius:5px;
48+
text-align: left;
49+
width:60%;
50+
cursor: pointer;
51+
opacity:0;
52+
transform:translateY(50%);
53+
animation: fade-in0.5s ease forwards;
54+
}
55+
56+
.choice:hover{
57+
background-color:#ddd;
58+
}
59+
60+
.choice.selected{
61+
background-color:#007bff;
62+
color:#fff;
63+
}
64+
65+
.btn{
66+
width:60%;
67+
font-size:20px;
68+
font-weight:600;
69+
padding:12px20px;
70+
margin:20px auto0 auto;
71+
background-color:#006400;
72+
color:#fff;
73+
border: none;
74+
border-radius:5px;
75+
cursor: pointer;
76+
}
77+
78+
.btn:hover{
79+
background-color:#08780c;
80+
}
81+
82+
.scoreCard{
83+
font-size:24px;
84+
margin-top:20px;
85+
}
86+
87+
.alert{
88+
background-color:#5d9b63;
89+
border-radius:5px;
90+
width:100%;
91+
padding:12px;
92+
position: absolute;
93+
top:0;
94+
display: none;
95+
}
96+
97+
.startBtn{
98+
width:15%;
99+
padding-block:20px;
100+
text-align: center;
101+
}
102+
103+
.timer{
104+
position: absolute;
105+
top:20px;
106+
right:20px;
107+
width:72px;
108+
height:72px;
109+
border-radius:50%;
110+
background-color:#08203e;
111+
border:2px solid#f00;
112+
font-size:24px;
113+
font-weight:600;
114+
padding:12px;
115+
display: flex;
116+
justify-content: center;
117+
align-items: center;
118+
animation: pulse1s ease-in-out infinite;
119+
}
120+
121+
/* Adding Animation to Timer */
122+
@keyframes pulse{
123+
0%{
124+
transform:scale(1);
125+
}
126+
50%{
127+
transform:scale(1.2);
128+
}
129+
100%{
130+
transform:scale(1);
131+
}
132+
}
133+
134+
/* Adding Animation to choices */
135+
@keyframes fade-in{
136+
0%{
137+
opacity:0;
138+
transform:translateY(50%);
139+
}
140+
100%{
141+
opacity:1;
142+
transform:translateY(0);
143+
}
144+
}
145+
146+
@media screenand (max-width:900px){
147+
.container {
148+
width:100%;
149+
margin-top:50px;
150+
}
151+
.containerh1{
152+
font-size:24px;
153+
}
154+
.container .question{
155+
font-size:22px;
156+
}
157+
158+
.timer{
159+
width:50px;
160+
height:50px;
161+
font-size:20px;
162+
}
163+
164+
.startBtn{
165+
width:50%;
166+
}
167+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp