`;
shuffledOptions.forEach((option, index) => { // Use shuffledOptions here
questionContent += `
`;
});
questionContent += `
`;
questionContent += `
`;
quizAppContainer.innerHTML = questionContent;
// Scroll to the top of the quiz container when a new question is rendered
quizAppContainer.scrollIntoView({ behavior: 'smooth', block: 'start' });
// Start timer for the new question
startTimer(currentQuestion.timeLimit);
// Enable/disable buttons based on hasAnsweredCurrent state
updateQuizButtonsState();
updateScoreDisplay(); // Update score display after rendering
};
const updateScoreDisplay = () => {
const scoreDisplayElement = document.getElementById('score-display');
if (scoreDisplayElement) {
scoreDisplayElement.textContent = `Score: ${score}/${totalQuestions}`;
}
};
const updateQuizButtonsState = () => {
const currentQuestion = shuffledQuizData[currentQuestionIndex];
const hintButton = document.getElementById('hint-btn');
if (hintButton) {
if (hasAnsweredCurrent) {
hintButton.disabled = true;
hintButton.classList.add('opacity-30', 'cursor-not-allowed');
} else {
hintButton.disabled = false;
hintButton.classList.remove('opacity-30', 'cursor-not-allowed');
}
}
const optionButtons = quizAppContainer.querySelectorAll('.grid button');
optionButtons.forEach(button => {
if (hasAnsweredCurrent) {
button.disabled = true;
if (button.textContent === currentQuestion.answer) {
button.classList.add('btn-correct-answer');
button.classList.remove('btn-neutral');
} else {
button.classList.add('btn-incorrect-answer');
button.classList.remove('btn-neutral');
}
} else {
button.disabled = false;
button.classList.remove('btn-correct-answer', 'btn-incorrect-answer', 'line-through'); /* Removed opacity-30 */
button.classList.add('btn-neutral');
}
});
updateScoreDisplay(); // Ensure score is updated when buttons state changes
};
const renderTypeSelectionScreen = () => {
// Load state to check if a quiz was in progress
loadState();
// Ensure totalQuestions is correctly set based on the full QUIZ_DATA
const currentTotalQuestions = QUIZ_DATA.length;
let continueButtonHtml = '';
// Only show "Continue Last Quiz" if there's a saved index greater than 0 AND the quiz was not completed
if (currentQuestionIndex > 0 && !quizCompleted) {
continueButtonHtml = `
`;
}
quizAppContainer.innerHTML = `
${QUIZ_TITLE}
Embark on a fun and challenging multiple choice quiz!
${continueButtonHtml}
`;
// Scroll to the top of the quiz container when the type selection screen is rendered
quizAppContainer.scrollIntoView({ behavior: 'smooth', block: 'start' });
};
const startNewQuiz = () => {
clearState(); // Clear all previous state
quizType = 'multipleChoice';
shuffledQuizData = shuffleArray(QUIZ_DATA); // Shuffle all questions
currentQuestionIndex = 0; // Ensure start from 0
score = 0;
answeredQuestions = [];
quizCompleted = false;
saveState();
renderQuizScreen();
};
const chooseQuizType = (type) => { // This function is now effectively replaced by startNewQuiz for direct start
startNewQuiz(); // Redirect to startNewQuiz for a clean start
};
const continueLastQuiz = () => {
loadState();
shuffledQuizData = shuffleArray(QUIZ_DATA); // Re-shuffle to maintain consistency, but current index will be loaded
if (quizCompleted) {
renderCompletionScreen();
} else {
renderQuizScreen();
}
};
const renderCompletionScreen = () => {
stopTimer();
const percentage = totalQuestions > 0 ? ((score / totalQuestions) * 100).toFixed(1) : 0;
quizAppContainer.innerHTML = `
Quiz Completed!
Your Score: ${score} / ${totalQuestions}
Percentage: ${percentage}%
Review Your Answers:
${answeredQuestions.map((q, index) => `
${index + 1}. ${q.question}
Your Answer: ${q.userAnswer} ${q.isCorrect ? '✔' : '✖'}
${!q.isCorrect ? `
Correct Answer: ${q.correctAnswer}
` : ''}
Explanation: ${q.explanation}
`).join('')}
`;
clearState(); // Clear state after completion screen is rendered
// Scroll to the top of the quiz container when the completion screen is rendered
quizAppContainer.scrollIntoView({ behavior: 'smooth', block: 'start' });
};
const restartQuiz = () => {
clearState();
renderTypeSelectionScreen();
};
// --- Global functions for inline event handlers ---
// These are necessary for functions called directly from HTML onclick attributes
window.handleAnswer = handleAnswer;
window.showModal = showModal;
window.hideModal = hideModal;
window.chooseQuizType = chooseQuizType; // Still exposed for consistency, though startNewQuiz is primary
window.startNewQuiz = startNewQuiz; // New function for explicit new quiz start
window.continueLastQuiz = continueLastQuiz;
window.restartQuiz = restartQuiz;
window.renderNextQuestionOrFinish = renderNextQuestionOrFinish;
// Initial load logic: Always render type selection screen first
document.addEventListener('DOMContentLoaded', function() {
console.log("DOM Content Loaded, initializing quiz.");
renderTypeSelectionScreen();
});