-
Notifications
You must be signed in to change notification settings - Fork 0
/
english.js
192 lines (171 loc) · 5.69 KB
/
english.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const quizData = [
{
question: 'What is the plural of "child"?',
options: ['childs', 'childes', 'children', 'childens'],
answer: 'children',
},
{
question: 'Which word means the opposite of "brave"?',
options: ['fearless', 'courageous', 'timid', 'bold'],
answer: 'timid',
},
{
question: 'What does the idiom "hit the nail on the head" mean?',
options: [
'To miss the target',
'To accurately describe or identify something',
'To avoid a problem',
'To be confused',
],
answer: 'To accurately describe or identify something',
},
{
question: 'What is the past participle of "eat"?',
options: ['eated', 'ate', 'eaten', 'eating'],
answer: 'eaten',
},
{
question: 'Which sentence is grammatically incorrect?',
options: [
'She speaks French fluently.',
'They has been waiting for hours.',
'We haven\'t seen each other since last year.',
'He will not come to the party.',
],
answer: 'They has been waiting for hours.',
},
{
question: 'What is the correct spelling?',
options: ['Accomodate', 'Accommodate', 'Acommodate', 'Accomadate'],
answer: 'Accommodate',
},
{
question: 'What part of speech is the word "quickly"?',
options: ['Noun', 'Verb', 'Adverb', 'Adjective'],
answer: 'Adverb',
},
{
question: 'Which sentence uses the correct form of the verb "to be"?',
options: [
'She were happy to see him.',
'We am going to the cinema tonight.',
'They was waiting for the bus.',
'He is studying for his exam.',
],
answer: 'He is studying for his exam.',
},
{
question: 'What is the meaning of the word "ubiquitous"?',
options: ['Rare', 'Widespread', 'Dangerous', 'Delicious'],
answer: 'Widespread',
},
{
question: 'What is the plural of "mouse" (the animal)?',
options: ['Mouses', 'Mice', 'Micees', 'Mousees'],
answer: 'Mice',
},
];
const quizContainer = document.getElementById('quiz');
const resultContainer = document.getElementById('result');
const submitButton = document.getElementById('submit');
const retryButton = document.getElementById('retry');
const showAnswerButton = document.getElementById('showAnswer');
let currentQuestion = 0;
let score = 0;
let incorrectAnswers = [];
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function displayQuestion() {
const questionData = quizData[currentQuestion];
const questionElement = document.createElement('div');
questionElement.className = 'question';
questionElement.innerHTML = questionData.question;
const optionsElement = document.createElement('div');
optionsElement.className = 'options';
const shuffledOptions = [...questionData.options];
shuffleArray(shuffledOptions);
for (let i = 0; i < shuffledOptions.length; i++) {
const option = document.createElement('label');
option.className = 'option';
const radio = document.createElement('input');
radio.type = 'radio';
radio.name = 'quiz';
radio.value = shuffledOptions[i];
const optionText = document.createTextNode(shuffledOptions[i]);
option.appendChild(radio);
option.appendChild(optionText);
optionsElement.appendChild(option);
}
quizContainer.innerHTML = '';
quizContainer.appendChild(questionElement);
quizContainer.appendChild(optionsElement);
}
function checkAnswer() {
const selectedOption = document.querySelector('input[name="quiz"]:checked');
if (selectedOption) {
const answer = selectedOption.value;
if (answer === quizData[currentQuestion].answer) {
score++;
} else {
incorrectAnswers.push({
question: quizData[currentQuestion].question,
incorrectAnswer: answer,
correctAnswer: quizData[currentQuestion].answer,
});
}
currentQuestion++;
selectedOption.checked = false;
if (currentQuestion < quizData.length) {
displayQuestion();
} else {
displayResult();
}
}
}
function displayResult() {
quizContainer.style.display = 'none';
submitButton.style.display = 'none';
retryButton.style.display = 'inline-block';
showAnswerButton.style.display = 'inline-block';
resultContainer.innerHTML = `You scored ${score} out of ${quizData.length}!`;
}
function retryQuiz() {
currentQuestion = 0;
score = 0;
incorrectAnswers = [];
quizContainer.style.display = 'block';
submitButton.style.display = 'inline-block';
retryButton.style.display = 'none';
showAnswerButton.style.display = 'none';
resultContainer.innerHTML = '';
displayQuestion();
}
function showAnswer() {
quizContainer.style.display = 'none';
submitButton.style.display = 'none';
retryButton.style.display = 'inline-block';
showAnswerButton.style.display = 'none';
let incorrectAnswersHtml = '';
for (let i = 0; i < incorrectAnswers.length; i++) {
incorrectAnswersHtml += `
<p>
<strong>Question:</strong> ${incorrectAnswers[i].question}<br>
<strong>Your Answer:</strong> ${incorrectAnswers[i].incorrectAnswer}<br>
<strong>Correct Answer:</strong> ${incorrectAnswers[i].correctAnswer}
</p>
`;
}
resultContainer.innerHTML = `
<p>You scored ${score} out of ${quizData.length}!</p>
<p>Incorrect Answers:</p>
${incorrectAnswersHtml}
`;
}
submitButton.addEventListener('click', checkAnswer);
retryButton.addEventListener('click', retryQuiz);
showAnswerButton.addEventListener('click', showAnswer);
displayQuestion();