-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriva.js
134 lines (120 loc) · 3.78 KB
/
triva.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//Create an Array of objects that contain a question, answers object and correct answer.
const questions = [
{
question: "What is the capital of France?",
answers: { A: "Berlin", B: "Paris", C: "Madrid" },
correct: "B",
},
{
question: "Who wrote 'Hamlet'?",
answers: {
A: "Charles Dickens",
B: "William Shakespeare",
C: "Leo Tolstoy",
},
correct: "B",
},
{
question: "What is the largest planet in our Solar System?",
answers: { A: "Earth", B: "Jupiter", C: "Saturn" },
correct: "B",
},
{
question: "What is the chemical symbol for water?",
answers: { A: "O2", B: "H2O", C: "CO2" },
correct: "B",
},
{
question: "Who painted the Mona Lisa?",
answers: {
A: "Vincent Van Gogh",
B: "Pablo Picasso",
C: "Leonardo da Vinci",
},
correct: "C",
},
{
question: "Who styled this app?",
answers: {
A: "Ishmael Sunday",
B: "Pablo Picasso",
C: "Leonardo da Vinci",
},
correct: "A",
},
];
// create variables currentQuestionIndex and score and set each to zero
let currentQuestionIndex = 0;
let score = 0;
// Create a function that load the questions
function loadQuestion() {
const questionContainer = document.getElementById("question-container");
questionContainer.innerHTML = "";
const currentQuestion = questions[currentQuestionIndex];
const questionElement = document.createElement("div");
questionElement.className = "question";
questionElement.innerHTML = `<p>${currentQuestionIndex + 1}. ${
currentQuestion.question
}</p>`;
for (const [key, value] of Object.entries(currentQuestion.answers)) {
questionElement.innerHTML += `
<label>
<input type="radio" name="answer" value="${key}">
${key}) ${value}
</label>
`;
}
questionContainer.appendChild(questionElement);
}
//Validate each answer
function validateAnswer(selectedAnswer) {
const currentQuestion = questions[currentQuestionIndex];
const correctAnswer = currentQuestion.correct;
if (selectedAnswer === correctAnswer) {
score++;
highlightAnswer(selectedAnswer, true);
} else {
highlightAnswer(selectedAnswer, false);
highlightAnswer(correctAnswer, true);
}
// Show the next button
document.getElementById("next-button").classList.remove("hidden");
}
//Highlight both correct answer and incorrect answers
function highlightAnswer(answer, isCorrect) {
const labels = document.querySelectorAll(`input[name="answer"]`);
labels.forEach((label) => {
if (label.value === answer) {
label.parentElement.classList.add(isCorrect ? "correct" : "incorrect");
}
});
}
document.getElementById("quiz-form").addEventListener("change", (e) => {
if (e.target.name === "answer") {
validateAnswer(e.target.value);
// Disable further selection until the answer is validated
document.querySelectorAll('input[name="answer"]').forEach((input) => {
input.disabled = true;
});
}
});
//show "next button" after an answer is selected until the last question
document.getElementById("next-button").addEventListener("click", () => {
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
loadQuestion();
document.getElementById("next-button").classList.add("hidden");
} else {
displayResult();
}
});
// Show final test result
function displayResult() {
const resultDiv = document.getElementById("result");
resultDiv.textContent = `Your total score is ${score} out of ${questions.length}.`;
resultDiv.classList.remove("hidden");
document.getElementById("question-container").classList.add("hidden");
document.getElementById("next-button").classList.add("hidden");
}
// Load the first question
loadQuestion();