-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
242 lines (134 loc) · 4.69 KB
/
javascript.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* THIS IS OUR JS CODE*/
var playing = false;
var score;
var action;
var timeremaining;
var correctAnswer;
// when we click on our start or reset
document.getElementById("startreset").onclick = function () {
if (playing == true) {
location.reload();//reload page
} else {
//if we are not playing
playing =true;
//set score to zero
score = 0;
document.getElementById("scoreValue").innerHTML= score;
// show coutdown box
show("timeremaining");
timeremaining = 60;
document.getElementById("time").innerHTML = timeremaining;
// hide gameover box
hide("gameOver");
// Change button reset
document.getElementById("startreset").innerHTML= "Reset Game";
//starting the countdown
startCountdown();
// generate a new Q&A
generateQA();
}
}
//clicking on any answer box
for(i=1; i<5; i++){
document.getElementById("box"+i).onclick = function(){
//check if we are playing
if(playing == true){
if(this.innerHTML == correctAnswer){
//if the answer is correct
//increase score by 1
score++;
//display score
document.getElementById("scoreValue").innerHTML = score;
// hide wrong box
hide("wrong");
show("correct");
setTimeout(function() {hide("correct");},1000);
//Then we generate new questions and answers
generateQA();
}else{
// if we have a wrong answer
hide("correct");
show("wrong");
setTimeout(function(){hide("wrong");},1000);
}
}
}
}
// if we click on the start/reset button
// if we are playing,
//reload the page.
//if we are not playing
//set score zero
//show countdown box
// reduce the time by 1 sec in loops
//time left?
//yes = continue
// no = gameover
//change button to reset
// generate new question and options.
// if we click on answer box
// are we playing or not?
//correct?
//yes
// increase score
// show correct boxfor 1sec
// generate new question and options
//if answer is wrong
//show try again box for 1sec.
//This is where we will define all our functions that we will be constantly using for our various manipulation
//start counter
function startCountdown(){
action = setInterval(function(){
timeremaining -=1;
document.getElementById("time").innerHTML = timeremaining;
if(timeremaining == 0){
// gameover
stopCountdown();
show("gameOver");
displayScore();
hide("timeremaining");
hide("correct");
hide("wrong");
playing = false;
document.getElementById("startreset").innerHTML = "Start Game";
}
},1000);
}
//stop Counter
function stopCountdown(){
clearInterval(action);
}
//hide an element
function hide(Id){
document.getElementById(Id).style.display= "none";
}
//show an element
function show(Id){
document.getElementById(Id).style.display= "block";
}
//generate question and multiple answers(options)
function generateQA(){
var x =1+ Math.round(9*Math.random());
var y =1+ Math.round(9*Math.random());
correctAnswer = x*y;
document.getElementById("question").innerHTML = x +"X"+y;
var correctPosition = 1 + Math.round(3*Math.random());
document.getElementById("box"+correctPosition).innerHTML=correctAnswer;//fill one box with the corect answer
var answers = [correctAnswer];
//fill other boxes with wrong answers
for(i=1; i<5; i++){
if(i != correctPosition){
var wrongAnswer;
do{
wrongAnswer =1+ Math.round(9*Math.random())*
(1+ Math.round(9*Math.random()));//a wrong answer
}while(answers.indexOf(wrongAnswer)>-1 )
document.getElementById("box"+i).innerHTML=wrongAnswer;
answers.push(wrongAnswer);
}
}
}
//this will generate our gameover message and set a score
function displayScore(){
document.getElementById("gameOver").innerHTML= "<p>Game over<p/><p>your score is "+ score +".</p>"
}