`;
});
quizForm.innerHTML = quizHtml;
}
// --- Core Calculation Logic ---
function calculateScore() {
let totalScore = 0;
let unanswered = false;
for (let i = 0; i < quizQuestions.length; i++) {
const qNum = i + 1;
const question = quizQuestions[i];
const element = document.querySelector(`input[name="seq-q${qNum}"]:checked`);
if (!element) {
unanswered = true;
break;
}
validationError.style.display = 'none';
const rawValue = parseInt(element.value);
if (question.positive) {
// For positive items: SA=3, A=2, D=1, SD=0
totalScore += rawValue;
} else {
// For negative items (reverse-scored): SA=0, A=1, D=2, SD=3
totalScore += (3 - rawValue);
}
}
if (unanswered) {
validationError.style.display = 'block';
resultsContainer.style.display = 'none';
} else {
displayResults(totalScore);
}
}
// --- Display Results ---
function displayResults(score) {
let interpretation = "";
let description = "";
let className = "";
if (score >= 26) {
interpretation = "High Self-Esteem";
className = "seq-interpretation-high";
description = "Your score indicates high self-esteem. You generally hold a very favorable opinion of yourself, value your worth, and maintain a strong sense of self-respect.";
} else if (score >= 15) {
interpretation = "Healthy / Average Self-Esteem";
className = "seq-interpretation-medium";
description = "Your score falls within the normal, healthy range. This suggests you have a generally positive view of yourself, though you may experience occasional self-doubt, which is common.";
} else {
interpretation = "Low Self-Esteem";
className = "seq-interpretation-low";
description = "Your score suggests low self-esteem. This may mean you often experience self-doubt, self-criticism, and feelings of inadequacy. It may be beneficial to explore strategies to build a more positive self-image.";
}
// Store for PDF
quizResult = {
score: score,
interpretation: interpretation,
description: description
};
// Update DOM
scoreDisplay.textContent = `Your Score: ${score} / 30`;
interpretationDisplay.textContent = interpretation;
interpretationDisplay.className = className;
descriptionDisplay.textContent = description;
resultsContainer.style.display = 'block';
resultsContainer.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
// --- PDF Download Logic ---
function downloadPDF() {
if (!quizResult.score) {
alert("Please calculate your score first.");
return;
}
const { score, interpretation, description } = quizResult;
const doc = new jsPDF();
const pageWidth = doc.internal.pageSize.getWidth();
const margin = 15;
const contentWidth = pageWidth - (margin * 2);
// Title
doc.setFontSize(20);
doc.setFont("helvetica", "bold");
doc.text("Self-Esteem Quiz Results", pageWidth / 2, 25, { align: "center" });
// Score
doc.setFontSize(16);
doc.setFont("helvetica", "bold");
doc.text(`Your Score: ${score} / 30`, margin, 45);
// Interpretation
doc.setFontSize(14);
doc.setFont("helvetica", "bold");
// Set color based on result
if (score >= 26) {
doc.setTextColor(67, 160, 71); // Green
} else if (score >= 15) {
doc.setTextColor(30, 136, 229); // Blue
} else {
doc.setTextColor(229, 57, 53); // Red
}
doc.text(`Interpretation: ${interpretation}`, margin, 58);
// Description
doc.setFontSize(11);
doc.setFont("helvetica", "normal");
doc.setTextColor(40, 40, 40); // Reset to dark grey
const descriptionLines = doc.splitTextToSize(description, contentWidth);
doc.text(descriptionLines, margin, 70);
// Footer line
const finalY = doc.autoTable.previous ? doc.autoTable.previous.finalY : 85; // Get Y pos
doc.setDrawColor(200, 200, 200);
doc.line(margin, finalY + 10, pageWidth - margin, finalY + 10);
doc.setFontSize(9);
doc.setTextColor(150, 150, 150);
doc.text("Quiz based on the Rosenberg Self-Esteem Scale.", margin, finalY + 15);
doc.save("Self-Esteem-Quiz-Results.pdf");
}
// --- Event Listeners ---
if (calculateBtn) {
calculateBtn.addEventListener('click', calculateScore);
}
if (pdfBtn) {
pdfBtn.addEventListener('click', downloadPDF);
}
// --- Initial Setup ---
buildQuiz();
});
