word/quiz: common js function to shuffle arrays
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 29 Dec 2021 05:00:24 +0000 (06:00 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Fri, 31 Dec 2021 04:29:05 +0000 (05:29 +0100)
word/memory.js
word/multichoice.js
word/quiz.js

index 375b7c185bab825ba6eb63b5ea80862952ac6cf0..48e8b44cfb441ae2db57eba2b90482e8b19910f2 100644 (file)
@@ -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`,
index aa018d015294ec5f031d58c8112542795b031178..649d19a5d9460e0ae549f15576870b18ea1c97bb 100644 (file)
@@ -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: () => {
index 021758650c4c26a8eec2688fd98388f5916e63b0..70f8500bf2bffa30e0b5b899be79c3d561effc92 100644 (file)
@@ -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) {