Practice Test
Welcome to the Practice Test
- This test is designed to help you practice and assess your knowledge.
- You will have 60 seconds per question to select the best answer.
- If the timer runs out, the test will automatically move to the next question.
- After completing the test, you can review your results.
- If you score 60% or above, you can download a personalized certificate.
Test Results
Welcome to the Practice Test
- This test is designed to help you practice and assess your knowledge.
- You will have 60 seconds per question to select the best answer.
- If the timer runs out, the test will automatically move to the next question.
- After completing the test, you can review your results.
- If you score 60% or above, you can download a personalized certificate.
Test Results
;
// Global variables to track current question, user answers, score, and timer
let currentQuestion = 0;
let userAnswers = new Array(questions.length).fill(-1);
let scorePercentage = 0;
let timerInterval;
let currentTimer = 60; // 60 seconds per question
// Function to start the test: hide instructions and show quiz
function startTest() {
document.getElementById("start-container").style.display = "none";
document.getElementById("quiz-container").style.display = "block";
displayQuestion();
}
// Function to display a question
function displayQuestion() {
clearInterval(timerInterval);
currentTimer = 60;
document.getElementById("timer").textContent = "Time remaining: " + currentTimer + " sec";
startTimer();
document.getElementById("question-counter").textContent = "Question " + (currentQuestion + 1) + "/" + questions.length;
const q = questions[currentQuestion];
document.getElementById("question-text").textContent = "Q" + (currentQuestion + 1) + ": " + q.question;
const optionsContainer = document.getElementById("options-container");
optionsContainer.innerHTML = "";
q.options.forEach((option, index) => {
const radioInput = document.createElement("input");
radioInput.type = "radio";
radioInput.id = "option" + index;
radioInput.name = "option";
radioInput.value = index;
if (userAnswers[currentQuestion] === index) {
radioInput.checked = true;
}
const labelElem = document.createElement("label");
labelElem.className = "option-label";
labelElem.htmlFor = "option" + index;
labelElem.textContent = option;
optionsContainer.appendChild(radioInput);
optionsContainer.appendChild(labelElem);
});
document.getElementById("prev-btn").style.display = (currentQuestion === 0) ? "none" : "inline-block";
const nextBtn = document.getElementById("next-btn");
nextBtn.textContent = (currentQuestion === questions.length - 1) ? "Submit" : "Next";
}
// Timer function: counts down from 60 seconds and auto-moves to next question when time expires
function startTimer() {
timerInterval = setInterval(() => {
currentTimer--;
document.getElementById("timer").textContent = "Time remaining: " + currentTimer + " sec";
if (currentTimer <= 0) {
clearInterval(timerInterval);
nextOrSubmit();
}
}, 1000);
}
// Function to move to the next question or submit if it's the last question
function nextOrSubmit() {
clearInterval(timerInterval);
const options = document.getElementsByName("option");
let selected = -1;
for (let opt of options) {
if (opt.checked) {
selected = parseInt(opt.value);
break;
}
}
userAnswers[currentQuestion] = selected;
if (currentQuestion === questions.length - 1) {
showResults();
} else {
currentQuestion++;
displayQuestion();
}
}
// Function to navigate to the previous question
function prevQuestion() {
clearInterval(timerInterval);
const options = document.getElementsByName("option");
let selected = -1;
for (let opt of options) {
if (opt.checked) {
selected = parseInt(opt.value);
break;
}
}
userAnswers[currentQuestion] = selected;
if (currentQuestion > 0) {
currentQuestion--;
displayQuestion();
}
}
// Function to allow the user to submit the test at any time
function submitTest() {
if (confirm("Are you sure you want to submit the test?")) {
clearInterval(timerInterval);
const options = document.getElementsByName("option");
let selected = -1;
for (let opt of options) {
if (opt.checked) {
selected = parseInt(opt.value);
break;
}
}
userAnswers[currentQuestion] = selected;
showResults();
}
}
// Function to evaluate the test and display results
function showResults() {
clearInterval(timerInterval);
document.getElementById("quiz-container").style.display = "none";
document.getElementById("result-container").style.display = "block";
let correctCount = 0;
let summaryHTML = "";
questions.forEach((q, index) => {
let userAnswer = userAnswers[index];
let isCorrect = (userAnswer === q.correctIndex);
if (isCorrect) correctCount++;
summaryHTML += "
Q" + (index + 1) + ": " + q.question + "
";
summaryHTML += "Your answer: " + (userAnswer === -1 ? "No answer selected" : q.options[userAnswer]) + "
";
summaryHTML += "Correct answer: " + q.options[q.correctIndex] + "
";
summaryHTML += "Result: " + (isCorrect ? "Correct" : "Wrong") + "
";
});
document.getElementById("result-summary").innerHTML = summaryHTML;
scorePercentage = Math.round((correctCount / questions.length) * 100);
const scoreMessage = document.getElementById("score-message");
scoreMessage.innerHTML = scorePercentage >= 50
? "Congratulations! You passed with a score of " + scorePercentage + "%."
: "You scored " + scorePercentage + "%. Try better next time!";
document.getElementById("certificate-btn").style.display = (scorePercentage >= 60) ? "inline-block" : "none";
}
// Function to generate an attractive certificate PDF using jsPDF
function downloadCertificate() {
let userName = prompt("Please enter your name for the certificate:");
if (!userName || userName.trim() === "") {
alert("Name is required to generate a certificate.");
return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: "landscape", unit: "pt", format: "a4" });
const pageWidth = doc.internal.pageSize.getWidth();
const pageHeight = doc.internal.pageSize.getHeight();
doc.setFillColor(255, 215, 0);
doc.rect(0, 0, pageWidth, pageHeight, "F");
const padding = 20;
doc.setFillColor(192, 192, 192);
doc.rect(padding, padding, pageWidth - 2 * padding, pageHeight - 2 * padding, "F");
const padding2 = 40;
doc.setFillColor(255, 255, 255);
doc.rect(padding2, padding2, pageWidth - 2 * padding2, pageHeight - 2 * padding2, "F");
doc.setDrawColor(255, 215, 0);
doc.setLineWidth(4);
doc.rect(padding2, padding2, pageWidth - 2 * padding2, pageHeight - 2 * padding2);
doc.setFillColor(255, 215, 0);
doc.circle(pageWidth/2 - 150, 90, 10, 'F');
doc.circle(pageWidth/2 + 150, 90, 10, 'F');
doc.setFont("times", "bold");
doc.setFontSize(36);
doc.setTextColor(30, 144, 255);
doc.text("Certificate of Achievement", pageWidth / 2, 130, { align: "center" });
doc.setFont("helvetica", "normal");
doc.setFontSize(20);
doc.setTextColor(0, 0, 0);
doc.text("This is to certify that", pageWidth / 2, 180, { align: "center" });
doc.setFont("times", "bolditalic");
doc.setFontSize(28);
doc.setTextColor(220, 20, 60);
doc.text(userName, pageWidth / 2, 230, { align: "center" });
doc.setFont("helvetica", "normal");
doc.setFontSize(18);
doc.setTextColor(0, 0, 0);
doc.text(`has successfully achieved a score of ${scorePercentage}% on the test.`, pageWidth / 2, 280, { align: "center" });
doc.setDrawColor(220, 20, 60);
doc.setLineWidth(2);
doc.line(pageWidth / 2 - 100, 300, pageWidth / 2 + 100, 300);
doc.setFontSize(16);
doc.text("Congratulations and keep up the excellent work!", pageWidth / 2, 340, { align: "center" });
doc.setFontSize(16);
doc.text("Test Subject: " + testSubject, pageWidth / 2, 370, { align: "center" });
doc.setFont("helvetica", "italic");
doc.setFontSize(20);
doc.setTextColor(30, 144, 255);
doc.text("XamCrack", pageWidth - 250, pageHeight - 110);
doc.setFont("helvetica", "italic");
doc.setFontSize(14);
doc.text("Authorized Signature", pageWidth - 250, pageHeight - 70);
const today = new Date();
const day = ("0" + today.getDate()).slice(-2);
const month = ("0" + (today.getMonth() + 1)).slice(-2);
const year = today.getFullYear();
const dateStr = `${day}/${month}/${year}`;
doc.text("Date: " + dateStr, 80, pageHeight - 50);
doc.setFont("helvetica", "normal");
doc.setFontSize(12);
doc.text("XamCrack.com", pageWidth / 2, pageHeight - 40, { align: "center" });
doc.save("Certificate_" + userName.replace(/\s+/g, '') + ".pdf");
}
// Function to share test results on WhatsApp
function shareOnWhatsApp() {
const testLink = "https://xamcrack.com/class-9-science-online-practice-test/";
const message = `🎉 I just completed the ${testSubject} and scored ${scorePercentage}%! 💯
Test yourself on XamCrack: ${testLink}`;
const encodedMessage = encodeURIComponent(message);
const whatsappURL = `https://wa.me/?text=${encodedMessage}`;
window.open(whatsappURL, "_blank");
}
// Function to reset the test for another try
function testAgain() {
currentQuestion = 0;
userAnswers = new Array(questions.length).fill(-1);
document.getElementById("result-container").style.display = "none";
document.getElementById("quiz-container").style.display = "block";
displayQuestion();
}