Quizes
Quizes
addEventListener("DOMContentLoaded", () => {
let startBtn = document.querySelector(".start");
if (startBtn) {
startBtn.addEventListener("click", () => {
window.location.href = "UserDetails.html";
});
}
});
window.onload = () => {
if (window.location.pathname.includes("questions.html")) {
getQuestions();
buttonHandling();
} else if (window.location.pathname.includes("scores.html")) { // Check if on
scores page
displayScores(); // Display scores
}
};
function userDetails() {
let userName = document.querySelector("#name").value.trim(); // Get username
if (userName !== "") { // Check if name is not empty
users.push(userName); // Add username to array
localStorage.setItem("users", JSON.stringify(users)); // Save users to
localStorage
document.querySelector("#name").value = ""; // Clear input field
}
}
function displayQuestions() {
if (currQuesIdx < questions.length) {
let currQues = questions[currQuesIdx];
let options = [
{ text: currQues.correct_answer, isCorrect: true },
{ text: currQues.incorrect_answers[0], isCorrect: false },
{ text: currQues.incorrect_answers[1], isCorrect: false },
{ text: currQues.incorrect_answers[2], isCorrect: false }
];
options = shuffle(options);
document.querySelector(".questions").innerText =
decodeHTML(currQues.question);
document.querySelector(".current-ques-num").innerText = `Q. ${currQuesIdx +
1}/${questions.length}`;
// Display options
document.querySelector(".option-a").innerText = `A) $
{decodeHTML(options[0].text)}`;
document.querySelector(".option-b").innerText = `B) $
{decodeHTML(options[1].text)}`;
document.querySelector(".option-c").innerText = `C) $
{decodeHTML(options[2].text)}`;
document.querySelector(".option-d").innerText = `D) $
{decodeHTML(options[3].text)}`;
// Shuffle options
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
function setTimer() {
clearInterval(timer); // Clear previous timer
timeLeft = 10;
document.querySelector(".timer").innerText = timeLeft; // Initialize timer
display
timer = setInterval(() => {
if (timeLeft <= 0) {
showCorrectAnswer();
clearInterval(timer);
setTimeout(nextQuestion, 2000); // Move to next question after showing
correct answer
} else {
document.querySelector(".timer").innerText = timeLeft;
timeLeft--;
}
}, 1000);
}
function showCorrectAnswer() {
clearInterval(timer); // Stop timer
document.querySelector(".correct-answer").innerText = `Correct Answer: $
{decodeHTML(questions[currQuesIdx].correct_answer)}`;
document.querySelector(".correct-answer").style.display = "block";
}
function nextQuestion() {
currQuesIdx++;
if (currQuesIdx < questions.length) {
document.querySelector(".correct-answer").innerText = "";
displayQuestions(); // Display the next question
setTimer(); // Restart timer
} else {
showFinalScore(); // If no more questions, end quiz
}
}
function showFinalScore() {
clearInterval(timer);
document.body.innerHTML = `
<div class="final-score" style="color:white; text-align:center;">
<h1 style="color:white;">Quiz Finished</h1>
<p>Your final score is: ${score}/${questions.length * 2}</p>
<p>Your highest score is: ${getHighestScore()}</p> <!-- Display the
highest score -->
</div>
<div class="replay" style="margin:10px;">
<i class="fa-solid fa-repeat"></i>
<button class="play-again">Play Again</button> <!-- Button text for
replay -->
</div>
<div class="Quit" style="margin:10px;">
<i class="fa-solid fa-xmark"></i>
<button class="quit">Quit</button> <!-- Button text for quitting -->
</div>`;