word/quiz: preset configuration filters level
[sheet.git] / word / quiz.js
index 70f8500bf2bffa30e0b5b899be79c3d561effc92..146aff73e61171c10f81779aad8be30777d7da35 100644 (file)
@@ -1,14 +1,23 @@
 Array.prototype.shuffle = function () {
-       return this.sort(() => {return .5 - Math.random()});
+       for (let i = this.length - 1; i > 0; i--) {
+               const j = Math.floor(Math.random() * (i + 1)); // random index 0..i
+               [this[i], this[j]] = [this[j], this[i]]; // swap elements
+       }
+       return this;
 };
 
 class WordQuiz {
        dataselect(json) {
+               // find viable rows from json data
                let rows = Object.values(json);
+               if (this.preset.level !== undefined) {
+                       rows = rows.filter(row => row[1] <= this.preset.level);
+               }
                return rows.shuffle();
        }
 
        load(dataurl) {
+               this.preset = {};
                fetch(dataurl).then(res => res.json()).then(json => {
                        this.words = this.dataselect(json)
                        this.setup();