-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz.js
More file actions
47 lines (39 loc) · 1.37 KB
/
quiz.js
File metadata and controls
47 lines (39 loc) · 1.37 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
const quizData = [
{ q: "HTML stands for?", a: ["Hyper Text Markup Language", "High Text Machine Language", "Hyper Transfer Markup"], correct: 0 },
{ q: "Which tag is used for CSS?", a: ["<css>", "<style>", "<script>"], correct: 1 },
{ q: "JavaScript is?", a: ["Backend", "Frontend", "Both"], correct: 2 },
{ q: "Which one is not a programming language?", a: ["Python", "HTML", "Java"], correct: 1 }
];
let index = 0;
let score = 0;
function loadQuestion() {
const q = quizData[index];
document.getElementById("question").innerText = q.q;
const options = document.getElementById("options");
options.innerHTML = "";
q.a.forEach((text, i) => {
options.innerHTML += `
<button class="btn btn-outline-primary w-100 mb-2" onclick="checkAnswer(${i})">${text}</button>
`;
});
}
function checkAnswer(choice) {
if (choice === quizData[index].correct) {
score++;
}
}
function nextQuestion() {
index++;
if (index < quizData.length) {
loadQuestion();
} else {
localStorage.setItem("webdevScore", score);
localStorage.setItem("webdevCompleted", "true");
document.querySelector(".course-card").innerHTML = `
<h4>Quiz Completed 🎉</h4>
<p>Your Score: ${score}/${quizData.length}</p>
<a href="certificate.html" class="course-btn mt-3">Get Certificate</a>
`;
}
}
loadQuestion();