word/quiz: fisher-yates shuffle algorithm
[sheet.git] / word / quiz.js
index 70f8500bf2bffa30e0b5b899be79c3d561effc92..361d075e66391f517b08b8f51f7a12222ef9b954 100644 (file)
@@ -1,5 +1,9 @@
 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 {