-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
198 lines (165 loc) · 6.07 KB
/
script.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
193
194
195
196
197
198
const startButton = document.createElement('button');
startButton.innerText = 'Start Exam';
startButton.className = 'button';
startButton.addEventListener('click', startExam);
const contentDiv = document.getElementById('content');
contentDiv.appendChild(startButton);
const dateElement = document.getElementById('date');
const timeElement = document.getElementById('time');
let timeRemaining = 600; // 10 minutes in seconds
let interval;
// Define an array of questions with options and correct answers
const questions = [
{
question: 'What is the capital of France?',
options: ['Paris', 'Berlin', 'Madrid', 'Rome'],
correctAnswer: 'Paris'
},
{
question: 'Which planet is known as the "Red Planet"?',
options: ['Venus', 'Mars', 'Jupiter', 'Saturn'],
correctAnswer: 'Mars'
},
// Add more questions here...
{
question: 'Which planet is known as the "Ring Planet"?',
options: ['Venus', 'Mars', 'Jupiter', 'Saturn'],
correctAnswer: 'Saturn'
},
{
question: 'Which planet is known as the "biggest Planet"?',
options: ['Venus', 'Mars', 'Jupiter', 'Saturn'],
correctAnswer: 'Jupiter'
},
{
question: 'Which planet is the as the closest to sun?',
options: ['Venus', 'Mars', 'Mercury', 'Saturn'],
correctAnswer: 'Mercury'
},
{
question: 'Which planet was once part of the solar system?',
options: ['Venus', 'Mars', 'Jupiter', 'Pluto'],
correctAnswer: 'Pluto'
},
{
question: 'Which planet is known as the "Blue Planet"?',
options: ['Venus', 'Mars', 'Jupiter', 'Earth'],
correctAnswer: 'Earth'
},
{
question: 'Which planet has the most number of moons?',
options: ['Venus', 'Mars', 'Jupiter', 'Saturn'],
correctAnswer: 'Saturn'
},
{
question: 'Which planet is known as the "Red Planet"?',
options: ['Venus', 'Mars', 'Jupiter', 'Saturn'],
correctAnswer: 'Mars'
},
{
question: 'On which planet does it rain diamonds"?',
options: ['Venus', 'Mars', 'Jupiter', 'Uranus'],
correctAnswer: 'Uranus'
},
];
let currentQuestionIndex = 0;
let userAnswers = [];
function updateTime() {
const minutes = Math.floor(timeRemaining / 60);
const seconds = timeRemaining % 60;
timeElement.textContent = `Time: ${minutes}:${seconds.toString().padStart(2, '0')}`;
timeRemaining--;
if (timeRemaining < 0) {
clearInterval(interval);
showTimeoutScreen();
}
}
function startExam() {
contentDiv.innerHTML = ''; // Clear content
// Reset the timer
timeRemaining = 600; // Reset to 10 minutes in seconds
interval = setInterval(updateTime, 1000);
updateTime();
showQuestion(currentQuestionIndex);
}
function showQuestion(questionNumber) {
const questionData = questions[questionNumber];
const questionElement = document.createElement('div');
questionElement.textContent = `Question ${questionNumber + 1}: ${questionData.question}`;
const optionsDiv = document.createElement('div');
optionsDiv.className = 'options'; // Add this line
questionData.options.forEach((option, index) => {
const optionButton = document.createElement('button');
optionButton.className = 'button option-button'; // Add this line
optionButton.textContent = option;
optionButton.addEventListener('click', () => selectAnswer(questionNumber, index));
optionsDiv.appendChild(optionButton);
});
const submitButton = document.createElement('button');
submitButton.className = 'button submit-button'; // Add this line
submitButton.textContent = 'Submit and go to next question';
submitButton.disabled = true;
submitButton.addEventListener('click', () => {
if (currentQuestionIndex < questions.length - 1) {
currentQuestionIndex++;
showQuestion(currentQuestionIndex);
} else {
showResultScreen();
}
});
contentDiv.innerHTML = ''; // Clear previous content
contentDiv.appendChild(questionElement);
contentDiv.appendChild(optionsDiv);
contentDiv.appendChild(submitButton);
}
function selectAnswer(questionNumber, answerIndex) {
userAnswers[questionNumber] = answerIndex;
const submitButton = document.querySelector('.submit-button');
submitButton.disabled = false;
const optionButtons = document.querySelectorAll('.option-button:not([disabled])');
optionButtons.forEach(button => button.disabled = true);
}
function calculateScore() {
let score = 0;
userAnswers.forEach((userAnswer, index) => {
if (userAnswer === questions[index].options.indexOf(questions[index].correctAnswer)) {
score += 10;
}
});
return score;
}
function showResultScreen() {
clearInterval(interval);
contentDiv.innerHTML = '';
const score = calculateScore();
const resultElement = document.createElement('div');
resultElement.textContent = `Your Score: ${score}`;
const restartButton = document.createElement('button');
restartButton.className = 'button';
restartButton.textContent = 'Restart Exam';
restartButton.addEventListener('click', () => {
currentQuestionIndex = 0;
userAnswers = [];
startExam();
});
contentDiv.appendChild(resultElement);
contentDiv.appendChild(restartButton);
}
function showTimeoutScreen() {
contentDiv.innerHTML = '';
const timeoutElement = document.createElement('div');
timeoutElement.textContent = 'Time Out! You did not complete the exam in time.';
const restartButton = document.createElement('button');
restartButton.className = 'button';
restartButton.textContent = 'Restart Exam';
restartButton.addEventListener('click', () => {
currentQuestionIndex = 0;
userAnswers = [];
startExam();
});
contentDiv.appendChild(timeoutElement);
contentDiv.appendChild(restartButton);
}
// Initialize date
const currentDate = new Date();
dateElement.textContent = `Date: ${currentDate.toDateString()}`;