From 2c074212f77f8f4fbafe5f21774c6a107394d0d4 Mon Sep 17 00:00:00 2001 From: taoky Date: Thu, 12 Oct 2023 11:36:12 +0800 Subject: [PATCH] terms: add quiz question shuffling --- frontend/templates/terms.html | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/frontend/templates/terms.html b/frontend/templates/terms.html index 76b180d..d2b8b80 100644 --- a/frontend/templates/terms.html +++ b/frontend/templates/terms.html @@ -62,6 +62,8 @@

{{ obj.name }}

return; } const quizs = JSON.parse(ele.textContent); + // comment out this if you don't want to shuffle the quizs. + this.inplace_shuffle(quizs.questions); this.quizs = quizs; }, check() { @@ -75,6 +77,18 @@

{{ obj.name }}

} } this.enable_submit = true; + }, + inplace_shuffle(array) { + // https://stackoverflow.com/a/2450976/8460426 + let current_index = array.length; + let random_index; + + while (current_index > 0) { + random_index = Math.floor(Math.random() * current_index); + current_index--; + + [array[current_index], array[random_index]] = [array[random_index], array[current_index]]; + } } } });