script.js
script.js
{
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;
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();