add random shuffle for questions on each login

This commit is contained in:
Aibek 2026-09-11 17:23:13 +00:00
parent 896c86a3e4
commit d526735366
3 changed files with 47 additions and 13 deletions

View File

@ -185,6 +185,16 @@ let timerInterval = null;
let seconds = 0; let seconds = 0;
let userName = ''; let userName = '';
let answered = false; let answered = false;
let shuffledQuestions = [];
function shuffleArray(array) {
const shuffled = [...array];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
}
function checkLogin() { function checkLogin() {
const savedName = localStorage.getItem('ebtUserName'); const savedName = localStorage.getItem('ebtUserName');
@ -253,13 +263,14 @@ function startQuiz() {
currentQuestion = 0; currentQuestion = 0;
answers = {}; answers = {};
seconds = 0; seconds = 0;
shuffledQuestions = shuffleArray(QUESTIONS);
startTimer(); startTimer();
document.getElementById('q-total').textContent = QUESTIONS.length; document.getElementById('q-total').textContent = shuffledQuestions.length;
showQuestion(); showQuestion();
} }
function showQuestion() { function showQuestion() {
const q = QUESTIONS[currentQuestion]; const q = shuffledQuestions[currentQuestion];
document.getElementById('q-current').textContent = currentQuestion + 1; document.getElementById('q-current').textContent = currentQuestion + 1;
document.getElementById('question-text').textContent = q.q; document.getElementById('question-text').textContent = q.q;
@ -347,7 +358,7 @@ function showResults() {
let correct = 0; let correct = 0;
let wrong = 0; let wrong = 0;
QUESTIONS.forEach((q, i) => { shuffledQuestions.forEach((q, i) => {
if (answers[i] === q.correct) { if (answers[i] === q.correct) {
correct++; correct++;
} else { } else {
@ -355,7 +366,7 @@ function showResults() {
} }
}); });
const percentage = Math.round((correct / QUESTIONS.length) * 100); const percentage = Math.round((correct / shuffledQuestions.length) * 100);
document.getElementById('quiz-section').classList.add('hidden'); document.getElementById('quiz-section').classList.add('hidden');
document.getElementById('result-section').classList.remove('hidden'); document.getElementById('result-section').classList.remove('hidden');

View File

@ -176,6 +176,16 @@ let timerInterval = null;
let seconds = 0; let seconds = 0;
let userName = ''; let userName = '';
let answered = false; let answered = false;
let shuffledQuestions = [];
function shuffleArray(array) {
const shuffled = [...array];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
}
function checkLogin() { function checkLogin() {
const savedName = localStorage.getItem('ebtUserName'); const savedName = localStorage.getItem('ebtUserName');
@ -244,13 +254,14 @@ function startQuiz() {
currentQuestion = 0; currentQuestion = 0;
answers = {}; answers = {};
seconds = 0; seconds = 0;
shuffledQuestions = shuffleArray(QUESTIONS);
startTimer(); startTimer();
document.getElementById('q-total').textContent = QUESTIONS.length; document.getElementById('q-total').textContent = shuffledQuestions.length;
showQuestion(); showQuestion();
} }
function showQuestion() { function showQuestion() {
const q = QUESTIONS[currentQuestion]; const q = shuffledQuestions[currentQuestion];
document.getElementById('q-current').textContent = currentQuestion + 1; document.getElementById('q-current').textContent = currentQuestion + 1;
document.getElementById('question-text').textContent = q.q; document.getElementById('question-text').textContent = q.q;
@ -338,15 +349,16 @@ function showResults() {
let correct = 0; let correct = 0;
let wrong = 0; let wrong = 0;
QUESTIONS.forEach((q, i) => { shuffledQuestions.forEach((q, i) => {
if (answers[i] === q.correct) { const originalIndex = QUESTIONS.findIndex(orig => orig.q === q.q);
if (answers[i] === originalIndex) {
correct++; correct++;
} else { } else {
wrong++; wrong++;
} }
}); });
const percentage = Math.round((correct / QUESTIONS.length) * 100); const percentage = Math.round((correct / shuffledQuestions.length) * 100);
document.getElementById('quiz-section').classList.add('hidden'); document.getElementById('quiz-section').classList.add('hidden');
document.getElementById('result-section').classList.remove('hidden'); document.getElementById('result-section').classList.remove('hidden');

View File

@ -179,6 +179,16 @@ let timerInterval = null;
let seconds = 0; let seconds = 0;
let userName = ''; let userName = '';
let answered = false; let answered = false;
let shuffledQuestions = [];
function shuffleArray(array) {
const shuffled = [...array];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
}
function checkLogin() { function checkLogin() {
const savedName = localStorage.getItem('ebtUserName'); const savedName = localStorage.getItem('ebtUserName');
@ -247,13 +257,14 @@ function startQuiz() {
currentQuestion = 0; currentQuestion = 0;
answers = {}; answers = {};
seconds = 0; seconds = 0;
shuffledQuestions = shuffleArray(QUESTIONS);
startTimer(); startTimer();
document.getElementById('q-total').textContent = QUESTIONS.length; document.getElementById('q-total').textContent = shuffledQuestions.length;
showQuestion(); showQuestion();
} }
function showQuestion() { function showQuestion() {
const q = QUESTIONS[currentQuestion]; const q = shuffledQuestions[currentQuestion];
document.getElementById('q-current').textContent = currentQuestion + 1; document.getElementById('q-current').textContent = currentQuestion + 1;
document.getElementById('question-text').textContent = q.q; document.getElementById('question-text').textContent = q.q;
@ -341,7 +352,7 @@ function showResults() {
let correct = 0; let correct = 0;
let wrong = 0; let wrong = 0;
QUESTIONS.forEach((q, i) => { shuffledQuestions.forEach((q, i) => {
if (answers[i] === q.correct) { if (answers[i] === q.correct) {
correct++; correct++;
} else { } else {
@ -349,7 +360,7 @@ function showResults() {
} }
}); });
const percentage = Math.round((correct / QUESTIONS.length) * 100); const percentage = Math.round((correct / shuffledQuestions.length) * 100);
document.getElementById('quiz-section').classList.add('hidden'); document.getElementById('quiz-section').classList.add('hidden');
document.getElementById('result-section').classList.remove('hidden'); document.getElementById('result-section').classList.remove('hidden');