-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.js
More file actions
137 lines (119 loc) · 3.83 KB
/
helper.js
File metadata and controls
137 lines (119 loc) · 3.83 KB
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
//if we click on the start/reset button
var start = document.getElementById("st");
var scoreValue = document.getElementById("scorevalue");
var time = document.getElementById("timer");
var box = document.getElementsByClassName("box");
var playing = false;
var action;
var timing;
var answer;
var score;
var highest = 0;
startreset.addEventListener("click",function(){
//if we are playing
//reload the page
if(playing === true){
start.textContent = "Start Game";
playing = false;
location.reload();
}
else if(playing === false){
hide("gameOver");
//if we are not playing
//change button text to reset game
playing = true;
start.textContent = "Reset Game";
//set score to 0
score = 0;
scoreValue.innerHTML = score.toString();
//show countdown box
show("timeremain");
timing = 60;
time.innerHTML = timing;
startcountdown();
//generate new question and answer
generateQuestion();
}
});
//to start countdown
function startcountdown(){
//reduce time by 1sec in loops
//timeleft?
//yes->continue
//no->gameover
action = setInterval(function(){
if(timing===0){
playing = false;
show("gameOver");
hide("timeremain");
hide("wrong");
hide("correct");
document.getElementById("finalscore").innerHTML = score.toString();
start.textContent = "Start Game";
if(score>highest){
highest = score;
document.getElementById("highest").innerHTML = highest.toString();
}
return;
}
timing--;
time.innerHTML = timing;
},1000);
};
//if we click on answer box
for(var i=0;i<box.length;i++){
box[i].addEventListener("click",function(){
//if we are playing
//correct?
if(playing){
if(this.textContent === answer.toString()){
//yes
//increase score by 1
score++;
//show correct box for 1 sec
scoreValue.innerHTML = score.toString();
show("correct");
setTimeout(function(){
hide("correct");
}, 1000);
//generate new questions and answers
generateQuestion();
}
else{
//no
//show try again box for 1 sec
show("wrong");
setTimeout(function(){
hide("wrong");
}, 1000);
}
}
//if we are not playing
//no action
});
};
//to generate a question and multiple answers
function generateQuestion(){
var x = Math.round(9*Math.random()) + 1;
var y = Math.round(9*Math.random()) + 1;
var correctAnswer = x*y;
var correctIndex = Math.round(3*Math.random()) + 1;
answer = correctAnswer;
document.getElementById("question").innerHTML = x + " X " + y;
for(var i=1;i<5;i++){
if(i===correctIndex){
continue;
}
var t = Math.round(99*Math.random()) + 1;
document.getElementById("box"+i).innerHTML = t;
}
document.getElementById("box"+correctIndex).innerHTML = correctAnswer;
};
//to hide a specific button
function hide(ID){
document.getElementById(ID).style.display = "none";
};
//to show a specific button
function show(ID){
document.getElementById(ID).style.display = "block";
};