-
Notifications
You must be signed in to change notification settings - Fork 10
/
kviz.html
200 lines (165 loc) · 6.01 KB
/
kviz.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
200
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Václaváky - kvíz</title>
<link rel='shortcut icon' type='image/x-icon' href='favicon.ico' />
<meta name='viewport' content='width=device-width, initial-scale=1'>
<meta name="theme-color" content="#EEEEEE" />
<style type='text/css'>
body {
background-color: #eee;
margin: 0;
}
main {
max-width: 960px;
padding-top: 10%;
margin: 1em auto;
padding: 1.5em;
}
footer {
text-align: center;
color: rgb(166, 166, 166);
}
div#question {
margin: 1em 0;
font-size: 2em;
}
div#question input#guess {
font-size: 1em;
width: 4em;
}
div#buttons {
margin: .5em auto;
width: 50%;
}
div#buttons button {
margin: 0 .3em;
font-size: 1em;
}
</style>
</head>
<body>
<main>
<div id='measures'>
</div>
<div id='question'>
</div>
</main>
<footer>
S láskou k absurdním konverzím sepsal <a href='https://twitter.com/kondrej'>@kondrej</a>. Zdrojáky volně dostupný <a href='https://github.com/kokes/vaclavaky'>na GitHubu</a>.
</footer>
<script type='text/javascript' src='lib.js'></script>
<script type='text/javascript'>
// function initMeasures() {
// const md = document.getElementById('measures');
// let j = 0
// for (let measure of Object.keys(convTg)) { // TODO: jeste nekonvertovany
// const elId = `measure_${j}`;
// const inp = document.createElement('input');
// inp.setAttribute('type', 'checkbox');
// inp.setAttribute('id', elId);
// inp.setAttribute('checked', 'checked');
// inp.setAttribute('disabled', 'disabled');
// inp.addEventListener('change', () => {
// // TODO
// })
// const label = document.createElement('label');
// label.textContent = measure;
// label.setAttribute('for', elId);
// md.appendChild(inp);
// md.appendChild(label);
// j += 1;
// }
// }
function randomElement(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
// warning: in place
function shuffleArray(arr) {
arr.sort(() => Math.random() - .5);
}
function norm(units, unit) {
if (!unit) {
return units;
}
return units * unitLookup(unit).rate;
}
function normval(convobj) {
return norm(convobj.units, convobj.unit);
}
function renderQuestion() {
const qd = document.getElementById('question');
qd.innerHTML = '';
let measures = new Set();
convTgObj.reduce((acc, val) => Object.keys(val.conversions).forEach(x => measures.add(x)));
const measure = randomElement(Array.from(measures));
let targets = convTgObj.filter(x => x.conversions.hasOwnProperty(measure));
targets = JSON.parse(JSON.stringify(targets)); // nechcem mazat reálný data, musíme je duplikovat
let qs = [];
shuffleArray(targets);
qs.push(targets.pop());
shuffleArray(targets);
qs.push(targets.pop());
// chceme tu menší/lehčí/levnější věc jako první, ať ta otázka má smysl
qs.sort((a, b) => normval(a.conversions[measure]) - normval(b.conversions[measure]));
qd.innerHTML = `${measure}: <input id='guess'></input> <em>${qs[0].label}</em> se vejde do <em>${qs[1].label}</em>`;
const answer = normval(qs[1].conversions[measure]) / normval(qs[0].conversions[measure]);
console.log(`Pro podvodníčky tu máme odpověď: ${answer}`);
const butts = document.createElement('div');
butts.setAttribute('id', 'buttons');
const val = document.createElement('button');
val.setAttribute('id', 'check');
val.textContent = 'Kontroluj';
const dunno = document.createElement('button');
dunno.textContent = 'Nevim, dál';
dunno.addEventListener('click', () => {
renderQuestion();
});
val.addEventListener('click', (e) => {
const guess = document.getElementById('guess').value;
// TODO: validace, že guess je číslo
const fguess = parseFloat(guess);
const err = Math.abs(Math.log10(fguess) - Math.log10(answer));
const evl = document.createElement('div');
evl.setAttribute('id', 'evaluation');
let answerFixed = answer.toFixed(2);
if (answerFixed.endsWith('.00')) {
answerFixed = answerFixed.slice(0, -3);
}
if (fguess.toFixed(2) === answerFixed) {
evl.textContent = `Naprosto přesně!`;
} else if (err < 1) {
evl.textContent = `Řádově můžem uznat! Přesně to je ${answerFixed}`;
} else {
evl.textContent = `Neni to tak, správně to je ${answerFixed}`;
}
qd.appendChild(evl);
e.target.setAttribute('disabled', 'disabled');
dunno.textContent = 'Dál';
})
butts.appendChild(val);
butts.appendChild(dunno);
qd.appendChild(butts);
document.getElementById('guess').focus();
document.getElementById('guess').addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
val.click();
}
})
}
// initMeasures();
renderQuestion();
</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-98368126-2"></script>
<script>
if (!window.location.href.startsWith('file:/')) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-98368126-2');
}
</script>
</body>
</html>