// Sample Questions
const questions = [
// Round 1 - Easy
{
question: "What is the capital of France?",
options: ["Berlin", "London", "Paris", "Madrid"],
answer: 2,
hint: "It's also known as the city of love.",
difficulty: 1
},
{
question: "2 + 2 equals?",
options: ["3", "4", "5", "22"],
answer: 1,
hint: "It's the first even number.",
difficulty: 1
},
// Round 2 - Medium
{
question: "Which element has the chemical symbol 'O'?",
options: ["Gold", "Oxygen", "Silver", "Iron"],
answer: 1,
hint: "It's essential for breathing.",
difficulty: 2
},
{
question: "Who wrote 'Hamlet'?",
options: ["Charles Dickens", "Leo Tolstoy", "Mark Twain", "William Shakespeare"],
answer: 3,
hint: "He is known as the Bard of Avon.",
difficulty: 2
},
// Round 3 - Hard
{
question: "What is the derivative of sin(x)?",
options: ["cos(x)", "-cos(x)", "sin(x)", "-sin(x)"],
answer: 0,
hint: "Think about the derivative rules in calculus.",
difficulty: 3
},
{
question: "What is the 7th prime number?",
options: ["13", "17", "19", "23"],
answer: 1,
hint: "It's between 15 and 20.",
difficulty: 3
}
];
let shuffledQuestions, currentQuestionIndex;
let score = 0;
let timeLeft = 30;
let timer;
let lifelineUsed = false;
const startButton = document.getElementById('start-btn');
const quizContainer = document.getElementById('quiz-container');
const questionElement = document.getElementById('question');
const optionsContainer = document.getElementById('options');
const nextButton = document.getElementById('next-btn');
const resultContainer = document.getElementById('result-container');
const scoreElement = document.getElementById('score');
const restartButton = document.getElementById('restart-btn');
const timerElement = document.getElementById('time');
const hintButton = document.getElementById('hint-btn');
const hintElement = document.getElementById('hint');
startButton.addEventListener('click', startQuiz);
nextButton.addEventListener('click', () => {
currentQuestionIndex++;
setNextQuestion();
});
restartButton.addEventListener('click', () => {
window.location.reload();
});
hintButton.addEventListener('click', useHint);
function startQuiz() {
startButton.classList.add('hide');
shuffledQuestions = questions;
currentQuestionIndex = 0;
quizContainer.classList.remove('hide');
setNextQuestion();
}
function setNextQuestion() {
resetState();
if (currentQuestionIndex < shuffledQuestions.length) {
showQuestion(shuffledQuestions[currentQuestionIndex]);
startTimer();
} else {
endQuiz();
}
}
function showQuestion(questionObj) {
questionElement.innerText = questionObj.question;
questionObj.options.forEach((option, index) => {
const button = document.createElement('button');
button.innerText = option;
button.classList.add('option-btn');
button.addEventListener('click', selectAnswer);
optionsContainer.appendChild(button);
});
}
function resetState() {
clearTimeout(timer);
timeLeft = 30;
timerElement.innerText = timeLeft;
lifelineUsed = false;
hintButton.disabled = false;
hintElement.classList.add('hide');
while (optionsContainer.firstChild) {
optionsContainer.removeChild(optionsContainer.firstChild);
}
}
function selectAnswer(e) {
const selectedButton = e.target;
const correct = shuffledQuestions[currentQuestionIndex].answer;
Array.from(optionsContainer.children).forEach((button, index) => {
if (index === correct) {
button.classList.add('correct');
} else {
button.classList.add('wrong');
}
button.disabled = true;
});
if (Array.from(optionsContainer.children).indexOf(selectedButton) === correct) {
calculateScore();
}
clearTimeout(timer);
nextButton.classList.remove('hide');
}
function calculateScore() {
const difficulty = shuffledQuestions[currentQuestionIndex].difficulty;
const points = timeLeft * difficulty * (lifelineUsed ? 0.5 : 1);
score += points;
}
function endQuiz() {
quizContainer.classList.add('hide');
resultContainer.classList.remove('hide');
scoreElement.innerText = Math.round(score);
}
function startTimer() {
timer = setInterval(() => {
timeLeft--;
timerElement.innerText = timeLeft;
if (timeLeft <= 0) {
clearTimeout(timer);
Array.from(optionsContainer.children).forEach((button) => {
button.disabled = true;
});
nextButton.classList.remove('hide');
}
}, 1000);
}
function useHint() {
if (!lifelineUsed) {
hintElement.innerText = shuffledQuestions[currentQuestionIndex].hint;
hintElement.classList.remove('hide');
lifelineUsed = true;
hintButton.disabled = true;
}
}