JAVASCRIPT
// Game Initialization
document.getElementById('start-btn').addEventListener('click', startGame);
let currentQuestionIndex = 0;
let score = 0;
let streak = 0;
let timer;
let timeLeft = 10; // 10 seconds for each question
// List of questions
let questions = [
{
question: "3 + 5 = ?",
options: ["12", "8", "15", "10"],
correct: "8"
},
{
question: "6 × 7 = ?",
options: ["42", "36", "28", "48"],
correct: "42"
},
{
question: "20 ÷ 5 = ?",
options: ["4", "5", "6", "3"],
correct: "4"
},
{
question: "9 + 8 = ?",
options: ["17", "18", "15", "14"],
correct: "17"
},
// Additional questions
{
question: "15 - 7 = ?",
options: ["8", "9", "7", "6"],
correct: "8"
},
{
question: "5 × 9 = ?",
options: ["45", "50", "40", "55"],
correct: "45"
},
{
question: "12 ÷ 4 = ?",
options: ["3", "2", "4", "5"],
correct: "3"
},
{
question: "18 + 6 = ?",
options: ["22", "24", "20", "26"],
correct: "24"
},
{
question: "30 - 15 = ?",
options: ["15", "12", "18", "20"],
correct: "15"
},
{
question: "8 × 3 = ?",
options: ["24", "20", "22", "26"],
correct: "24"
},
{
question: "16 ÷ 2 = ?",
options: ["6", "8", "4", "10"],
correct: "8"
},
{
question: "14 + 9 = ?",
options: ["23", "22", "21", "24"],
correct: "23"
},
{
question: "25 - 10 = ?",
options: ["15", "20", "10", "5"],
correct: "15"
},
{
question: "7 × 6 = ?",
options: ["42", "36", "48", "54"],
correct: "42"
},
{
question: '100 ÷ 4 = ?',
options: ['25', '30', '20', '35'],
correct: '25'
}
];
function startGame() {
document.querySelector('.greeting-screen').style.display = 'none';
document.querySelector('.game-container').style.display = 'block';
showQuestion();
startTimer();
}
function startTimer() {
timeLeft = 10; // Reset timer for each question
document.getElementById('timer').textContent = timeLeft;
timer = setInterval(function() {
timeLeft--;
document.getElementById('timer').textContent = timeLeft;
if (timeLeft <= 0) {
clearInterval(timer);
gameOver();
}
}, 1000);
}
function showQuestion() {
let currentQuestion = questions[currentQuestionIndex];
document.getElementById('question').textContent = currentQuestion.question;
let optionsHtml = '';
currentQuestion.options.forEach(option => {
optionsHtml += `<button class="option-btn">${option}</button>`;
});
document.querySelector('.options-container').innerHTML = optionsHtml;
document.getElementById('score').textContent = score;
document.getElementById('streak').textContent = streak;
}
// Handle answer selection
document.querySelector('.options-container').addEventListener('click', function(event) {
if (event.target.tagName === 'BUTTON') {
let selectedOption = event.target.textContent;
checkAnswer(selectedOption);
}
});
function checkAnswer(selectedOption) {
let currentQuestion = questions[currentQuestionIndex];
if (selectedOption === currentQuestion.correct) {
score += 10;
streak++;
} else {
gameOver();
}
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
clearInterval(timer);
showQuestion();
startTimer();
} else {
gameOver();
}
}
function gameOver() {
document.querySelector('.game-container').style.display = 'none';
document.querySelector('.losing-screen').style.display = 'block';
document.getElementById('smarter-percentage').textContent = `You are smarter than ${Math.floor(Math.random() * 100)}% of people!`;
document.querySelector('.score-message').textContent = `Your score: ${score}`;
document.querySelector('.final-message').textContent = 'Thanks for trying my app!';
}