-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCQ.html
151 lines (140 loc) · 6.16 KB
/
MCQ.html
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html>
<head>
<title>Multiple Choice Quiz</title>
<style>
body {
font-family: Arial, sans-serif;
}
.quiz-container {
width: 600px;
margin: auto;
}
.question {
margin-bottom: 20px;
}
.timer {
font-size: 20px;
margin-bottom: 20px;
}
.results {
display: none;
}
.correct-answer {
color: green;
}
.wrong-answer {
color: red;
}
</style>
</head>
<body>
<div class="quiz-container">
<h1>Multiple Choice Quiz</h1>
<div class="timer">Time left: <span id="timer">60</span> seconds</div>
<form id="quizForm">
<div class="question">
<p>1. What is the capital of France?</p>
<input type="radio" name="q1" value="a"> A) Berlin<br>
<input type="radio" name="q1" value="b"> B) Madrid<br>
<input type="radio" name="q1" value="c"> C) Paris<br>
<input type="radio" name="q1" value="d"> D) Rome<br>
<input type="text" id="answer1" class="answer" readonly>
</div>
<div class="question">
<p>2. Who wrote 'To Kill a Mockingbird'?</p>
<input type="radio" name="q2" value="a"> A) Harper Lee<br>
<input type="radio" name="q2" value="b"> B) J.K. Rowling<br>
<input type="radio" name="q2" value="c"> C) Ernest Hemingway<br>
<input type="radio" name="q2" value="d"> D) Mark Twain<br>
<input type="text" id="answer2" class="answer" readonly>
</div>
<div class="question">
<p>3. What is the smallest prime number?</p>
<input type="radio" name="q3" value="a"> A) 1<br>
<input type="radio" name="q3" value="b"> B) 2<br>
<input type="radio" name="q3" value="c"> C) 3<br>
<input type="radio" name="q3" value="d"> D) 5<br>
<input type="text" id="answer3" class="answer" readonly>
</div>
<div class="question">
<p>4. What is the chemical symbol for water?</p>
<input type="radio" name="q4" value="a"> A) H<br>
<input type="radio" name="q4" value="b"> B) O<br>
<input type="radio" name="q4" value="c"> C) H2O<br>
<input type="radio" name="q4" value="d"> D) CO2<br>
<input type="text" id="answer4" class="answer" readonly>
</div>
<div class="question">
<p>5. Who painted the Mona Lisa?</p>
<input type="radio" name="q5" value="a"> A) Vincent van Gogh<br>
<input type="radio" name="q5" value="b"> B) Pablo Picasso<br>
<input type="radio" name="q5" value="c"> C) Leonardo da Vinci<br>
<input type="radio" name="q5" value="d"> D) Claude Monet<br>
<input type="text" id="answer5" class="answer" readonly>
</div>
<button type="submit">Submit</button>
</form>
<div id="results" class="results">
<h2>Your Score: <span id="score"></span>/5</h2>
</div>
</div>
<script>
const correctAnswers = {
q1: 'c',
q2: 'a',
q3: 'b',
q4: 'c',
q5: 'c'
};
function showCorrectAnswer(question, answerId, correctAnswer) {
const selectedAnswer = document.querySelector(`input[name=${question}]:checked`);
if (selectedAnswer) {
if (selectedAnswer.value !== correctAnswer) {
alert(`Wrong answer for question ${question.slice(1)}. Correct answer is option ${correctAnswer.toUpperCase()}.`);
document.getElementById(answerId).value = `Correct Answer: Option ${correctAnswer.toUpperCase()}`;
document.getElementById(answerId).classList.add('wrong-answer');
} else {
document.getElementById(answerId).value = `Correct!`;
document.getElementById(answerId).classList.add('correct-answer');
}
} else {
alert(`You didn't answer question ${question.slice(1)}. Correct answer is option ${correctAnswer.toUpperCase()}.`);
document.getElementById(answerId).value = `Correct Answer: Option ${correctAnswer.toUpperCase()}`;
document.getElementById(answerId).classList.add('wrong-answer');
}
}
function startTimer(duration, display) {
let timer = duration, minutes, seconds;
const interval = setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = seconds;
if (--timer < 0) {
clearInterval(interval);
document.getElementById("quizForm").submit();
}
}, 1000);
}
window.onload = function () {
const oneMinute = 60 * 1,
display = document.querySelector('#timer');
startTimer(oneMinute, display);
};
document.getElementById('quizForm').onsubmit = function(event) {
event.preventDefault();
let score = 0;
for (let question in correctAnswers) {
showCorrectAnswer(question, `answer${question.slice(1)}`, correctAnswers[question]);
if (document.querySelector(`input[name=${question}]:checked`)?.value === correctAnswers[question]) {
score++;
}
}
document.getElementById('results').style.display = 'block';
document.getElementById('score').textContent = score;
};
</script>
</body>
</html>