add random shuffle for questions on each login
This commit is contained in:
parent
896c86a3e4
commit
d526735366
@ -185,6 +185,16 @@ let timerInterval = null;
|
||||
let seconds = 0;
|
||||
let userName = '';
|
||||
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() {
|
||||
const savedName = localStorage.getItem('ebtUserName');
|
||||
@ -253,13 +263,14 @@ function startQuiz() {
|
||||
currentQuestion = 0;
|
||||
answers = {};
|
||||
seconds = 0;
|
||||
shuffledQuestions = shuffleArray(QUESTIONS);
|
||||
startTimer();
|
||||
document.getElementById('q-total').textContent = QUESTIONS.length;
|
||||
document.getElementById('q-total').textContent = shuffledQuestions.length;
|
||||
showQuestion();
|
||||
}
|
||||
|
||||
function showQuestion() {
|
||||
const q = QUESTIONS[currentQuestion];
|
||||
const q = shuffledQuestions[currentQuestion];
|
||||
document.getElementById('q-current').textContent = currentQuestion + 1;
|
||||
document.getElementById('question-text').textContent = q.q;
|
||||
|
||||
@ -347,7 +358,7 @@ function showResults() {
|
||||
|
||||
let correct = 0;
|
||||
let wrong = 0;
|
||||
QUESTIONS.forEach((q, i) => {
|
||||
shuffledQuestions.forEach((q, i) => {
|
||||
if (answers[i] === q.correct) {
|
||||
correct++;
|
||||
} 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('result-section').classList.remove('hidden');
|
||||
|
||||
22
index.html
22
index.html
@ -176,6 +176,16 @@ let timerInterval = null;
|
||||
let seconds = 0;
|
||||
let userName = '';
|
||||
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() {
|
||||
const savedName = localStorage.getItem('ebtUserName');
|
||||
@ -244,13 +254,14 @@ function startQuiz() {
|
||||
currentQuestion = 0;
|
||||
answers = {};
|
||||
seconds = 0;
|
||||
shuffledQuestions = shuffleArray(QUESTIONS);
|
||||
startTimer();
|
||||
document.getElementById('q-total').textContent = QUESTIONS.length;
|
||||
document.getElementById('q-total').textContent = shuffledQuestions.length;
|
||||
showQuestion();
|
||||
}
|
||||
|
||||
function showQuestion() {
|
||||
const q = QUESTIONS[currentQuestion];
|
||||
const q = shuffledQuestions[currentQuestion];
|
||||
document.getElementById('q-current').textContent = currentQuestion + 1;
|
||||
document.getElementById('question-text').textContent = q.q;
|
||||
|
||||
@ -338,15 +349,16 @@ function showResults() {
|
||||
|
||||
let correct = 0;
|
||||
let wrong = 0;
|
||||
QUESTIONS.forEach((q, i) => {
|
||||
if (answers[i] === q.correct) {
|
||||
shuffledQuestions.forEach((q, i) => {
|
||||
const originalIndex = QUESTIONS.findIndex(orig => orig.q === q.q);
|
||||
if (answers[i] === originalIndex) {
|
||||
correct++;
|
||||
} else {
|
||||
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('result-section').classList.remove('hidden');
|
||||
|
||||
@ -179,6 +179,16 @@ let timerInterval = null;
|
||||
let seconds = 0;
|
||||
let userName = '';
|
||||
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() {
|
||||
const savedName = localStorage.getItem('ebtUserName');
|
||||
@ -247,13 +257,14 @@ function startQuiz() {
|
||||
currentQuestion = 0;
|
||||
answers = {};
|
||||
seconds = 0;
|
||||
shuffledQuestions = shuffleArray(QUESTIONS);
|
||||
startTimer();
|
||||
document.getElementById('q-total').textContent = QUESTIONS.length;
|
||||
document.getElementById('q-total').textContent = shuffledQuestions.length;
|
||||
showQuestion();
|
||||
}
|
||||
|
||||
function showQuestion() {
|
||||
const q = QUESTIONS[currentQuestion];
|
||||
const q = shuffledQuestions[currentQuestion];
|
||||
document.getElementById('q-current').textContent = currentQuestion + 1;
|
||||
document.getElementById('question-text').textContent = q.q;
|
||||
|
||||
@ -341,7 +352,7 @@ function showResults() {
|
||||
|
||||
let correct = 0;
|
||||
let wrong = 0;
|
||||
QUESTIONS.forEach((q, i) => {
|
||||
shuffledQuestions.forEach((q, i) => {
|
||||
if (answers[i] === q.correct) {
|
||||
correct++;
|
||||
} 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('result-section').classList.remove('hidden');
|
||||
|
||||
Loading…
Reference in New Issue
Block a user