-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.js
137 lines (114 loc) · 4.57 KB
/
game.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
var LIMIT = localStorage.getItem("qCount");
var TYPE = localStorage.getItem("type");
var NEG = JSON.parse(localStorage.getItem("negSubtraction"));
window.onload = function(){
startCountdown();
localStorage.setItem("number", 0);
localStorage.setItem("score", 0);
document.getElementById("text").style.opacity = 0;
var txt = document.getElementById("text");
var btn = document.getElementById("submit");
text.value = "";
txt.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
btn.click();
}
});
}
// Refresh game if user tabs out (this is a temporary solution to prevent cheaters from abusing how the timer pauses when tab is not in focus; a better solution would be to continue the timer in the background)
window.addEventListener('blur', function(){
document.location.reload();
});
function startCountdown(){
setTimeout(function(){document.getElementById("countdown").style.opacity = "0";}, 800);
setTimeout(function(){document.getElementById("countdown").style.opacity = "1"; document.getElementById("countdown").innerHTML = "3"}, 1000);
setTimeout(function(){document.getElementById("countdown").style.opacity = "0";}, 1800);
setTimeout(function(){document.getElementById("countdown").style.opacity = "1"; document.getElementById("countdown").innerHTML = "2"}, 2000);
setTimeout(function(){document.getElementById("countdown").style.opacity = "0";}, 2800);
setTimeout(function(){document.getElementById("countdown").style.opacity = "1"; document.getElementById("countdown").innerHTML = "1"}, 3000);
setTimeout(function(){document.getElementById("countdown").style.opacity = "0";}, 3800);
setTimeout(function(){document.getElementById("countdown").style.opacity = "1"; document.getElementById("countdown").innerHTML = "Go!"}, 4000);
setTimeout(function(){document.getElementById("countdown-wrapper").style.visibility = "hidden"; document.getElementById("countdown-wrapper").style.height = "15vh"}, 5000);
setTimeout(function(){
var dataset = generateQuestion();
localStorage.setItem("answer", dataset[1]);
document.getElementById("question").innerHTML = dataset[0];
}, 5000);
startTimer();
}
function generateQuestion(){
var a = Math.floor(Math.random() * 10);
var b = Math.floor(Math.random() * 10);
if(TYPE == "addition"){
var question = a + " + " + b;
var answer = a + b;
}
if(TYPE == "subtraction"){
if(NEG == true){
var question = a + " - " + b;
var answer = a - b;
} else {
var question = Math.max(a, b) + " - " + Math.min(a, b);
var answer = Math.max(a, b) - Math.min(a, b);
}
}
if(TYPE == "multiplication"){
var question = a + " × " + b;
var answer = a * b;
}
if(TYPE == "division"){
while(b == 0){
b = Math.floor(Math.random() * 10);
}
var question = a*b + " ÷ " + b;
var answer = (a*b) / b;
}
if(question == document.getElementById("question").innerHTML){
generateQuestion()
} else {
return [question, answer];
}
}
var score = 0;
var number = 0;
function check(){
number++;
localStorage.setItem("number", number);
if(localStorage.getItem("answer") == document.getElementById("text").value){
score++;
localStorage.setItem("score", score);
}
if(number >= LIMIT){
localStorage.setItem("score", window.btoa(score))
localStorage.setItem("seconds", window.btoa(seconds))
window.location.href = "finish.html";
} else {
document.getElementById("qnumber").innerHTML = "Q" + (number + 1);
var dataset = generateQuestion();
localStorage.setItem("answer", dataset[1]);
document.getElementById("question").innerHTML = dataset[0];
document.getElementById("text").value = "";
document.getElementById("text").focus();
}
}
var seconds = 0;
function startTimer(){
setTimeout(
function(){
setTimeout(function(){
document.getElementById("timer").style.opacity = "1";
document.getElementById("text").style.opacity = "1";
document.getElementById("text").disabled = false;
document.getElementById("submit").disabled = false;
document.getElementById("text").focus();
document.getElementById("submit").style.opacity = "1";
setInterval(function() {
document.getElementById("timer").innerHTML = Math.floor((seconds++)/100);
document.getElementById("qnumber").style.opacity = "1";
}, 10);
}, 1000)
generateQuestion();
},
4000)
}