c'est moche mais c'est mieux

php
Anthony RICHARD 1 year ago
parent 9aa4ab4676
commit eea9ddcd78

@ -1,38 +1,23 @@
var currentQuestion = 0;
var score = 0;
function validateAndNext(correct) {
if (correct) score++;
nextQuestion();
}
function nextQuestion() {
var currentQuestionDiv = document.getElementById("question" + currentQuestion);
currentQuestionDiv.style.display = "none";
currentQuestion++;
if (currentQuestion < len) {
var nextQuestionDiv = document.getElementById("question" + currentQuestion);
nextQuestionDiv.style.display = "block";
} else {
// Toutes les questions ont été posées, afficher le score
alert("Quiz terminé. Votre score est de " + score + "/" + len);
}
}
function validateAndNext() {
var currentForm = document.getElementById("quizForm" + currentQuestion);
var selectedAnswer = currentForm.querySelector('input[name="answer' + currentQuestion + '"]:checked');
if (selectedAnswer) {
// L'utilisateur a sélectionné une réponse
if (selectedAnswer.classList.contains("correct")) {
// C'est la bonne réponse, augmenter le score
score++;
}
// Passer à la question suivante
nextQuestion();
} else {
// Aucune réponse sélectionnée, afficher un message d'erreur
alert("Veuillez sélectionner une réponse avant de passer à la question suivante.");
}
else alert("Quiz terminé. Votre score est de " + score + "/" + len);
}
window.onload = function () {

@ -31,22 +31,19 @@
<form id="quizForm{{ i }}" method="post">
{% for answer in answers[i] %}
{% if answer == goodAnswers[i] %}
<input type="radio" name="answer{{ i }}" value="{{ answer }}" class="correct"> {{ answer }}<br>
<input type="button" value={{ answer }} onclick="validateAndNext(true)">
{% else %}
<input type="radio" name="answer{{ i }}" value="{{ answer }}"> {{ answer }}<br>
<input type="button" value={{ answer }} onclick="validateAndNext(false)">
{% endif %}
{% endfor %}
</form>
</div>
</div>
{% endfor %}
<input type="button" value="Suivant" onclick="validateAndNext()">
{% endif %}
</div>
<script>
var len = JSON.parse('{{ questions | length }}');
</script>
<script> var len = JSON.parse('{{ questions | length }}'); </script>
<script src="{{base}}/js/quiz.js"></script>
</section>

Loading…
Cancel
Save