-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
80 lines (64 loc) · 2.17 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
const que=document.querySelector(".question"); //h2
const options=document.querySelector(".options"); //options ke buttons
const nextButton=document.querySelector(".next");
// console.log(nextButton);
var quesIndex=0;
var score=0;
function displayQuestion(){
clearPrevOptions();
let questionData=QuestionCollection[quesIndex];
que.textContent=(quesIndex+1)+".) "+questionData.question;
nextButton.style.display="none";
questionData.answers.forEach((itr)=>{
//Creating a button and adding it to div class options
const btn=document.createElement("button");
btn.textContent=itr.text;
options.append(btn);
//Setting a data attribute to isolate correct option buy means of dataset
if(itr.correct)
btn.dataset.correct=itr.correct;
btn.addEventListener("click",(e)=>{
if(e.target.dataset.correct==="true"){
e.target.classList.add("correct");
score++;
}
else
e.target.classList.add("incorrect");
Array.from(options.children).forEach((button)=>{
if(button.dataset.correct==="true")
button.classList.add("correct");
// To make other buttons unclickable after getting the answer
button.disabled=true;
})
nextButton.style.display="block";
})
})
}
nextButton.addEventListener("click",(e)=>{
if(quesIndex<QuestionCollection.length){
quesIndex++;
if(quesIndex<QuestionCollection.length)
displayQuestion();
else{
clearPrevOptions();
que.innerHTML=`You have scored ${score} out of ${QuestionCollection.length}`;
nextButton.innerHTML="Play Again";
}
}
else{
start();
}
})
function start(){
quesIndex=0;
score=0;
nextButton.innerHTML="Next";
clearPrevOptions();
displayQuestion();
}
const clearPrevOptions=()=>{
while(options.firstChild){
options.removeChild(options.firstChild);
}
};
start();