-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz3.html
199 lines (179 loc) · 5.21 KB
/
quiz3.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
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
<div id="quiz"></div>
<button id="submit">Submit Quiz</button>
<div id="results"></div>
<button id="main" onclick="document.location='mainpage2.html'">Back to main page</button>
<style>
body{
font-size: 20px;
font-family: sans-serif;
color: #333;
}
.question{
font-weight: 600;
}
.answers {
margin-bottom: 20px;
}
.answers label{
display: block;
}
#main{
font-family: sans-serif;
font-size: 20px;
background-color: #279;
color: #fff;
border: 0px;
border-radius: 3px;
padding: 20px;
cursor: pointer;
margin-bottom: 20px;
}
#main:hover{
background-color: #38a;
}
#submit{
font-family: sans-serif;
font-size: 20px;
background-color: #279;
color: #fff;
border: 0px;
border-radius: 3px;
padding: 20px;
cursor: pointer;
margin-bottom: 20px;
}
#submit:hover{
background-color: #38a;
}
</style>
<script>
(function(){
function buildQuiz(){
// variable to store the HTML output
const output = [];
// for each question...
myQuestions.forEach(
(currentQuestion, questionNumber) => {
// variable to store the list of possible answers
const answers = [];
// and for each available answer...
for(letter in currentQuestion.answers){
// ...add an HTML radio button
answers.push(
`<label>
<input type="radio" name="question${questionNumber}" value="${letter}">
${letter} :
${currentQuestion.answers[letter]}
</label>`
);
}
// add this question and its answers to the output
output.push(
`<div class="question"> ${currentQuestion.question} </div>
<div class="answers"> ${answers.join('')} </div>`
);
}
);
// finally combine our output list into one string of HTML and put it on the page
quizContainer.innerHTML = output.join('');
}
function showResults(){
// gather answer containers from our quiz
const answerContainers = quizContainer.querySelectorAll('.answers');
// keep track of user's answers
let numCorrect = 0;
// for each question...
myQuestions.forEach( (currentQuestion, questionNumber) => {
// find selected answer
const answerContainer = answerContainers[questionNumber];
const selector = `input[name=question${questionNumber}]:checked`;
const userAnswer = (answerContainer.querySelector(selector) || {}).value;
// if answer is correct
if(userAnswer === currentQuestion.correctAnswer){
// add to the number of correct answers
numCorrect++;
// color the answers green
answerContainers[questionNumber].style.color = 'lightgreen';
}
// if answer is wrong or blank
else{
// color the answers red
answerContainers[questionNumber].style.color = 'red';
}
});
// show number of correct answers out of total
//resultsContainer.innerHTML = `${numCorrect} out of ${myQuestions.length}`;
if (numCorrect < 4) {
resultsContainer.innerHTML = `${numCorrect} out of ${myQuestions.length}
Better luck next time! Refresh this page to try again. Try reviewing some of the tabs on the main page to help you learn!`;
}
else if (numCorrect == 4) {
resultsContainer.innerHTML = `${numCorrect} out of ${myQuestions.length}
Nice work! Almost perfect, try again?`;
}
else if (numCorrect == 5) {
resultsContainer.innerHTML = `${numCorrect} out of ${myQuestions.length}
Wow! You're an expert! Maybe it's time for you to start doing your own research!`;
}
}
const quizContainer = document.getElementById('quiz');
const resultsContainer = document.getElementById('results');
const submitButton = document.getElementById('submit');
const myQuestions = [
{
question: "Which of the following is NOT a source of renewable energy?",
answers: {
a: "Tidal",
b: "Geothermal",
c: "Biomass",
d: "Natural gas"
},
correctAnswer: "d"
},
{
question: "What is the technical name for a solar panel?",
answers: {
a: "Photoelectric cell",
b: "Photovoltaic cell",
c: "Photosynthesis cell",
d: "Photosensitive cell"
},
correctAnswer: "b"
},
{
question: "Which chemical is most widely used as a basis for rechargeable batteries?",
answers: {
a: "Lithium",
b: "Nickel",
c: "Cadmium",
d: "Lead"
},
correctAnswer: "a"
},
{
question: "What is the technical term for 'energy per unit of electricity'?",
answers: {
a: "Current",
b: "Voltage",
c: "Power",
d: "Force"
},
correctAnswer: "b"
},
{
question: "How much of Australia's electricity was supplied by solar power in 2019?",
answers: {
a: "21%",
b: "7%",
c: "6%",
d: "0.8%"
},
correctAnswer: "c"
}
];
// Kick things off
buildQuiz();
// Event listeners
submitButton.addEventListener('click', showResults);
})();
</script>