-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdrill.html
More file actions
232 lines (196 loc) · 6.77 KB
/
drill.html
File metadata and controls
232 lines (196 loc) · 6.77 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
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
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="jatloesmile-hd.png">
<title>Drill Drill Drill</title>
<style>
.side{
display: inline;
}
body, button, p, input, select {
font-family: 'Monospace Typewriter', monospace;
font-size: 48px;
}
#mainTimer {
display: inline-block;
transform-origin: center;
}
</style>
</head>
<body>
<p id="problem">Press space to start</p>
<p id="timer">------------------------------------------------------------</p>
<p id="score">Score: 0</p>
<input type="text" id="ansbox" disabled>
<br> <br> <br>
<p class="side">Problem type:</p>
<select name="problemType" id="problemType" onchange="changeProblemType()">
<option value="binary">Binary</option>
<option value="divisors">Divisors</option>
<option value="addition">Addition</option>
<option value="alphabet">Alphabet</option>
<option value="alphabetrev">Alphabet (Rev)</option>
<option value="sudoku">Sudoku</option>
<option value="bigram">Repeated Bigram</option>
</select>
<br> <br>
<p class="side">Time:</p>
<input type="number" value="60" min="1" max="999" id="timelimit">
<p class="side">frames in</p>
<input type="number" value="20" min="1" max="999" id="timelimitfps">
<p class="side">fps</p>
<br> <br>
<button onclick="alert('For binary, you can use ASDFG for 1 and HJKL; for 0. Space to give up or restart. Recommended for Alphabet and Alphabet (Rev) is 60 frames in 60 fps.')">Read this</button>
</body>
<script>
let fps = 20;
let maxTimerLen = 60;
let timer = document.getElementById("timer");
let problemElement = document.getElementById("problem");
let scoreElement = document.getElementById("score");
let problemTypeElement = document.getElementById("problemType");
let answerBox = document.getElementById("ansbox");
let answer = "";
let playing = false;
let score = 0;
let problemType = "binary"; // binary, divisors, addition, alphabet
let playedAtAll = false;
function changeProblemType() {
problemType = problemTypeElement.value;
}
function lose() {
playing = false;
problemElement.innerText = `${problemElement.innerText} -> ${answer} but you put ${answerBox.value}.`;
if (timer.innerText) problemElement.innerText += " (Incorrect)";
else problemElement.innerText += " (Out of time)";
timer.innerText = "-".repeat(maxTimerLen);
answerBox.value = "";
answerBox.disabled = true;
score = 0; // Don't update display until restart
}
function answerChanged() {
if (!playing) return;
if (problemType === "binary")
answerBox.value = answerBox.value.replaceAll(/[asdfg]/g, "1").replaceAll(/[hjkl;]/g, "0").replaceAll(/[^0-9asdfghjkl; ]/g, ""); // Space to give up
if (problemType === "alphabetrev")
answerBox.value = answerBox.value.toUpperCase();
let currInput = answerBox.value;
if (answer == currInput) {
answerBox.value = "";
score++;
newProblem();
}
else if (answer.substring(0, currInput.length) != currInput) lose();
else console.log(currInput);
return false;
}
answerBox.oninput = answerChanged;
// answerBox.onmousedown = () => {
// return true;
// }
function decreaseTimer() {
if (!playing) return;
answerBox.focus();
if (timer.innerText) timer.innerText = timer.innerText.substring(1);
if (!timer.innerText) lose();
}
function randomInteger(min, max) {
return Math.floor(Math.random()*(max-min+1)) + min;
}
// https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function generateNewProblem() {
playing = true;
answerBox.disabled = false;
let problem = "";
if (problemType === "binary") {
let num = randomInteger(32,63);
problem = num.toString();
answer = "";
while (num) {
answer = (num % 2).toString() + answer;
num >>= 1;
}
} else if (problemType === "divisors") {
let num = randomInteger(10,99);
problem = num.toString();
answer = 0;
for (let i = 1; i <= num; i++) {
if (num % i == 0) answer += 1;
}
answer = answer.toString();
} else if (problemType === "addition") {
let num1 = randomInteger(100,999);
let num2 = randomInteger(100,999);
problem = num1.toString() + " + " + num2.toString();
answer = (num1+num2).toString();
} else if (problemType === "alphabet") {
let num1 = randomInteger(1,26);
problem = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(num1);
answer = num1.toString();
} else if (problemType === "alphabetrev") {
let num1 = randomInteger(1,26);
problem = num1.toString();
answer = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(num1);
} else if (problemType === "sudoku") {
let num = randomInteger(1,9);
let arr = [1,2,3,4,5,6,7,8,9];
arr.splice(num-1,1);
shuffleArray(arr);
problem = "";
for (let i of arr) problem += i.toString();
answer = num.toString();
} else if (problemType === "bigram") {
let s = "";
let ans = "";
while (true) {
s = "";
for (let i = 0; i < 20; i++) s += randomInteger(0,9);
let cts = [];
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
let s1 = i.toString() + j.toString();
let num = 0;
for (let ind = 0; ind < s.length-1; ind++) {
if (s.substring(ind,ind+2) == s1) num++;
}
if (num == 2) ans = s1;
cts.push(num);
}
}
cts.sort();
if (cts[cts.length-1] == 2 && cts[cts.length-2] < 2) break;
}
problem = s;
answer = ans;
}
return problem;
}
function initialize() {
document.getElementById("timelimit").disabled = true;
document.getElementById("timelimitfps").disabled = true;
maxTimerLen = parseInt(document.getElementById("timelimit").value);
fps = parseInt(document.getElementById("timelimitfps").value);
setInterval(decreaseTimer, 1000/fps);
}
function newProblem() {
if (!playedAtAll) {
initialize();
playedAtAll = true;
}
problemElement.innerText = generateNewProblem();
timer.innerText = "-".repeat(maxTimerLen);
scoreElement.innerText = "Score: " + score.toString();
}
document.onkeydown = (e) => {
if (e.keyCode === 32 && playing === false) {
newProblem();
}
}
</script>
</html>