-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (61 loc) · 2.59 KB
/
Copy pathscript.js
File metadata and controls
72 lines (61 loc) · 2.59 KB
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
let score = 0;
let correctAnswers = 0;
let wrongAnswers = 0;
let currentQuestionIndex = 0;
const questions = document.querySelectorAll(".quiz");
const submitButtons = document.querySelectorAll(".submit-btn");
// Function to show the current question
function showQuestion(index) {
questions.forEach((question, idx) => {
question.classList.toggle("active", idx === index);
});
}
// Function to handle the submission
submitButtons.forEach((button, idx) => {
button.addEventListener("click", function () {
const selectedOption = questions[idx].querySelector('input[type="radio"]:checked');
if (selectedOption) {
// Disable all options for the current question
const inputAns = questions[idx].querySelectorAll("input[type='radio']");
inputAns.forEach((input) => {
input.disabled = true;
});
// Check if the selected answer is correct
let ansBg = selectedOption.parentElement;
if (selectedOption.value === "true") {
ansBg.style.backgroundColor = "green";
score++;
correctAnswers++;
} else {
ansBg.style.backgroundColor = "red";
wrongAnswers++;
}
// Move to the next question after a short delay
setTimeout(() => {
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion(currentQuestionIndex);
} else {
showSummary(); // Show the summary after the last question
}
}, 1000);
} else {
alert("Please select an answer before submitting.");
}
});
});
// Function to display the summary
function showSummary() {
const summaryDiv = document.getElementById("quiz-summary");
document.getElementById("score").textContent = "Total Score: " + score;
document.getElementById("questions-attempted").textContent = "Questions Attempted: " + (correctAnswers + wrongAnswers);
document.getElementById("correct-questions").textContent = "Correct Questions: " + correctAnswers;
document.getElementById("wrong-questions").textContent = "Wrong Questions: " + wrongAnswers;
// Hide all questions and show the summary
questions.forEach((question) => {
question.style.display = "none";
});
summaryDiv.style.display = "block";
}
// Show the first question initially
showQuestion(currentQuestionIndex);