From: Mischa POSLAWSKY Date: Wed, 29 Dec 2021 05:00:24 +0000 (+0100) Subject: word/quiz: common js function to shuffle arrays X-Git-Tag: v1.13~66 X-Git-Url: http://git.shiar.nl/sheet.git/commitdiff_plain/557e9a9b737294f5e170f8f5474ff750ab72cb4a word/quiz: common js function to shuffle arrays --- diff --git a/word/memory.js b/word/memory.js index 375b7c1..48e8b44 100644 --- a/word/memory.js +++ b/word/memory.js @@ -45,7 +45,7 @@ class WordMemory extends WordQuiz { this.form = document.getElementById('quiz'); this.cards = Object.entries(pairs).flat() .map(e => e.toString()) - .sort(() => {return .5 - Math.random()}) // shuffle + .shuffle() this.cards.forEach(word => { put(this.form, 'figure>img[src=$]<', `/data/word/en/${word}.jpg`, diff --git a/word/multichoice.js b/word/multichoice.js index aa018d0..649d19a 100644 --- a/word/multichoice.js +++ b/word/multichoice.js @@ -5,8 +5,8 @@ class WordMultiChoice extends WordQuiz { '+img[src=$]+ul', `/data/word/en/${word[0]}.jpg`, ); - let answers = [word[2], this.words[1][2], this.words[2][2], this.words[3][2]] - .sort(() => {return .5 - Math.random()}) // shuffle + let answers = [word[2], this.words[0][2], this.words[1][2], this.words[2][2]] + .shuffle() answers.forEach(suggest => { let label = suggest.replace(/\/.*/, ''); let option = put(form, 'li', label, {onclick: () => { diff --git a/word/quiz.js b/word/quiz.js index 0217586..70f8500 100644 --- a/word/quiz.js +++ b/word/quiz.js @@ -1,7 +1,11 @@ +Array.prototype.shuffle = function () { + return this.sort(() => {return .5 - Math.random()}); +}; + class WordQuiz { dataselect(json) { let rows = Object.values(json); - return rows.sort(() => {return .5 - Math.random()}) // shuffle + return rows.shuffle(); } load(dataurl) {