-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
154 lines (151 loc) · 4.38 KB
/
index.html
File metadata and controls
154 lines (151 loc) · 4.38 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Math</title>
<meta name="description" content="A simple nostalgic math game">
<style>
.row {
max-width: 30rem;
display: flex;
justify-content: space-between;
}
.number {
flex-grow: 1;
font-size: 10rem;
text-align: right;
}
.operation {
flex-grow: 0;
}
.dividor {
max-width: 30rem;
border-top: 0.5rem solid black;
border-radius: 0.5rem;
}
.answer,
.answer:focus {
border: none;
outline: none;
max-width: 30rem;
caret-color: transparent;
}
.score {
font-size: 2rem;
}
.beside {
float: left;
}
.info {
margin-left: 4rem;
}
</style>
</head>
<body>
<div class="displayBox beside">
<div class="row">
<span class="score" id="score">0</span>
<span class="score" id="timer">00:00</span>
</div>
<div class="row">
<span class="number" id="first"></span>
</div>
<div class="row">
<span class="number operation" id="op"></span>
<span class="number" id="second"></span>
</div>
<div class="dividor"></div>
<div class="row">
<input class="answer number" type="text" name="answer" id="answer" autofocus />
</div>
</div>
<div class="beside info">
<h4>
URL Parameters
</h4>
<ul>
<li><code>op</code>: math operation. one of +, or x</li>
<li><code>min</code>: min input number</li>
<li><code>max</code>: max input number</li>
</ul>
</div>
<script>
document.getElementById('answer').oninput = checkAnswer;
document.body.onclick = reselect;
const params = new URLSearchParams(window.location.search);
const valRange = [
!isNaN(parseInt(params.get('min'), 10)) ? parseInt(params.get('min'), 10) : 1,
!isNaN(parseInt(params.get('max'), 10)) ? parseInt(params.get('max'), 10) : 20
];
const startTime = Date.now();
let op = params.get('op') ?? '+';
let inputs, answer;
let correct = 0;
const ping = new Audio('./ping.mp3');
const pong = new Audio('./pong.mp3');
const dong = new Audio('./dong.mp3');
function genNext() {
inputs = Array(2)
.fill()
.map(() => ((Math.random() * valRange[1]) | 0) + valRange[0]);
switch (op) {
case '+': {
answer = inputs[0] + inputs[1];
break;
}
case 'x': {
answer = inputs[0] * inputs[1];
break;
}
default: {
throw new Error('Unknown operation: ' + op);
}
}
document.getElementById('first').innerText = inputs[0];
document.getElementById('second').innerText = inputs[1];
document.getElementById('op').innerText = op;
}
function checkAnswer() {
const box = document.getElementById('answer');
box.value = box.value.replace(/[^0-9-]/g, '');
const toCheck = box.value;
if (answer.toString().length === toCheck.length) {
box.value = '';
}
if (answer.toString() === toCheck) {
correct += 1;
document.getElementById('score').innerText = correct;
if (correct % 10 === 0) {
ping.currentTime = 0;
ping.play();
} else {
pong.currentTime = 0;
pong.play();
}
genNext();
} else if (box.value === '') {
dong.currentTime = 0;
dong.play();
}
}
function reselect() {
const toSelect = document.getElementById('answer');
toSelect.focus();
toSelect.select();
}
genNext();
reselect();
setInterval(() => {
const since = Date.now() - startTime;
const seconds = Math.floor(since / 1000);
const minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
document.getElementById('timer').innerText = `${minutes.toString().padStart(2, '0')}:${secs
.toString()
.padStart(2, '0')}`;
}, 1000);
</script>
</body>
</html>