-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
199 lines (195 loc) · 5.11 KB
/
index.html
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
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap"
rel="stylesheet"
/>
</head>
<body style="font-family: "Open Sans", sans-serif">
<div style="text-align: center" id="initial">
<h1>Reaction Test</h1>
<p>
Press the arrow key or on-screen button with the same direction as the
arrow shown on screen.
</p>
<input
style="width: 25%; height: 2rem; font-size: 1rem"
type="button"
onclick="begin()"
value="Begin"
/>
</div>
<div
style="
height: 100%;
display: none;
text-align: center;
flex-direction: column;
flex-wrap: wrap;
"
id="game"
>
<h1 style="flex-basis: 100%" id="game-header">
Press the correct arrow key for the arrow shown on screen.
</h1>
<h3 style="flex-basis: 100%" id="game-done">Test: 1/50</h3>
<h3 style="flex-basis: 100%" id="game-score">Score: 0</h3>
<h3 style="flex-basis: 100%" id="game-avg"></h3>
</div>
<div style="width: 100%; display: none" id="game-arrows">
<img
id="a1"
src="./arrow.png"
style="position: relative; left: 0%; transform: scaleX(-1)"
width="25%"
hidden="false"
/>
<img
id="a2"
src="./arrow.png"
style="position: relative; left: 75%"
width="25%"
hidden="false"
/>
<br />
<input
type="button"
onclick="lClick()"
style="
font-size: 1rem;
position: relative;
left: 0%;
width: 25%;
height: 2rem;
"
value="Left"
/>
<input
type="button"
onclick="rClick()"
style="
font-size: 1rem;
position: relative;
left: 50%;
width: 25%;
height: 2rem;
"
value="Right"
/>
</div>
<script>
let times = [];
let tests = [];
let t1;
let currentTest = 0;
let currentAnswer;
let score = 0;
let done = false;
function nextTest() {
if (currentTest == 50) {
done = true;
let avg = 0;
for (let i = 0; i < times.length; i++) {
avg += times[i];
}
avg /= times.length;
document.getElementById("game-arrows").style.display = "none";
document.getElementById("game-header").innerText = "You're done!";
document.getElementById(
"game-avg",
).innerText = `Average time to right answer: ${avg} seconds.`;
return;
}
document.getElementById("a1").hidden = true;
document.getElementById("a2").hidden = true;
let test = tests[currentTest];
if (test[0] == 0) {
var currentArrow = document.getElementById("a1");
} else {
var currentArrow = document.getElementById("a2");
}
if (test[1] == 0) {
currentArrow.style.transform = "scaleX(-1)";
currentAnswer = "l";
} else {
currentArrow.style.transform = "scaleX(1)";
currentAnswer = "r";
}
currentArrow.hidden = false;
currentTest++;
t1 = Date.now();
}
function lClick() {
if (currentAnswer == "l") {
score++;
times.push((Date.now() - t1) / 1000);
}
if (currentTest != 50) {
document.getElementById("game-done").innerText = `Test: ${
currentTest + 1
}/50`;
}
document.getElementById("game-score").innerText = `Score: ${score}`;
nextTest();
}
function rClick() {
if (currentAnswer == "r") {
score++;
times.push((Date.now() - t1) / 1000);
}
if (currentTest != 50) {
document.getElementById("game-done").innerText = `Test: ${
currentTest + 1
}/50`;
}
document.getElementById("game-score").innerText = `Score: ${score}`;
nextTest();
}
function begin() {
window.addEventListener("keydown", (event) => {
if(done) {
return;
}
if (event.code == "ArrowLeft") {
lClick();
} else if (event.code == "ArrowRight") {
rClick();
}
});
document
.getElementById("initial")
.parentNode.removeChild(document.getElementById("initial"));
document.getElementById("game").style.display = "flex";
document.getElementById("game-arrows").style.display = "block";
for (let i = 0; i < 50; i++) {
if (i <= 4) {
var lOrR = Math.random();
if (lOrR < 0.5) {
tests.push([0, 0]);
} else {
tests.push([1, 1]);
}
} else {
var j1 = Math.random();
var j2 = Math.random();
var test = [];
if (j1 < 0.5) {
test.push(0);
} else {
test.push(1);
}
if (j2 < 0.5) {
test.push(0);
} else {
test.push(1);
}
tests.push(test);
}
}
nextTest();
}
</script>
</body>