word/quiz: preset configuration filters level
[sheet.git] / word / quiz.js
1 Array.prototype.shuffle = function () {
2         for (let i = this.length - 1; i > 0; i--) {
3                 const j = Math.floor(Math.random() * (i + 1)); // random index 0..i
4                 [this[i], this[j]] = [this[j], this[i]]; // swap elements
5         }
6         return this;
7 };
8
9 class WordQuiz {
10         dataselect(json) {
11                 // find viable rows from json data
12                 let rows = Object.values(json);
13                 if (this.preset.level !== undefined) {
14                         rows = rows.filter(row => row[1] <= this.preset.level);
15                 }
16                 return rows.shuffle();
17         }
18
19         load(dataurl) {
20                 this.preset = {};
21                 fetch(dataurl).then(res => res.json()).then(json => {
22                         this.words = this.dataselect(json)
23                         this.setup();
24                 });
25         }
26
27         constructor(dataurl) {
28                 this.load(dataurl);
29         }
30 }