word/quiz: put-selector library to build html in js
[sheet.git] / word / quiz.js
1 let quiz = {
2 dataurl: '/data/wordlist.nl.json',
3
4 next: () => {
5         let word = quiz.words.shift();
6         let form = put(quiz.form,
7                 'img[src=$]+ul', `/data/word/en/${word[0]}.jpg`,
8         );
9
10         let answers = [word[2], quiz.words[1][2], quiz.words[2][2], quiz.words[3][2]]
11                 .sort(() => {return .5 - Math.random()}) // shuffle
12         answers.forEach(suggest => {
13                 let option = put(form, 'li', suggest, {onclick: () => {
14                         if (suggest != word[2]) {
15                                 // incorrect
16                                 put(option, '.wrong');
17                                 return;
18                         }
19                         put(option, '.good');
20                         window.setTimeout(quiz.next, 500);
21                 }});
22         });
23 },
24
25 setup: () => {
26         fetch(quiz.dataurl).then(res => res.json()).then(json => {
27                 quiz.form = document.getElementById('quiz');
28                 quiz.words = Object.values(json)
29                         .sort(() => {return .5 - Math.random()}) // shuffle
30                         .map(row => row.split(/:/))
31                 quiz.next();
32         });
33 },
34 };
35
36 quiz.setup();