Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
115 lines (99 loc) · 2.94 KB
/
Copy pathscript.js
File metadata and controls
115 lines (99 loc) · 2.94 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
const quizData = [
{
question: "1. What does HTML stand for?",
answers: [
"i. Hyper Text Markup Language",
"ii. High Text Machine Language",
"iii. Hyper Transfer Markup Language",
"iv. Home Tool Markup Language"
],
correct: 0
},
{
question: "2. Which language is used for styling web pages?",
answers: [
"i. HTML",
"ii. Python",
"iii. CSS",
"iv. Java"
],
correct: 2
},
{
question: "3. Which keyword is used to declare a variable in JavaScript?",
answers: [
"i. int",
"ii. var",
"iii. string",
"iv. float"
],
correct: 1
},
{
question: "4. Which method is used to select an element by id?",
answers: [
"i. querySelectorAll()",
"ii. getElementById()",
"iii. getElementsByClassName()",
"iv. createElement()"
],
correct: 1
},
{
question: "5. Which symbol is used for comments in JavaScript?",
answers: [
"i. /* */",
"ii. //",
"iii. #",
"iv. <!-- -->"
],
correct: 1
}
];
const question = document.getElementById("question");
const answers = document.getElementById("answers");
const nextBtn = document.getElementById("nextBtn");
let currentQuestion = 0;
let score = 0;
function loadQuestion() {
answers.innerHTML = "";
question.textContent = quizData[currentQuestion].question;
quizData[currentQuestion].answers.forEach(function(answer, index) {
const button = document.createElement("button");
button.textContent = answer;
button.classList.add("answer");
button.addEventListener("click", function() {
checkAnswer(index, button);
});
answers.appendChild(button);
});
}
function checkAnswer(selected, button) {
const correct = quizData[currentQuestion].correct;
const buttons = document.querySelectorAll(".answer");
buttons.forEach(function(btn) {
btn.disabled = true;
});
if (selected === correct) {
score++;
button.classList.add("correct");
} else {
button.classList.add("wrong");
buttons[correct].classList.add("correct");
}
nextBtn.style.display = "block";
}
nextBtn.addEventListener("click", function() {
currentQuestion++;
if (currentQuestion < quizData.length) {
loadQuestion();
nextBtn.style.display = "none";
} else {
document.getElementById("quiz").innerHTML = `
<h2>Your Score</h2>
<h1>${score} / ${quizData.length}</h1>
<button onclick="location.reload()">Play Again</button>
`;
}
});
loadQuestion();