[Modify/Add] Improved practice exam layout and turned it into an interactive test experience.

This commit is contained in:
Kanani Nirav
2025-01-09 10:45:44 +09:00
parent 428aa3dafe
commit bb0244aad7
26 changed files with 252 additions and 209 deletions

65
assets/css/exam.css Normal file
View File

@@ -0,0 +1,65 @@
ol {
padding-left: 20px;
}
li {
margin-bottom: 20px;
}
ul {
list-style-type: none;
padding: 0;
}
ul li {
margin: 5px 0;
}
/* Checkbox Styling */
input[type="checkbox"] {
accent-color: #4caf50; /* Green color for checkbox */
margin-right: 10px;
transform: scale(1.2); /* Slightly larger checkboxes */
}
/* Button Styling */
button {
background-color: #4caf50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
display: block;
margin: 20px auto;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
/* Result Display Styling */
#result {
text-align: center;
font-size: 18px;
margin-top: 20px;
}
#result h2 {
font-size: 24px;
color: #4caf50;
}
#result .percentage {
font-size: 20px;
color: #ff9800;
}
.incorrect {
background-color: #f8d7da;
border: 1px solid #f5c2c7;
color: #842029;
}

55
assets/js/exam.js Normal file
View File

@@ -0,0 +1,55 @@
document.addEventListener('DOMContentLoaded', () => {
// Dynamically add checkboxes to the list items
document.querySelectorAll('ol > li').forEach((question, index) => {
const options = question.querySelectorAll('ul li');
options.forEach(option => {
const optionText = option.textContent.trim();
if (/^[A-Z]\.\s/.test(optionText)) {
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.name = `question-${index}`; // Unique identifier for each question
checkbox.value = optionText.charAt(0); // Use the letter as the value
option.prepend(checkbox);
}
});
});
});
function submitQuiz() {
let score = 0;
let totalQuestions = 0;
document.querySelectorAll('ol > li').forEach((question, index) => {
const correctAnswerText = question.querySelector('details p').textContent.match(/Correct answer:\s([A-Z, ]+)/i);
if (!correctAnswerText) return; // Skip if no correct answers are found
const correctAnswers = correctAnswerText[1]
.replace(/\s+/g, '') // Remove spaces
.split(/,|(?=[A-Z])/); // Split on commas or consecutive uppercase letters
const selectedOptions = Array.from(
document.querySelectorAll(`input[name="question-${index}"]:checked`)
).map(input => input.value);
if (correctAnswers.length > 0) {
totalQuestions++;
}
const isCorrect =
selectedOptions.length === correctAnswers.length &&
correctAnswers.every(answer => selectedOptions.includes(answer));
if (isCorrect) {
score++;
question.classList.remove('incorrect'); // Remove incorrect class if correct
} else {
question.classList.add('incorrect'); // Highlight incorrect questions
}
});
const percentage = totalQuestions > 0 ? ((score / totalQuestions) * 100).toFixed(2) : 0;
document.getElementById('score').innerText = `${score} / ${totalQuestions}`;
document.getElementById('percentage').innerText = percentage;
document.getElementById('result').style.display = 'block';
}