-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
107 lines (93 loc) · 3.14 KB
/
script.js
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
// Variables for quiz
let currentQuestion = 0;
let timer = 60;
let countdownTimer = 0;
let score = 0;
let questionSet = [
{
question: "What is the capital of France?",
choices: ["Paris", "Madrid", "Rome", "London"],
answer: "Paris"
},
{
question: "What is the largest planet in our solar system?",
choices: ["Jupiter", "Mars", "Earth", "Venus"],
answer: "Jupiter"
}
];
// Create elements to display quiz
let startButton = document.querySelector("#start-button");
let startScreen = document.querySelector("#start-screen");
let questionEl = document.querySelector("#question");
let choicesEl = document.querySelector("#choices");
let timeEl = document.querySelector("#time");
let scoreEl = document.querySelector("#score");
let initialsEl = document.querySelector("#initials");
let submitButton = document.querySelector("#submit-button");
// Add event listener to start button
// startButton.addEventListener("click", startQuiz);
//Create an event listener on the start button
startButton.addEventListener("click", function (event) {
console.log(this.event.target)
});
// Function to start quiz
function startQuiz() {
startButton.classList.replace("start", "hide"); //Hide the default screen
startScreen.classList.add("hide"); //Hide the start quiz button
questionEl.classList.remove("hide"); //Display the first question
currentQuestion = 0;
// Start countdown
countdownTimer = setInterval(function () {
timer--; // Deduct time by 1
timeEl.textContent = timer; // Display remaining time
if (timer <= 0 || currentQuestion >= questionSet.length) {
endQuiz();
}
}, 1000);
// Display first question
displayQuestion();
}
// Function to display question
function displayQuestion() {
let currentQuestionObj = questionSet[currentQuestion];
questionEl.textContent = currentQuestionObj.question;
// Clear previous choices
while (choicesEl.firstChild) {
choicesEl.removeChild(choicesEl.firstChild);
}
// Display answer choices
currentQuestionObj.choices.forEach(function (choice) {
let choiceButton = document.createElement("button");
choiceButton.textContent = choice;
choiceButton.classList.add("choice");
choicesEl.appendChild(choiceButton);
choiceButton.addEventListener("click", function () {
checkAnswer(choice);
});
});
}
// Function to check answer
function checkAnswer(choice) {
let currentQuestionObj = questionSet[currentQuestion];
if (choice === currentQuestionObj.answer) {
score++;
scoreEl.textContent = score;
} else {
timer -= 10; // Deduct 10 seconds from timer for wrong answer
}
currentQuestion++;
if (currentQuestion >= questionSet.length) {
endQuiz();
} else {
displayQuestion();
}
}
// Function to end quiz
function endQuiz() {
clearInterval(countdownTimer);
questionEl.classList.add("hide");
choicesEl.classList.add("hide");
timeEl.classList.add("hide");
initialsEl.classList.remove("hide");
submitButton.classList.remove("hide");
}