-
Notifications
You must be signed in to change notification settings - Fork 483
Expand file tree
/
Copy pathindex.js
More file actions
212 lines (176 loc) · 7.98 KB
/
index.js
File metadata and controls
212 lines (176 loc) · 7.98 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
document.addEventListener("DOMContentLoaded", () => {
/************ HTML ELEMENTS ************/
// View divs
const quizView = document.querySelector("#quizView");
const endView = document.querySelector("#endView");
// Quiz view elements
const progressBar = document.querySelector("#progressBar");
const questionCount = document.querySelector("#questionCount");
const questionContainer = document.querySelector("#question");
const choiceContainer = document.querySelector("#choices");
const nextButton = document.querySelector("#nextButton");
// End view elements
const resultContainer = document.querySelector("#result");
/************ SET VISIBILITY OF VIEWS ************/
// Show the quiz view (div#quizView) and hide the end view (div#endView)
quizView.style.display = "block";
endView.style.display = "none";
/************ QUIZ DATA ************/
// Array with the quiz questions
const questions = [
new Question("What is 2 + 2?", ["3", "4", "5", "6"], "4", 1),
new Question(
"What is the capital of France?",
["Miami", "Paris", "Oslo", "Rome"],
"Paris",
1
),
new Question(
"Who created JavaScript?",
["Plato", "Brendan Eich", "Lea Verou", "Bill Gates"],
"Brendan Eich",
2
),
new Question(
"What is the mass–energy equivalence equation?",
["E = mc^2", "E = m*c^2", "E = m*c^3", "E = m*c"],
"E = mc^2",
3
),
// Add more questions here
];
const quizDuration = 120; // 120 seconds (2 minutes)
/************ QUIZ INSTANCE ************/
// Create a new Quiz instance object
const quiz = new Quiz(questions, quizDuration, quizDuration);
// Shuffle the quiz questions
quiz.shuffleQuestions();
/************ SHOW INITIAL CONTENT ************/
// Convert the time remaining in seconds to minutes and seconds, and pad the numbers with zeros if needed
const minutes = Math.floor(quiz.timeRemaining / 60)
.toString()
.padStart(2, "0");
const seconds = (quiz.timeRemaining % 60).toString().padStart(2, "0");
// Display the time remaining in the time remaining container
const timeRemainingContainer = document.getElementById("timeRemaining");
timeRemainingContainer.innerText = `${minutes}:${seconds}`;
// Show first question
showQuestion();
/************ TIMER ************/
let timer;
timer = setInterval(() => {
quiz.timeRemaining--;
const minutes = Math.floor(quiz.timeRemaining / 60)
.toString()
.padStart(2, "0");
const seconds = (quiz.timeRemaining % 60).toString().padStart(2, "0");
const timeRemainingContainer = document.getElementById("timeRemaining");
timeRemainingContainer.innerText = `${minutes}:${seconds}`;
if (quiz.timeRemaining <= 0) {
clearInterval(timer);
showEndView();
}
}, 1000);
/************ EVENT LISTENERS ************/
nextButton.addEventListener("click", nextButtonHandler);
/************ FUNCTIONS ************/
// showQuestion() - Displays the current question and its choices
// nextButtonHandler() - Handles the click on the next button
// showResults() - Displays the end view and the quiz results
function showQuestion() {
// If the quiz has ended, show the results
if (quiz.hasEnded()) {
showResults();
return;
}
// Clear the previous question text and question choices
questionContainer.innerText = "";
choiceContainer.innerHTML = "";
// Get the current question from the quiz by calling the Quiz class method `getQuestion()`
const question = quiz.getQuestion();
// Shuffle the choices of the current question by calling the method 'shuffleChoices()' on the question object
question.shuffleChoices();
// YOUR CODE HERE:
//
// 1. Show the question
questionContainer.innerText = question.text;
// Update the inner text of the question container element and show the question text
// 2. Update the green progress bar
// Update the green progress bar (div#progressBar) width so that it shows the percentage of questions answered
const percentsNumber =
((quiz.currentQuestionIndex + 1) / quiz.questions.length) * 100;
progressBar.style.width = `${percentsNumber}%`; // This value is hardcoded as a placeholder
// 3. Update the question count text
// Update the question count (div#questionCount) show the current question out of total questions
questionCount.innerText = `${quiz.currentQuestionIndex + 1} of ${
quiz.questions.length
}`;
showChoices(question);
// 4. Create and display new radio input element with a label for each choice.
// Loop through the current question `choices`.
// For each choice create a new radio input with a label, and append it to the choice container.
// Each choice should be displayed as a radio input element with a label:
/*
<input type="radio" name="choice" value="CHOICE TEXT HERE">
<label>CHOICE TEXT HERE</label>
<br>
*/
// Hint 1: You can use the `document.createElement()` method to create a new element.
// Hint 2: You can use the `element.type`, `element.name`, and `element.value` properties to set the type, name, and value of an element.
// Hint 3: You can use the `element.appendChild()` method to append an element to the choices container.
// Hint 4: You can use the `element.innerText` property to set the inner text of an element.
function showChoices(question) {
const choicesContainer = document.querySelector("#choices");
choicesContainer.innerHTML = "";
question.choices.forEach((choice) => {
// const input = document.createElement("input");
// input.type = "radio";
// input.name = "choice";
// input.value = choice;
// const label = document.createElement("label");
// label.innerText = choice;
// const br = document.createElement("br");
// choicesContainer.appendChild(input);
// choicesContainer.appendChild(label);
// choicesContainer.appendChild(br);
const myDiv = document.createElement("div");
myDiv.innerHTML = `<input type="radio" name="choice" value="${choice}">
<label>${choice}</label>
<br>`;
choicesContainer.appendChild(myDiv);
});
}
}
function nextButtonHandler() {
let selectedAnswer; // A variable to store the selected answer value
// 1. Get all the choice elements. You can use the `document.querySelectorAll()` method.
const allChoices = document.querySelectorAll("input");
console.log(allChoices);
allChoices.forEach((oneChoice) => {
if (oneChoice.checked) {
selectedAnswer = oneChoice.value;
}
});
quiz.checkAnswer(selectedAnswer);
console.log(quiz.correctAnswers);
quiz.moveToNextQuestion();
showQuestion();
// 2. Loop through all the choice elements and check which one is selected
// Hint: Radio input elements have a property `.checked` (e.g., `element.checked`).
// When a radio input gets selected the `.checked` property will be set to true.
// You can use check which choice was selected by checking if the `.checked` property is true.
// 3. If an answer is selected (`selectedAnswer`), check if it is correct and move to the next question
// Check if selected answer is correct by calling the quiz method `checkAnswer()` with the selected answer.
// Move to the next question by calling the quiz method `moveToNextQuestion()`.
// Show the next question by calling the function `showQuestion()`.
}
function showResults() {
// YOUR CODE HERE:
// 1. Hide the quiz view (div#quizView)
quizView.style.display = "none";
// 2. Show the end view (div#endView)
endView.style.display = "flex";
// 3. Update the result container (div#result) inner text to show the number of correct answers out of total questions
resultContainer.innerText = `You scored ${quiz.correctAnswers} out of ${quiz.questions.length} correct answers!`; // This value is hardcoded as a placeholder
}
});