-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
108 lines (95 loc) · 3.23 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
106
107
108
const questions = [
{
question: "CAPITAL OF FRANCE ?",
options: [
"BERLIN",
"Madrid",
"PARIS",
"ROME"
],
answer: 2
},
{
question: "Which of the following is known as the Red Planet?",
options: [
"MARS",
"Earth",
"Venus",
"JUPiter"
],
answer: 0
},
{
question: "Largest ocean on Earth?",
options: [
"Atlantic",
"Pacific",
"INDIAN",
"ARTIC"
],
answer: 1
}
];
let currentQuestionIndex = 0;
let score = 0;
const questionText = document.getElementById("question-text");
const optionsContainer = document.getElementById("options-container");
const nextButton = document.getElementById("next-button");
const prevButton = document.getElementById("prev-button");
const submitButton = document.getElementById("submit-button");
const scoreContainer = document.getElementById("score-container");
const finalScore = document.getElementById("final-score");
function loadQuestion() {
const currentQuestion = questions[currentQuestionIndex];
questionText.textContent = currentQuestion.question;
optionsContainer.innerHTML = "";
currentQuestion.options.forEach((option, index) => {
const label = document.createElement("label");
label.innerHTML = `
<input type="radio" name="option" value="${index}" /> ${option}
`;
optionsContainer.appendChild(label);
});
prevButton.style.display = currentQuestionIndex > 0 ? "inline-block" : "none";
nextButton.style.display = currentQuestionIndex < questions.length - 1 ? "inline-block" : "none";
submitButton.style.display = currentQuestionIndex === questions.length - 1 ? "inline-block" : "none";
}
function handleNextButton() {
const selectedOption = document.querySelector('input[name="option"]:checked');
if (selectedOption) {
if (parseInt(selectedOption.value) === questions[currentQuestionIndex].answer) {
score++;
}
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
loadQuestion();
} else {
displayScore();
}
} else {
alert("Please select an answer!");
}
}
function handlePrevButton() {
if (currentQuestionIndex > 0) {
currentQuestionIndex--;
loadQuestion();
}
}
function displayScore() {
questionText.style.display = "none";
optionsContainer.style.display = "none";
nextButton.style.display = "none";
prevButton.style.display = "none";
submitButton.style.display = "none";
scoreContainer.style.display = "block";
finalScore.textContent = `${score} / ${questions.length}`;
}
function handleSubmit() {
alert("Thanks for taking the quiz!");
location.reload();
}
nextButton.addEventListener("click", handleNextButton);
prevButton.addEventListener("click", handlePrevButton);
submitButton.addEventListener("click", handleSubmit);
loadQuestion();