-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
146 lines (121 loc) · 3.58 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
var playing = false;
var score;
var action;
var timeremaining;
var correctanswer;
//if we click on the start/reset button
document.getElementById("startreset").onclick=function(){
//if we are playing
if(playing==true)
{
location.reload(); //reload page
}
else{
//if we are not playing
//change into playing mode
playing = true;
//set score = 0
score=0;
document.getElementById("scorevalue").innerHTML=score;
//show countdown box
show("timer");
timeremaining=60;
document.getElementById("timervalue").innerHTML=timeremaining;
//hide game over box
hide("gameover");
//change button to reset
document.getElementById("startreset").innerHTML="Reset Game";
//start countdown
startcountdown();
//generate questions and answer
generateQA();
}
}
//clicking on the 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)
{
score++;
document.getElementById("scorevalue").innerHTML=score;
//hide wrong box and show correct box
hide("wrong");
show("correct");
setTimeout(function(){
hide("correct");
},1000)
//generate new Q&A
generateQA();
}
else{
hide("correct");
show("wrong");
setTimeout(function(){
hide("wrong");
},1000)
}
}
}
}
//functions area
//start counter funcn
function startcountdown()
{
action = setInterval(function(){
timeremaining -=1;
document.getElementById("timervalue").innerHTML=timeremaining;
if(timeremaining==0)
{//game over
stopcountdown();
show("gameover");
document.getElementById("gameover").innerHTML="<p>Game over!!</p><p>You score is " + score +"</p>";
hide("timer");
hide("correct");
hide("wrong");
playing = false;
document.getElementById("startreset").innerHTML="Start Game";
}
},1000);
}
//stop counter funcn
function stopcountdown()
{
clearInterval(action);
}
//hide elements
function hide(Id)
{
document.getElementById(Id).style.display="none";
}
//show elements
function show(Id)
{
document.getElementById(Id).style.display="block";
}
//generate Q&A
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;
//fill one box with correct answer
var correctposition=1+Math.round(3*Math.random());
document.getElementById("box" + correctposition).innerHTML=correctanswer;
//fill the 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()));
}while(wronganswer == correctanswer)
wronganswer=(1 + Math.round(9*Math.random()))*(1 + Math.round(9*Math.random()));
document.getElementById("box"+i).innerHTML=wronganswer;
}
}
}