-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
136 lines (121 loc) · 3.57 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const questions=[
{
question: "What is the capital of France?",
answers:[
{text: "Paris", correct: true},
{text: "London", correct: false},
{text: "Germany",correct:false},
{text: "Rome", correct: false}
]
},
{
question:"What is the largest animal in the world?",
answers:[
{text:"Panda",correct:false},
{text:"Blue whale",correct:true},
{text:"Elephant",correct:false},
{text:"Koala",correct:false},
]
},
{
question:"Electric bulb filament is made up of?",
answers:[
{text:"Carbon",correct:false},
{text:"Tungsten",correct:true},
{text:"Lead",correct:false},
{text:"Copper",correct:false},
]
},
{
question:"Which of the following is usedin pencil?",
answers:[
{text:"Graphite",correct:true},
{text:"Aluminium",correct:false},
{text:"Charcoal",correct:false},
{text:"Phosphorus",correct:false},
]
},
{
question:"What is the largest planet in our solar system?",
answers:[
{text:"Saturn",correct:false},
{text:"Jupiter",correct:true},
{text:"Mars",correct:false},
{text:"Earth",correct:false},
]
}
];
const questionelem=document.getElementById("ques");
const answerB=document.getElementById("answers");
const nextbutton=document.getElementById("next");
let questionindex=0;
let score=0;
function startquiz(){
questionindex=0;
score=0;
nextbutton.innerHTML="Next";
displayQuestion();
}
function displayQuestion(){
resetQuestion();
let current =questions[questionindex]
let qno=questionindex+1;
questionelem.innerHTML=qno+"."+current.question;
current.answers.forEach(answer => {
const button=document.createElement("button");
button.innerHTML=answer.text;
button.classList.add("btn1");
answerB.appendChild(button);
if (answer.correct){
button.dataset.correct=answer.correct;
}
button.addEventListener("click",answerSelected);
});
}
function resetQuestion(){
nextbutton.style.display="none";
while(answerB.firstChild)
answerB.removeChild(answerB.firstChild)
}
function answerSelected(a){
const selectedButton=a.target;
const correct=selectedButton.dataset.correct==="true";
if(correct){
selectedButton.classList.add("correct");
score++;
}
else{
selectedButton.classList.add("incorrect");
}
Array.from(answerB.children).forEach(button=>{
if(button.dataset.correct==="true"){
button.classList.add("correct");
}
button.disabled=true;
});
nextbutton.style.display="block";
}
function showscore(){
resetQuestion();
questionelem.innerHTML=`You scored ${score} out of ${questions.length}!`;
nextbutton.innerHTML="PLAY AGAIN";
nextbutton.style.display="block";
}
function handleNext(){
questionindex++;
if(questionindex<questions.length){
displayQuestion();
}
else{
showscore();
}
}
nextbutton.addEventListener("click",()=>{
if(questionindex<questions.length){
handleNext();
}
else{
startquiz();
}
});
startquiz();