-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
261 lines (211 loc) · 8.35 KB
/
index.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
class Card {
constructor({ word, translation, options, description }) {
this._word = word;
this._translation = translation;
this._options = options;
this._description = description;
}
isCorrect(n) {
return this._translation === this._options[n];
}
getCorrect() {
return this._options.reduce((res, option, i) => {
if (option === this._translation) res = i;
return res;
} ,0);
}
}
class Deck {
constructor({ words }) {
this._words = words;
this._index = 0;
this._stats = {
correct: 0
};
}
getNext() {
if (this._index >= this._getSize()) return null;
let cardData = this._words[this._index];
this._currentCard = new Card({
word: cardData[0],
translation: cardData[2],
options: this._getRandomOptions(this._index),
description: cardData[1],
});
this._index++;
return this._currentCard;
}
validateOption(n) {
let result = this._currentCard.isCorrect(n);
if (result) {
this._stats.correct += 1;
}
return result;
}
_getRandomOptions(index) {
let indexes = [index];
while (indexes.length < 4) {
let newIndex = getRandom(this._getSize());
if (!indexes.includes(newIndex)) {
indexes.push(newIndex);
}
}
return shuffle(indexes).map(index => this._getTranslation(index));
}
_getSize() {
return this._words.length;
}
_getTranslation(index) {
return this._words[index][2];
}
getStats() {
return {
correct: this._stats.correct,
incorrect: this._words.length - this._stats.correct
}
}
}
document.addEventListener("DOMContentLoaded", function() {
let deck;
window.deck = deck;
let q = document.querySelector.bind(document);
showAction('start');
function showAction(action) {
switch(action) {
case 'start': {
let data = q('.data').value;
let words = data.trim().split('\n').map(row => {
let arr = parseRow(row);
if (arr.length === 3) return arr;
throw Error('can\'t parse: ' + row);
});
deck = new Deck({ words: shuffle(words).splice(0, 30) });
let $deck = q('.deck');
$deck.innerHTML = '<form action="/" class="deck__form"><button value="next">Start</button></form>';
q('.deck__form').addEventListener('click', function(e) {
e.preventDefault();
console.log(e.target);
if (e.target && e.target.tagName === 'BUTTON') {
let action = e.target.value;
showAction(action);
}
});
}
case 'next': {
let card = deck.getNext();
if (card) {
q('.deck__form').innerHTML = `<div class="card">
<div class="card__word"><span class="card__word-inner">${card._word.toLowerCase()}</span></div>
<div class="card__desc"> </div>
<div class="card__options">
<button class="card__option" type="submit" value="chose0">${card._options[0].toLowerCase()}</button>
<button class="card__option" type="submit" value="chose1">${card._options[1].toLowerCase()}</button>
<button class="card__option" type="submit" value="chose2">${card._options[2].toLowerCase()}</button>
<button class="card__option" type="submit" value="chose3">${card._options[3].toLowerCase()}</button>
</div>
</div>
`;
} else {
let stats = deck.getStats();
q('.deck__form').innerHTML = `
<div class="deck__results">
correct: ${stats.correct}(${(stats.correct / deck._getSize() * 100).toFixed(2)}%)<br>
incorrect: ${stats.incorrect}(${(stats.incorrect / deck._getSize() * 100).toFixed(2)}%)</div>
<button class="deck__start" value="start">start</button>
`;
}
break;
}
case 'chose0': {
let card = deck._currentCard;
deck.validateOption(0);
q('.deck__form').innerHTML = getResult({ card: card, selected: 0, correct: card.getCorrect() });
break;
}
case 'chose1': {
let card = deck._currentCard;
let res = deck.validateOption(1);
q('.deck__form').innerHTML = getResult({ card: card, selected: 1, correct: card.getCorrect() });
break;
}
case 'chose2': {
let card = deck._currentCard;
let res = deck.validateOption(2);
q('.deck__form').innerHTML = getResult({ card: card, selected: 2, correct: card.getCorrect() });
break;
}
case 'chose3': {
let card = deck._currentCard;
let res = deck.validateOption(3);
q('.deck__form').innerHTML = getResult({ card: card, selected: 3, correct: card.getCorrect() });
break;
}
}
function getResult({ card, correct, selected }) {
function getCorrectOrSelected(row) {
if (correct == row)
return 'card__option--correct';
if (selected == row)
return 'card__option--selected';
return '';
}
return `<div class="card">
<div class="card__word"><span class="card__word-inner">${card._word.toLowerCase()}</span></div>
<div class="card__desc">${card._description.toLowerCase() && ' '}</div>
<div class="card__options">
<button disabled class="card__option ${getCorrectOrSelected(0)}">${card._options[0].toLowerCase()}</button>
<button disabled class="card__option ${getCorrectOrSelected(1)}">${card._options[1].toLowerCase()}</button>
<button disabled class="card__option ${getCorrectOrSelected(2)}">${card._options[2].toLowerCase()}</button>
<button disabled class="card__option ${getCorrectOrSelected(3)}">${card._options[3].toLowerCase()}</button>
</div>
<button class="deck__next" value="next">next</button>
</div>
`
}
}
});
const words = [
['bla', 'bla desc', 'ру бла'],
['bla1', '', 'ру бла1'],
['bla2', 'bla desc2', 'ру бла2'],
['bla3', '', 'ру бла3'],
['bla4', 'bla desc4', 'ру бла4'],
];
/* ======================================================================== */
function getRandom(n) {
return Math.trunc(Math.random() * n);
}
function shuffle(orig_array) {
var array = orig_array.slice();
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function parseRow(row) {
var res = [];
var str = row;
var index;
while (res.length != 3) {
if (str[0] === '"') {
index = str.indexOf('"', 1);
if (index == -1) index = str.length;
res.push(str.substring(1, index));
str = str.substring(index + 2);
} else {
index = str.indexOf(',');
if (index == -1) index = str.length;
res.push(str.substring(0, index));
str = str.substring(index + 1);
}
}
return res;
}