-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
78 lines (71 loc) · 1.82 KB
/
Copy pathscript.js
File metadata and controls
78 lines (71 loc) · 1.82 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
const questions = [
{
question: "5+2=?",
options: ["7", "8", "9", "10"],
answer: "7"
},
{
question: "10/2=?",
options: ["2", "6", "5", "8"],
answer: "5"
},
{
question: "3*7=?",
options: ["20", "28", "14", "21"],
answer: "21"
},
{
question: "8-3=?",
options: ["2", "5", "4", "6"],
answer: "5"
},
{
question: "9+4=?",
options: ["13", "9", "10", "11"],
answer: "13"
}
];
let currentQuestion = 0;
let marks = 0;
const questionElement = document.getElementById("questionElement");
const optionsElement = document.getElementById("optionsElement");
const nextButton = document.getElementById("nextButton");
const resultElement = document.getElementById("resultElement");
function loadQuestion() {
const currentQuiz = questions[currentQuestion];
questionElement.textContent = currentQuiz.question;
optionsElement.innerHTML = "";
currentQuiz.options.forEach(option => {
const li = document.createElement("li");
li.innerHTML = `
<label>
<input type="radio" name="answer" value="${option}" />
${option}
</label>`;
optionsElement.appendChild(li);
});
}
nextButton.addEventListener("click", () => {
const selected = document.querySelector("input[name='answer']:checked");
if (!selected) {
alert("Please select an answer before continuing");
return;
}
const answer = selected.value;
if (answer === questions[currentQuestion].answer) {
marks++;
}
currentQuestion++;
if (currentQuestion < questions.length) {
loadQuestion();
} else {
showResult();
}
}
);
function showResult() {
document.getElementById("quizElement").classList.add("hidden");
resultElement.classList.remove("hidden");
resultElement.textContent = `You scored ${marks} out of ${questions.length}!`;
}
loadQuestion();