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