-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhquiz.js
151 lines (133 loc) · 5.06 KB
/
hquiz.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Quiz questions and answers
const quizData = [
{
question: "What does a model of the heart help you visualize?",
options: ["The external structure only", "The internal and external structures", "Only the blood vessels", "Only the heart valves"],
correct: 1
},
{
question: "Which part of a heart model would you find the septum?",
options: ["Between the atria and ventricles", "In the left atrium", "In the right ventricle", "On the outer wall"],
correct: 0
},
{
question: "When looking at a model of the heart, which chambers are on the top?",
options: ["Ventricles", "Atria", "Septa", "Valves"],
correct: 1
},
{
question: "In a heart model, the aorta is usually depicted as:",
options: ["A vein", "A capillary", "A large artery", "A small artery"],
correct: 2
},
{
question: "Which color is typically used to represent deoxygenated blood in heart models?",
options: ["Red", "Blue", "Green", "Yellow"],
correct: 1
},
{
question: "What structure is shown as a \"door\" in a model of the heart, ensuring blood flows in the right direction?",
options: ["Atrium", "Valve", "Ventricle", "Septum"],
correct: 1
},
{
question: "In a model of the heart, the pulmonary artery carries blood to:",
options: ["The liver", "The lungs", "The brain", "The kidneys"],
correct: 1
},
{
question: "Which part of a heart model demonstrates the exchange of oxygen and carbon dioxide?",
options: ["Coronary artery", "Pulmonary veins", "Aorta", "Capillaries in the lungs"],
correct: 3
},
{
question: "How can a model of the heart help students understand cardiovascular diseases?",
options: ["By showing external symptoms", "By demonstrating internal abnormalities and flow of blood", "By teaching surgical techniques", "By diagnosing diseases"],
correct: 1
},
{
question: "What educational advantage does a heart model provide over 2D diagrams?",
options: ["Simplicity", "Better spatial understanding and visualization of structures", "Less information", "Limited views"],
correct: 1
}
];
let currentQuestion = 0;
let score = 0;
let userAnswers = new Array(quizData.length).fill(null);
// DOM elements
const questionEl = document.getElementById('question');
const optionsEl = document.getElementById('options');
const prevBtn = document.getElementById('prev-btn');
const nextBtn = document.getElementById('next-btn');
const submitBtn = document.getElementById('submit-btn');
const resultsEl = document.getElementById('results');
const scoreEl = document.getElementById('score');
const totalEl = document.getElementById('total');
const progressBar = document.querySelector('.progress');
// Initialize quiz
function loadQuestion() {
const question = quizData[currentQuestion];
questionEl.textContent = question.question;
optionsEl.innerHTML = '';
question.options.forEach((option, index) => {
const button = document.createElement('button');
button.className = 'option';
button.textContent = option;
if (userAnswers[currentQuestion] === index) {
button.classList.add('selected');
}
button.addEventListener('click', () => selectOption(index));
optionsEl.appendChild(button);
});
updateNavButtons();
updateProgressBar();
}
// Handle option selection
function selectOption(index) {
userAnswers[currentQuestion] = index;
const options = optionsEl.getElementsByClassName('option');
for (let option of options) {
option.classList.remove('selected');
}
options[index].classList.add('selected');
updateNavButtons();
}
// Navigation and UI updates
function updateNavButtons() {
prevBtn.disabled = currentQuestion === 0;
nextBtn.style.display = currentQuestion === quizData.length - 1 ? 'none' : 'block';
submitBtn.style.display = currentQuestion === quizData.length - 1 ? 'block' : 'none';
}
function updateProgressBar() {
const progress = ((currentQuestion + 1) / quizData.length) * 100;
progressBar.style.width = `${progress}%`;
}
// Event listeners
prevBtn.addEventListener('click', () => {
if (currentQuestion > 0) {
currentQuestion--;
loadQuestion();
}
});
nextBtn.addEventListener('click', () => {
if (currentQuestion < quizData.length - 1) {
currentQuestion++;
loadQuestion();
}
});
submitBtn.addEventListener('click', () => {
// Calculate score
score = userAnswers.reduce((total, answer, index) => {
return total + (answer === quizData[index].correct ? 1 : 0);
}, 0);
// Display results
scoreEl.textContent = score;
totalEl.textContent = quizData.length;
resultsEl.style.display = 'block';
// Disable all interactions
optionsEl.querySelectorAll('button').forEach(btn => btn.disabled = true);
submitBtn.disabled = true;
prevBtn.disabled = true;
});
// Start the quiz
loadQuestion();