Message 2
Message 2
com/";
let subject = "", chapter = "", topic = "", questions = [], currentQuestionIndex =
0, timer = 0, timerInterval;
function startTimer() {
timer = 0;
clearInterval(timerInterval);
timerInterval = setInterval(() => {
timer++;
$("#timer").text(timer);
}, 1000);
}
function stopTimer() {
clearInterval(timerInterval);
}
function renderMathJax() {
MathJax.typesetPromise().then(() => {
console.log('MathJax rendering complete');
}).catch((err) => {
console.error('MathJax rendering failed:', err);
});
}
function renderContent(html) {
//return html.replace(/\$\$(.*?)\$\$/g, '\\\[$1\\\]')
//.replace(/\$(.*?)\$/g, '\\\$$1\\\$');
return html;
}
function fetchAndRenderSubjects() {
$("#subject-container").show();
$("#subjects").html("");
["maths", "physics", "chemistry"].forEach(sub => {
$("#subjects").append(`
<button class='subject' data-subject='${sub}'>
${sub.toUpperCase()}
</button>
`);
});
}
function fetchChapters() {
$.get(`${API_BASE}${subject}`, (data) => {
$("#chapter-container").show();
$("#chapters").html("");
data.chapters.forEach(ch => {
$("#chapters").append(`
<button class='chapter' data-chapter='${ch}'>
${ch.replace(/-/g, ' ')}
</button>
`);
});
$("#subject-container").hide();
});
}
function fetchTopics() {
$.get(`${API_BASE}${subject}/${chapter}`, (data) => {
$("#topic-container").show();
$("#topics").html("");
data.topics.forEach(tp => {
$("#topics").append(`
<button class='topic' data-topic='${tp}'>
${tp.replace(/-/g, ' ')}
</button>
`);
});
$("#chapter-container").hide();
});
}
function fetchQuestions() {
$.get(`${API_BASE}${subject}/${chapter}/${topic}`, (data) => {
questions = data.questions;
currentQuestionIndex = 0;
$("#total-questions").text(questions.length);
$("#question-container").show();
renderQuestion();
$("#topic-container").hide();
});
}
function renderQuestion() {
if (!questions.length) return;
$("#question").html(renderContent(q.content));
startTimer();
renderMathJax();
}
// Event Listeners
$(document).on("click", ".subject", function() {
subject = $(this).data("subject");
fetchChapters();
});
$("#check-answer").click(function() {
stopTimer();
$(".option").each(function() {
const optIdentifier = $(this).data("identifier");
if (correctOptions.includes(optIdentifier)) {
$(this).addClass("correct");
}
});
if (correctOptions.includes(selected)) {
$(".option.selected").addClass("correct");
$("#explanation").html(`
<div class="success">
<strong>Correct!</strong><br>
${renderContent(currentQuestion.question.explanation)}
</div>
`);
} else {
$(".option.selected").addClass("incorrect");
$("#explanation").html(`
<div class="error">
<strong>Incorrect!</strong><br>
${renderContent(currentQuestion.question.explanation)}
</div>
`);
}
} else if (currentQuestion.type === "integer") {
const userAnswer = $("#integer-input").val();
const correctAnswer = currentQuestion.question.answer;
renderMathJax();
});
$("#prev-question").click(function() {
if (currentQuestionIndex > 0) {
currentQuestionIndex--;
renderQuestion();
}
});
$("#next-question").click(function() {
if (currentQuestionIndex < questions.length - 1) {
currentQuestionIndex++;
renderQuestion();
}
});
$("#back-to-chapters").click(function() {
$("#topic-container").hide();
$("#chapter-container").show();
});
$(document).ready(() => {
fetchAndRenderSubjects();
});
function updateBreadcrumb() {
$("#breadcrumb-subject").text(subject).toggle(Boolean(subject));
$("#breadcrumb-chapter").text(chapter.replace(/-/g, '
')).toggle(Boolean(chapter));
$("#breadcrumb-topic").text(topic.replace(/-/g, ' ')).toggle(Boolean(topic));
}
// Handle the click events for subject, chapter, and topic links
$(document).on("click", ".subject", function () {
subject = $(this).data("subject");
updateBreadcrumb();
fetchChapters();
hideQuestionContainer(); // Ensure question view is hidden
$("#subject-container").show();
});
$("#breadcrumb-chapter").click(function () {
topic = "";
updateBreadcrumb();
hideQuestionContainer(); // Ensure question view is hidden
$("#topic-container").hide();
$("#chapter-container").show();
});