-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
117 lines (96 loc) · 3.28 KB
/
script.js
File metadata and controls
117 lines (96 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//codewithcurious.com
// Define an array of quiz questions
const quizQuestions = [
{
question: "What is the capital of France?",
options: ["Paris", "London", "Berlin", "Rome"],
correctAnswer: "Paris"
},
{
question: "Which planet is known as the Red Planet?",
options: ["Venus", "Mars", "Jupiter", "Saturn"],
correctAnswer: "Mars"
},
{
question: "What is the chemical symbol for gold?",
options: ["Au", "Ag", "Cu", "Fe"],
correctAnswer: "Au"
}
];
// Variables to track quiz state
let currentQuestionIndex = 0;
let score = 0;
let timeLeft = 30;
let timerInterval;
// Function to start the quiz
function startQuiz() {
// Hide the start button and display the first question
document.getElementById("start-button").style.display = "none";
displayQuestion();
startTimer();
}
// Function to display a question and its options
function displayQuestion() {
const currentQuestion = quizQuestions[currentQuestionIndex];
const questionText = document.getElementById("question-text");
const answerButtons = document.getElementById("answer-buttons");
// Clear previous question and answer options
questionText.innerHTML = "";
answerButtons.innerHTML = "";
// Display the current question
questionText.innerHTML = currentQuestion.question;
// Create answer buttons for each option
currentQuestion.options.forEach(option => {
const button = document.createElement("button");
button.innerText = option;
button.classList.add("answer-button");
answerButtons.appendChild(button);
// Add click event listener to check the answer
button.addEventListener("click", function() {
checkAnswer(option);
});
});
}
// Function to check the selected answer
function checkAnswer(selectedOption) {
const currentQuestion = quizQuestions[currentQuestionIndex];
// Check if the selected answer is correct
if (selectedOption === currentQuestion.correctAnswer) {
score++;
}
// Move to the next question or end the quiz if all questions are answered
currentQuestionIndex++;
if (currentQuestionIndex < quizQuestions.length) {
displayQuestion();
} else {
endQuiz();
}
}
// Function to start the timer
function startTimer() {
timerInterval = setInterval(function() {
timeLeft--;
// Update the timer text
document.getElementById("timer").textContent = timeLeft;
// End the quiz if time runs out
if (timeLeft <= 0) {
endQuiz();
}
}, 1000);
}
// Function to end the quiz
function endQuiz() {
// Stop the timer
clearInterval(timerInterval);
// Calculate the score percentage
const scorePercentage = (score / quizQuestions.length) * 100;
// Display the final score
const questionContainer = document.getElementById("question-container");
questionContainer.innerHTML = `
<h2>Quiz Completed!</h2>
<p>Your Score: ${score} out of ${quizQuestions.length}</p>
<p>Score Percentage: ${scorePercentage}%</p>
`;
}
// Add event listener to start the quiz when the start button is clicked
document.getElementById("start-button").addEventListener("click", startQuiz);