word/multichoice: stop when answers run out
[sheet.git] / word / multichoice.js
1 class WordMultiChoice extends WordQuiz {
2         next() {
3                 if (this.words.length < 4) return;
4                 let word = this.words.shift();
5                 if (!word[2]) return this.next();
6                 let form = put(this.form,
7                         '+img[src=$]+ul', `/data/word/en/${word[2]}.jpg`,
8                 );
9
10                 let answers = [word[0], this.words[0][0], this.words[1][0], this.words[2][0]]
11                         .shuffle()
12                 answers.forEach(suggest => {
13                         let label = suggest.replace(/\/.*/, '');
14                         let option = put(form, 'li', label, {onclick: () => {
15                                 if (suggest != word[0]) {
16                                         // incorrect
17                                         put(option, '.wrong');
18                                         return;
19                                 }
20                                 put(option, '.good');
21                                 window.setTimeout(() => this.next(), 500);
22                         }});
23                 });
24         }
25
26         setup() {
27                 this.form = document.getElementById('quiz');
28                 this.next();
29         }
30 };