0% found this document useful (0 votes)
8 views

script.js

The document outlines a JavaScript quiz game that presents multiple-choice questions to the player. It includes functionalities for tracking the score, managing a timer, and displaying a leaderboard for high scores. The game progresses through a series of questions, and upon completion, the player's score is saved and displayed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

script.js

The document outlines a JavaScript quiz game that presents multiple-choice questions to the player. It includes functionalities for tracking the score, managing a timer, and displaying a leaderboard for high scores. The game progresses through a series of questions, and upon completion, the player's score is saved and displayed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

const questions = [

{
question: "What is the circumference of a circle with radius 8 cm?",
answers: ["16π cm", "50.24 cm", "64π cm", "25.12 cm"],
correct: "50.24 cm",
hint: "Circumference = 2πr"
},
{
question: "Which algebraic expression represents 'Subtract 7 from a number
y and then triple the result'?",
answers: ["3(y - 7)", "3y - 7", "3y - 21", "(y - 7)/3"],
correct: "3(y - 7)",
hint: "Remember the order of operations!"
},
{
question: "For the sequence 6, 12, 18, 24, 30, the next term is:",
answers: ["34", "35", "36", "37"],
correct: "36",
hint: "The sequence increases by 6."
}
];

let currentQuestionIndex = 0;
let score = 0;
let timeLeft = 15 * 60;

const questionContainer = document.getElementById("question-container");


const answerButtons = document.getElementById("answer-buttons");
const nextButton = document.getElementById("next-btn");
const scoreContainer = document.getElementById("score-container");
const leaderboardList = document.getElementById("leaderboard-list");

function startGame() {
showQuestion();
startTimer();
}

function showQuestion() {
resetState();
const currentQuestion = questions[currentQuestionIndex];
questionContainer.innerText = currentQuestion.question;

currentQuestion.answers.forEach(answer => {
const button = document.createElement("button");
button.innerText = answer;
button.addEventListener("click", () => selectAnswer(answer));
answerButtons.appendChild(button);
});
}

function resetState() {
answerButtons.innerHTML = "";
nextButton.style.display = "none";
}

function selectAnswer(selectedAnswer) {
const currentQuestion = questions[currentQuestionIndex];
if (selectedAnswer === currentQuestion.correct) {
score++;
}
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion();
} else {
endGame();
}
}

function startTimer() {
const timerElement = document.getElementById("time");
const countdown = setInterval(() => {
const minutes = Math.floor(timeLeft / 60);
const seconds = timeLeft % 60;
timerElement.textContent = `${minutes}:${seconds < 10 ? '0' : ''}$
{seconds}`;

if (timeLeft <= 0) {
clearInterval(countdown);
endGame();
}
timeLeft--;
}, 1000);
}

function endGame() {
questionContainer.innerText = `Game Over! Your score: ${score} / $
{questions.length}`;
scoreContainer.innerHTML = `🎯 Final Score: <b>${score}</b>`;
saveScore(score);
}

function saveScore(score) {
const playerName = prompt("Enter your name for the leaderboard:");
const newScore = `${playerName}: ${score}`;
const listItem = document.createElement("li");
listItem.innerText = newScore;
leaderboardList.appendChild(listItem);
}

startGame();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy