File size: 3,795 Bytes
1677ecc 57819f8 1677ecc 57819f8 1677ecc 57819f8 1677ecc 57819f8 1677ecc 57819f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
let questions = [];
let currentIndex = 0;
let userAnswers = [];
let startTime;
document.getElementById('start-session').addEventListener('click', async () => {
const file = document.getElementById('file').value;
if (!file) return alert("Please select a file");
const response = await fetch('/load_questions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ file_name: file })
});
questions = await response.json();
userAnswers = Array(questions.length).fill([]);
startQuiz();
});
function startQuiz() {
document.getElementById('file-selection').style.display = 'none';
document.getElementById('quiz-container').style.display = 'block';
startTime = Date.now();
displayQuestion();
setInterval(updateTimer, 1000);
}
function displayQuestion() {
const question = questions[currentIndex];
document.getElementById('question').innerText = question.question;
const isMultipleChoice = /Select TWO|Choose two/i.test(question.question);
const optionsDiv = document.getElementById('options');
optionsDiv.innerHTML = '';
const ul = document.createElement('ul');
ul.style.listStyleType = 'disc';
question.options.forEach((option, i) => {
const li = document.createElement('li');
li.style.cursor = 'pointer';
li.style.marginBottom = '10px';
li.className = userAnswers[currentIndex].includes(i) ? 'selected' : '';
li.innerText = option;
li.addEventListener('click', () => {
if (isMultipleChoice) {
// Allow multiple selections
const currentAnswers = userAnswers[currentIndex];
if (currentAnswers.includes(i)) {
userAnswers[currentIndex] = currentAnswers.filter(j => j !== i);
} else {
userAnswers[currentIndex].push(i);
}
} else {
// Allow only one selection
userAnswers[currentIndex] = [i];
}
displayQuestion();
});
ul.appendChild(li);
});
optionsDiv.appendChild(ul);
}
function selectAnswer(optionIndex) {
const currentAnswers = userAnswers[currentIndex];
if (currentAnswers.includes(optionIndex)) {
userAnswers[currentIndex] = currentAnswers.filter(i => i !== optionIndex);
} else {
userAnswers[currentIndex].push(optionIndex);
}
displayQuestion();
}
document.getElementById('next').addEventListener('click', () => {
currentIndex = Math.min(currentIndex + 1, questions.length - 1);
displayQuestion();
});
document.getElementById('prev').addEventListener('click', () => {
currentIndex = Math.max(currentIndex - 1, 0);
displayQuestion();
});
document.getElementById('end-session').addEventListener('click', async () => {
const response = await fetch('/submit_results', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ questions, user_answers: userAnswers })
});
const result = await response.json();
document.getElementById('quiz-container').style.display = 'none';
document.getElementById('results-container').style.display = 'block';
document.getElementById('score').innerText = `Your score: ${result.score.toFixed(2)}%`;
});
function updateTimer() {
const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
const hours = String(Math.floor(elapsedTime / 3600)).padStart(2, '0');
const minutes = String(Math.floor((elapsedTime % 3600) / 60)).padStart(2, '0');
const seconds = String(elapsedTime % 60).padStart(2, '0');
document.getElementById('timer').innerText = `Time: ${hours}:${minutes}:${seconds}`;
} |