Fausto Busuito
commited on
Commit
·
242f01a
1
Parent(s):
06a56b9
Application changes
Browse files- app/static/script.js +70 -49
app/static/script.js
CHANGED
|
@@ -17,63 +17,84 @@ document.getElementById('start-session').addEventListener('click', async () => {
|
|
| 17 |
userAnswers = Array(questions.length).fill([]);
|
| 18 |
startQuiz();
|
| 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 |
if (input.checked) {
|
| 53 |
-
|
| 54 |
} else {
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
}
|
| 60 |
-
});
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
form.appendChild(label);
|
| 69 |
-
});
|
| 70 |
|
| 71 |
-
|
| 72 |
}
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
|
| 78 |
function selectAnswer(optionIndex) {
|
| 79 |
const currentAnswers = userAnswers[currentIndex];
|
|
|
|
| 17 |
userAnswers = Array(questions.length).fill([]);
|
| 18 |
startQuiz();
|
| 19 |
});
|
| 20 |
+
function renderQuestion(question, index) {
|
| 21 |
+
const questionContainer = document.getElementById('question-container');
|
| 22 |
+
questionContainer.innerHTML = '';
|
| 23 |
+
|
| 24 |
+
const questionText = document.createElement('p');
|
| 25 |
+
questionText.innerText = `${index + 1}. ${question.question}`;
|
| 26 |
+
questionContainer.appendChild(questionText);
|
| 27 |
+
|
| 28 |
+
const optionsContainer = document.createElement('div');
|
| 29 |
+
|
| 30 |
+
// Se la domanda permette solo una risposta, usa i radio button
|
| 31 |
+
if (question.correct.length === 1) {
|
| 32 |
+
question.options.forEach((option, idx) => {
|
| 33 |
+
const label = document.createElement('label');
|
| 34 |
+
const input = document.createElement('input');
|
| 35 |
+
input.type = 'radio';
|
| 36 |
+
input.name = `question-${index}`; // Nome unico per ogni domanda
|
| 37 |
+
input.value = idx;
|
| 38 |
+
if (userAnswers[index] === idx) input.checked = true; // Pre-seleziona la risposta dell'utente se presente
|
| 39 |
+
input.addEventListener('change', () => {
|
| 40 |
+
userAnswers[index] = [idx]; // Memorizza la risposta
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
label.appendChild(input);
|
| 44 |
+
label.appendChild(document.createTextNode(option));
|
| 45 |
+
optionsContainer.appendChild(label);
|
| 46 |
+
});
|
| 47 |
+
} else {
|
| 48 |
+
// Se la domanda permette risposte multiple, usa le checkbox
|
| 49 |
+
question.options.forEach((option, idx) => {
|
| 50 |
+
const label = document.createElement('label');
|
| 51 |
+
const input = document.createElement('input');
|
| 52 |
+
input.type = 'checkbox';
|
| 53 |
+
input.name = `question-${index}`; // Nome unico per ogni domanda
|
| 54 |
+
input.value = idx;
|
| 55 |
+
if (userAnswers[index] && userAnswers[index].includes(idx)) input.checked = true; // Pre-seleziona le risposte dell'utente se presenti
|
| 56 |
+
input.addEventListener('change', () => {
|
| 57 |
+
const answerIndex = userAnswers[index] || [];
|
| 58 |
if (input.checked) {
|
| 59 |
+
answerIndex.push(idx);
|
| 60 |
} else {
|
| 61 |
+
const answerIdx = answerIndex.indexOf(idx);
|
| 62 |
+
if (answerIdx > -1) {
|
| 63 |
+
answerIndex.splice(answerIdx, 1);
|
| 64 |
+
}
|
| 65 |
}
|
| 66 |
+
userAnswers[index] = answerIndex; // Memorizza la risposta
|
| 67 |
+
});
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
label.appendChild(input);
|
| 70 |
+
label.appendChild(document.createTextNode(option));
|
| 71 |
+
optionsContainer.appendChild(label);
|
| 72 |
+
});
|
| 73 |
+
}
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
questionContainer.appendChild(optionsContainer);
|
| 76 |
}
|
| 77 |
|
| 78 |
+
// Funzione per navigare tra le domande
|
| 79 |
+
function showQuestion(index) {
|
| 80 |
+
const question = questions[index];
|
| 81 |
+
renderQuestion(question, index);
|
| 82 |
+
|
| 83 |
+
// Imposta i pulsanti di navigazione
|
| 84 |
+
const prevButton = document.getElementById('prev-button');
|
| 85 |
+
const nextButton = document.getElementById('next-button');
|
| 86 |
+
|
| 87 |
+
prevButton.disabled = index === 0;
|
| 88 |
+
nextButton.disabled = index === questions.length - 1;
|
| 89 |
+
|
| 90 |
+
prevButton.onclick = () => showQuestion(index - 1);
|
| 91 |
+
nextButton.onclick = () => showQuestion(index + 1);
|
| 92 |
+
}
|
| 93 |
|
| 94 |
+
// Avvio del quiz
|
| 95 |
+
function startQuiz() {
|
| 96 |
+
showQuestion(0); // Inizia con la prima domanda
|
| 97 |
+
}
|
| 98 |
|
| 99 |
function selectAnswer(optionIndex) {
|
| 100 |
const currentAnswers = userAnswers[currentIndex];
|