`;
quizContainer.appendChild(qaDiv);
// Insert the ad div after the second question (index 1)
if (index === 1) {
// Create the div element
const adDiv = document.createElement('div');
adDiv.className = 'inserted-div';
adDiv.className = 'QA';
// Create the script element
const adScript = document.createElement('script');
adScript.async = true;
adScript.id = 'PS_67ebd889ca6f1325b65a5242';
adScript.type = 'text/javascript';
adScript.src = 'https://ads.playstream.media/api/adserver/scripts?PS_TAGID=67ebd889ca6f1325b65a5242&PS_PUB_ID=67ebd5e3ca6f1325b65a46fe';
// Append script to div, and div to container
adDiv.appendChild(adScript);
quizContainer.appendChild(adDiv);
}
});
// Add event listeners for clicking on option items (since radio buttons are hidden)
document.querySelectorAll('.option-item').forEach(item => {
item.addEventListener('click', function() {
// Get the radio input and question index
const radio = this.querySelector('input[type="radio"]');
if (radio) {
// Clear previous selections in this question
const questionOptions = document.querySelectorAll(`input[name="${radio.name}"]`);
questionOptions.forEach(option => {
option.checked = false;
option.closest('li').classList.remove('selected');
});
// Select this option
radio.checked = true;
this.classList.add('selected');
// Mark as user selected
radio.setAttribute('data-user-selected', 'true');
// Get the question index from the radio button name
const questionIndex = radio.name.replace('q', '');
// Hide the explanation if it's visible (user is making a new selection)
const explanationDiv = document.getElementById(`explanation${questionIndex}`);
const buttonSpan = document.querySelector(`.B button[onclick="showQuizAnswer(${questionIndex})"] span`);
if (explanationDiv && explanationDiv.style.display === "block") {
explanationDiv.style.display = "none";
if (buttonSpan) {
buttonSpan.textContent = "Show Answer";
}
// Remove highlight background from all options in this question
questionOptions.forEach(opt => {
const optItem = opt.closest('li');
if (optItem) {
optItem.style.backgroundColor = "";
}
});
}
}
});
});
}
function showQuizAnswer(index) {
const explanationDiv = document.getElementById(`explanation${index}`);
const buttonSpan = document.querySelector(`.B button[onclick="showQuizAnswer(${index})"] span`);
// Check if the explanation is already visible
if (explanationDiv.style.display === "block") {
// If visible, hide it and change button text to "Show Answer"
explanationDiv.style.display = "none";
if (buttonSpan) {
buttonSpan.textContent = "Show Answer";
}
// Remove the yellow background from all options
const allOptions = document.querySelectorAll(`input[name="q${index}"]`);
allOptions.forEach(option => {
const optionItem = option.closest('li');
if (optionItem) {
optionItem.style.backgroundColor = "";
}
});
} else {
// If hidden, show it and change button text to "Hide Answer"
const correctAnswerIndex = quizData[index].answer;
const optionLabels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
const correctOptionLetter = optionLabels[correctAnswerIndex];
explanationDiv.innerHTML = `Answer (${correctOptionLetter}): ${quizData[index].options[correctAnswerIndex]} ${quizData[index].explanation}`;
explanationDiv.style.display = "block";
if (buttonSpan) {
buttonSpan.textContent = "Hide Answer";
}
// Find the correct option and highlight it with yellow background
const correctRadio = document.querySelector(`input[name="q${index}"][value="${correctAnswerIndex}"]`);
if (correctRadio) {
// Find the parent list item and highlight it
const correctOption = correctRadio.closest('li');
if (correctOption) {
correctOption.style.backgroundColor = "#fff9b7";
}
}
}
}
function submitQuiz() {
let score = 0, attempted = 0;
quizData.forEach((q, index) => {
// Since we've hidden the radio buttons, we need to check their state
const selectedOption = document.querySelector(`input[name="q${index}"]:checked`);
if (selectedOption) {
attempted++;
if (parseInt(selectedOption.value) === q.answer) score++;
}
});
if (attempted < Math.ceil(quizData.length / 2)) {
alert(`Please attempt at least ${Math.ceil(quizData.length / 2)} questions before submitting.`);
return;
}
document.getElementById("result").innerText = `You scored ${score} out of ${quizData.length}!`;
document.querySelector(".review-btn").disabled = false;
document.querySelector(".review-btn").classList.add("enabled");
document.querySelector(".result-container").style.display = "block";
// Destroy any existing chart to prevent duplicate rendering
if (window.quizResultChart) {
window.quizResultChart.destroy();
}
// Create new chart and store reference
window.quizResultChart = new Chart(document.getElementById("resultChart"), {
type: 'pie',
data: {
labels: ["Correct", "Incorrect"],
datasets: [{
data: [score, quizData.length - score],
backgroundColor: ["#28a745", "#dc3545"]
}]
}
});
}
function reviewQuiz() {
quizData.forEach((q, index) => {
showQuizAnswer(index);
});
document.querySelector(".review-btn").disabled = true;
}
loadQuiz();