From e5055fbc487c3a27faec51dc7ac5f3070f697702 Mon Sep 17 00:00:00 2001 From: Oleh Omelchenko Date: Thu, 30 Jan 2025 16:22:20 +0200 Subject: [PATCH] Randomize quiz question order and store ordinal number for each question --- src/js/quiz.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/js/quiz.js b/src/js/quiz.js index 78b5009..c5bd760 100644 --- a/src/js/quiz.js +++ b/src/js/quiz.js @@ -56,6 +56,7 @@ document.addEventListener('DOMContentLoaded', function () { throw new Error('Invalid quiz data or version'); } questions = data.quizzes[version].questions; + questions = shuffleArray(questions); // Randomize the order of questions displayQuestion(); }) .catch(error => { @@ -63,6 +64,15 @@ document.addEventListener('DOMContentLoaded', function () { quizContainer.innerHTML = '

Error loading quiz. Please try again later.

'; }); + // Function to shuffle an array + function shuffleArray(array) { + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } + return array; + } + function displayQuestion() { if (!questions || currentQuestionIndex >= questions.length) { showResults(); @@ -162,6 +172,7 @@ document.addEventListener('DOMContentLoaded', function () { correctAnswer: currentQuestion.answer, isCorrect: isCorrect, questionIndex: currentQuestionIndex, + ordinalNumber: currentQuestionIndex + 1, // Store the ordinal number of the question timestamp: new Date().toISOString(), timeSpent: 25000 - timeLeft, // Time spent in milliseconds chartType: currentQuestion.chart,