// Initialize variables
let currentQuestion = 1;
let correctAnswers = 0;
let examTitle = "";
let questionsData = {};
let userSelections = {};
// Get HTML elements
const questionText = document.getElementById("questionText");
const questionPos = document.getElementById("questionPos");
const choicesContainer = document.getElementById("choices");
const homeBtn = document.getElementById("homeBtn");
const prevBtn = document.getElementById("prevBtn");
const nextBtn = document.getElementById("nextBtn");
const correctAnswersSpan = document.getElementById("correctAnswers");
const percentageSpan = document.getElementById("percentage");
const examContent = document.querySelector(".exam-content");
const resultContainer = document.querySelector(".result-container");
const newExamBtn = document.getElementById("newExamBtn");
// Timer variables
let startTime;
let intervalId;
// Function to format choices
function formatChoices(choices) {
return choices.map((choice, index) => {
return `${String.fromCharCode(65 + index)}) ${choice.text}`;
}).join("
");
}
// Function to update the timer
function updateTimer() {
const currentTime = new Date();
const elapsedTime = currentTime - startTime;
const formattedTime = formatTime(elapsedTime);
const question = questionsData[currentQuestion];
const totalQuestions = Object.keys(questionsData).length;
questionPos.innerHTML = `Progress: ${currentQuestion}/${totalQuestions} [Elapsed Time: ${formattedTime}]`;
}
// Function to format time as hh:mm:ss
function formatTime(milliseconds) {
const totalSeconds = Math.floor(milliseconds / 1000);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
return `${padNumber(hours)}:${padNumber(minutes)}:${padNumber(seconds)}`;
}
// Function to pad numbers with leading zeros
function padNumber(number) {
return number.toString().padStart(2, "0");
}
// Function to Display Questions
function displayQuestion(questionKey) {
const question = questionsData[questionKey];
const currentTime = new Date();
const elapsedTime = currentTime - startTime;
const formattedTime = formatTime(elapsedTime);
const totalQuestions = Object.keys(questionsData).length;
questionPos.innerHTML = `Progress: ${questionKey}/${totalQuestions} [Elapsed Time: ${formattedTime}]`;
if (questionKey !== totalQuestions) {
resultContainer.style.display = "none";
}
if (!question) {
return;
}
questionText.innerHTML = `${questionKey}) ${question.question}`;
const choicesHTML = question.choices.map((choice, index) => {
const choiceLetter = String.fromCharCode(65 + index);
const isChecked = userSelections[questionKey]?.includes(index) ? "checked" : "";
const isCorrect = question.correct.includes(index);
return `
`;
}).join("");
choicesContainer.innerHTML = choicesHTML;
prevBtn.disabled = questionKey === 1;
const selectedCount = userSelections[questionKey]?.length || 0;
nextBtn.disabled = selectedCount < question.minChoices;
}
// Function to Update Button States
function updateButtonStates() {
const selectedCount = userSelections[currentQuestion]?.length || 0;
nextBtn.disabled = selectedCount < questionsData[currentQuestion].minChoices;
}
// Function to Reset Variables
function resetVariables() {
currentQuestion = 1;
correctAnswers = 0;
questionsData = {};
userSelections = {};
}
// Function to Check Answers
function checkAnswers() {
const questionKeys = Object.keys(questionsData);
questionKeys.forEach((questionKey) => {
const question = questionsData[questionKey];
const selectedChoices = userSelections[questionKey] || [];
const correctChoiceIndices = question.correct;
const isAnswerCorrect = selectedChoices.length === correctChoiceIndices.length &&
selectedChoices.every(choiceIndex => correctChoiceIndices.includes(choiceIndex));
if (isAnswerCorrect) {
correctAnswers++;
question.answeredCorrectly = true;
} else {
question.answeredCorrectly = false;
}
question.answered = true;
});
// Exam Results
const percentage = (correctAnswers / questionKeys.length) * 100;
correctAnswersSpan.textContent = correctAnswers;
percentageSpan.textContent = percentage.toFixed(2) + "%";
// Elapsed Time
clearInterval(intervalId);
const endTime = new Date();
const elapsedTime = endTime - startTime;
const formattedElapsedTime = formatTime(elapsedTime);
const elapsedTimeElement = document.getElementById("elapsedTime");
elapsedTimeElement.textContent = `Elapsed Time: ${formattedElapsedTime}`;
examContent.style.display = "none";
resultContainer.style.display = "block";
if (percentage < 100){
displayWrongAnswers();
}
// Event listener "New Exam" button
const newExamBtn = document.getElementById("newExamBtn");
newExamBtn.addEventListener("click", () => {
resetVariables();
window.location.href = "index.html";
});
}
// Function to Display Wrong Answers
function displayWrongAnswers() {
const wrongAnswers = Object.keys(questionsData)
.filter(questionKey => questionsData[questionKey].answered && !questionsData[questionKey].answeredCorrectly);
const wrongAnswersHTML = wrongAnswers.map((questionKey) => {
const question = questionsData[questionKey];
const choicesHTML = question.choices.map((choice, index) => {
const choiceLetter = String.fromCharCode(65 + index);
const isChecked = userSelections[questionKey]?.includes(index) ? "checked" : "";
if (question.correct.includes(index)) {
return `
`;
} else {
return `
`;
}
}).join("");
return `
${questionKey}) ${question.question}
${choicesHTML}`; }).join("