-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.js
137 lines (91 loc) · 3.04 KB
/
js.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
var playing = false;
var score;
var action;
var timeremaining;
var correctans;
var arrAns = [];
document.getElementById("startreset").onclick = function() {
if (playing == true) {
location.reload();
} else {
score = 0;
playing = true;
document.getElementById("timeremaining").style.display = "block";
document.getElementById("scorevalue").innerHTML = score;
document.getElementById("startreset").innerHTML = "reset";
timeremaining = 60;
count();
document.getElementById("timeremainingvalue").innerHTML = timeremaining;
hide("gameOver");
hide("correct");
hide("wrong");
generateQA();
for (var i = 1; i < 5; i++) {
document.getElementById("box" + i).onclick = function() {
if (this.innerHTML == correctans) {
show("correct");
hide("wrong");
score++;
document.getElementById("scorevalue").innerHTML = score;
setTimeout(function() {
hide("correct");
}, 1050)
generateQA();
} else {
hide("correct");
show("wrong");
setTimeout(function() {
hide("wrong");
}, 1050)
}
}
}
}
}
function count() {
action = setInterval(
function() {
timeremaining -= 1;
document.getElementById("timeremainingvalue").innerHTML = timeremaining;
if (timeremaining == 0) {
clear();
hide("timeremaining");
document.getElementById("startreset").innerHTML = "Start Game";
show("gameOver");
document.getElementById("gameOver").innerHTML = "<p> Game Over <p> <p>Your Score is " + score + "</p>";
playing = false;
}
}, 1000);
}
function hide(id) {
document.getElementById(id).style.display = "none";
}
function show(id) {
document.getElementById(id).style.display = "block";
}
function clear() {
clearInterval(action);
}
function generateQA() {
var arrAns = [];
do {
var x = 1 + Math.round(9 * Math.random());
var y = 1 + Math.round(9 * Math.random());
correctans = x * y;
}
while (arrAns.indexOf(correctans) > -1)
arrAns.push(correctans);
var correctpos = 1 + Math.round(3 * Math.random());
document.getElementById("question").innerHTML = x + "x" + y;
document.getElementById("box" + correctpos).innerHTML = correctans;
var wrongans;
var arr2 = [correctans];
for (var i = 1; i < 5; i++) {
if (i != correctpos) {
do { wrongans = 1 + Math.round(9 * Math.random()) * 1 + Math.round(9 * Math.random()); }
while (arr2.indexOf(wrongans) > -1)
document.getElementById("box" + i).innerHTML = wrongans;
arr2.push(wrongans);
}
}
}