X-Git-Url: http://git.shiar.nl/sheet.git/blobdiff_plain/30bf0fcde3f7e44f1ec7bcfc8253dfce8f81e8e6..f42145cdf77de3602378c70f18545703783122c5:/word/quiz.js diff --git a/word/quiz.js b/word/quiz.js index ce60d3a..0518fa1 100644 --- a/word/quiz.js +++ b/word/quiz.js @@ -1,41 +1,33 @@ -let quiz = { -dataurl: '/data/wordlist.nl.json', +class Quiz { + next() { + let word = this.words.shift(); + let form = put(this.form, + '+img[src=$]+ul', `/data/word/en/${word[0]}.jpg`, + ); -next: () => { - let word = quiz.words.shift(); - let question = document.createElement('img'); - question.src = `/data/word/en/${word[0]}.jpg`; - question.style.maxWidth = '50%'; - - let answers = [word[2], quiz.words[1][2], quiz.words[2][2], quiz.words[3][2]] - .sort(() => {return .5 - Math.random()}) // shuffle - let form = document.createElement('ul'); - answers.forEach(suggest => { - let option = document.createElement('li'); - option.onclick = () => { - if (suggest != word[2]) { - // incorrect - option.classList.add('wrong'); - return; - } - option.classList.add('good'); - window.setTimeout(quiz.next, 500); - }; - option.append(suggest); - form.append(option); - }); - quiz.form.append(question, form); -}, - -setup: () => { - fetch(quiz.dataurl).then(res => res.json()).then(json => { - quiz.form = document.getElementById('quiz'); - quiz.words = Object.values(json) + let answers = [word[2], this.words[1][2], this.words[2][2], this.words[3][2]] .sort(() => {return .5 - Math.random()}) // shuffle - .map(row => row.split(/:/)) - quiz.next(); - }); -}, -}; + answers.forEach(suggest => { + let label = suggest.replace(/\/.*/, ''); + let option = put(form, 'li', label, {onclick: () => { + if (suggest != word[2]) { + // incorrect + put(option, '.wrong'); + return; + } + put(option, '.good'); + window.setTimeout(() => this.next(), 500); + }}); + }); + } -quiz.setup(); + constructor(dataurl) { + fetch(dataurl).then(res => res.json()).then(json => { + this.form = document.getElementById('quiz'); + this.words = Object.values(json) + .sort(() => {return .5 - Math.random()}) // shuffle + .map(row => row.split(/:/)) + this.next(); + }); + } +};