361d075e66391f517b08b8f51f7a12222ef9b954
[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                 let rows = Object.values(json);
12                 return rows.shuffle();
13         }
14
15         load(dataurl) {
16                 fetch(dataurl).then(res => res.json()).then(json => {
17                         this.words = this.dataselect(json)
18                         this.setup();
19                 });
20         }
21
22         constructor(dataurl) {
23                 this.load(dataurl);
24         }
25 }