-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmathsquiz.js
165 lines (150 loc) · 4.75 KB
/
mathsquiz.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const questions=[
{
question: "Find the greatest number that will divide 43, 91 and 183 so as to leave the same remainder in each case.",
answers: [
{text:'4',correct:true},
{text:'7',correct:false},
{text:'9',correct:false},
{text:'13',correct:false}
]
},
{
question:"The H.C.F. of two numbers is 23 and the other two factors of their L.C.M. are 13 and 14. The larger of the two numbers is:",
answers: [
{text:'276',correct:false},
{text:'299',correct:false},
{text:'322',correct:true},
{text:'345',correct:false}
]
},
{
question:"Six bells commence tolling together and toll at intervals of 2, 4, 6, 8 10 and 12 seconds respectively. In 30 minutes, how many times do they toll together ?",
answers: [
{text:'4',correct:false},
{text:'10',correct:false},
{text:'15',correct:false},
{text:'16',correct:true}
]
},
{
question:'Let N be the greatest number that will divide 1305, 4665 and 6905, leaving the same remainder in each case. Then sum of the digits in N is:',
answers: [
{text:'4',correct:true},
{text:'5',correct:false},
{text:'6',correct:false},
{text:'8',correct:false}
]
},
{
question:"The greatest number of four digits which is divisible by 15, 25, 40 and 75 is:",
answers:[
{text:'9000',correct:false},
{text:'9400',correct:false},
{text:'9600',correct:true},
{text:'9800',correct:false}
]
}
];
const questionElement=document.getElementById("question");
const answerButtons=document.getElementById("answer-buttons");
const nextButton=document.getElementById("next-btn");
//Current Question Index
let cqi=0;
let score=0;
function startQuiz()
{
cqi=0;
score=0;
nextButton.innerHTML="Next";
//at the end we'll change it to Replay
showQuestion();
}
function showQuestion()
{
resetState();
//to hide the previous questions and answers
let currentQuestion=questions[cqi];
let questionNo=cqi+1;
questionElement.innerHTML=questionNo+'. '+currentQuestion.question;
currentQuestion.answers.forEach(answer=>{
const button=document.createElement("button");
button.innerHTML=answer.text;
//will create a button with the text for each option of the current question
button.classList.add("btn");
//adding the class name 'btn' to the button
answerButtons.appendChild(button);
//to display this button in the div with id 'answerButton'
if(answer.correct)
{
button.dataset.correct=answer.correct;
}
//it'll add the true or false value in the dataset correct
button.addEventListener("click",selectAnswer);
//when we'll click on the answer button, it'll call this function
});
}
function resetState()
{
nextButton.style.display='none';
while(answerButtons.firstChild)
{
answerButtons.removeChild(answerButtons.firstChild);
}
}
function selectAnswer(e)
{
const selectedBtn =e.target;
const isCorrect = selectedBtn.dataset.correct=="true";
if(isCorrect)
{
selectedBtn.classList.add("correct");
score++;
}
else{
selectedBtn.classList.add("incorrect");
}
//using css, we'll change the background colours of the classes 'correct' and 'incorrect'
Array.from(answerButtons.children).forEach(button=>{
if(button.dataset.correct==="true")
{
button.classList.add("correct");
}
//to show the correct answer regardless of the fact we have answered correctly or not
//so to do that it is traversing through the list
button.disabled=true;
//disabling the button so that no more options can be selected
});
nextButton.style.display="block";
//to make the next button visible which was previously set as display:'none'
}
function showScore()
{
resetState();
questionElement.innerHTML=`You scored ${score} out of ${questions.length}!`;
nextButton.innerHTML="Play Again";
nextButton.style.display="block";
exit.style.display="block";
}
function handleNextButton()
{
cqi++;
if(cqi<questions.length)
{
showQuestion();
}
else
{
showScore();
}
}
nextButton.addEventListener("click",()=>{
if(cqi<questions.length)
{
handleNextButton();
}
else
{
startQuiz();
}
});
startQuiz();