const API_BASE = "[Link]
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() {
[Link]().then(() => {
[Link]('MathJax rendering complete');
}).catch((err) => {
[Link]('MathJax rendering failed:', err);
});
}
function renderContent(html) {
//return [Link](/\$\$(.*?)\$\$/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}'>
${[Link]()}
</button>
`);
});
}
function fetchChapters() {
$.get(`${API_BASE}${subject}`, (data) => {
$("#chapter-container").show();
$("#chapters").html("");
[Link](ch => {
$("#chapters").append(`
<button class='chapter' data-chapter='${ch}'>
${[Link](/-/g, ' ')}
</button>
`);
});
$("#subject-container").hide();
});
}
function fetchTopics() {
$.get(`${API_BASE}${subject}/${chapter}`, (data) => {
$("#topic-container").show();
$("#topics").html("");
[Link](tp => {
$("#topics").append(`
<button class='topic' data-topic='${tp}'>
${[Link](/-/g, ' ')}
</button>
`);
});
$("#chapter-container").hide();
});
}
function fetchQuestions() {
$.get(`${API_BASE}${subject}/${chapter}/${topic}`, (data) => {
questions = [Link];
currentQuestionIndex = 0;
$("#total-questions").text([Link]);
$("#question-container").show();
renderQuestion();
$("#topic-container").hide();
});
}
function renderQuestion() {
if (![Link]) return;
const currentQuestion = questions[currentQuestionIndex];
const q = [Link];
// Update question number and paper title
$("#current-question").text(currentQuestionIndex + 1);
$("#paper-title").text([Link] || "");
$("#question").html(renderContent([Link]));
// Render based on question type
if ([Link] === "mcq") {
const options = [Link](opt => {
const renderedContent = renderContent([Link]);
return `
<button class='option' data-identifier='${[Link]}'>
${renderedContent}
</button>
`;
}).join("");
$("#options").html(options);
$("#integer-input").hide();
$("#options").show();
} else if ([Link] === "integer") {
$("#options").hide();
$("#integer-input")
.show()
.val("") // Clear previous input
.attr('type', 'number');
}
// Reset previous states
$(".option").removeClass("selected correct incorrect");
$("#explanation").html("");
$("#check-answer").show();
// Manage navigation buttons
$("#prev-question").prop('disabled', currentQuestionIndex === 0);
$("#next-question").prop('disabled', currentQuestionIndex === [Link]
- 1);
startTimer();
renderMathJax();
}
// Event Listeners
$(document).on("click", ".subject", function() {
subject = $(this).data("subject");
fetchChapters();
});
$(document).on("click", ".chapter", function() {
chapter = $(this).data("chapter");
fetchTopics();
});
$(document).on("click", ".topic", function() {
topic = $(this).data("topic");
fetchQuestions();
});
$(document).on("click", ".option", function() {
$(".option").removeClass("selected");
$(this).addClass("selected");
$("#check-answer").show();
});
$("#check-answer").click(function() {
stopTimer();
const currentQuestion = questions[currentQuestionIndex];
if ([Link] === "mcq") {
const selected = $(".[Link]").data("identifier");
const correctOptions = [Link].correct_options;
$(".option").each(function() {
const optIdentifier = $(this).data("identifier");
if ([Link](optIdentifier)) {
$(this).addClass("correct");
}
});
if ([Link](selected)) {
$(".[Link]").addClass("correct");
$("#explanation").html(`
<div class="success">
<strong>Correct!</strong><br>
${renderContent([Link])}
</div>
`);
} else {
$(".[Link]").addClass("incorrect");
$("#explanation").html(`
<div class="error">
<strong>Incorrect!</strong><br>
${renderContent([Link])}
</div>
`);
}
} else if ([Link] === "integer") {
const userAnswer = $("#integer-input").val();
const correctAnswer = [Link];
if (userAnswer === [Link]()) {
$("#explanation").html(`
<div class="success">
<strong>Correct!</strong><br>
${renderContent([Link])}
</div>
`);
} else {
$("#explanation").html(`
<div class="error">
<strong>Incorrect!</strong> The correct answer is $
{correctAnswer}<br>
${renderContent([Link])}
</div>
`);
}
}
renderMathJax();
});
$("#prev-question").click(function() {
if (currentQuestionIndex > 0) {
currentQuestionIndex--;
renderQuestion();
}
});
$("#next-question").click(function() {
if (currentQuestionIndex < [Link] - 1) {
currentQuestionIndex++;
renderQuestion();
}
});
// Back navigation buttons
$("#back-to-subjects").click(function() {
$("#chapter-container").hide();
$("#subject-container").show();
});
$("#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([Link](/-/g, '
')).toggle(Boolean(chapter));
$("#breadcrumb-topic").text([Link](/-/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();
});
$(document).on("click", ".chapter", function () {
chapter = $(this).data("chapter");
updateBreadcrumb();
fetchTopics();
hideQuestionContainer(); // Ensure question view is hidden
$("#chapter-container").show();
});
$(document).on("click", ".topic", function () {
topic = $(this).data("topic");
updateBreadcrumb();
fetchQuestions();
hideQuestionContainer(); // Ensure question view is hidden
$("#topic-container").show();
});
// Function to hide the question container
function hideQuestionContainer() {
$("#question-container").hide();
}
// Optional: Handle breadcrumb navigation for back buttons
$("#breadcrumb-subject").click(function () {
chapter = "";
topic = "";
updateBreadcrumb();
hideQuestionContainer(); // Ensure question view is hidden
$("#chapter-container").hide();
$("#topic-container").hide();
$("#subject-container").show();
});
$("#breadcrumb-chapter").click(function () {
topic = "";
updateBreadcrumb();
hideQuestionContainer(); // Ensure question view is hidden
$("#topic-container").hide();
$("#chapter-container").show();
});
// Optional: Handle topic breadcrumb
$("#breadcrumb-topic").click(function () {
updateBreadcrumb();
hideQuestionContainer(); // Ensure question view is hidden
$("#question-container").show(); // If you want to show the question view for
this breadcrumb
});