-
Notifications
You must be signed in to change notification settings - Fork 4
/
2-jsquiz-fancy.html
206 lines (174 loc) · 5.62 KB
/
2-jsquiz-fancy.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
200
201
202
203
204
205
206
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Quiz.js</title>
<style>
body {
margin: 1rem auto;
padding: 3rem;
font-family: sans-serif;
}
header {
width: 50%;
margin: 1em auto;
}
main {
min-width: 25rem;
max-width: 50%;
margin: 0px auto;
display:flex;
flex-direction: column;
}
#statement {
border: 1px solid black;
border-radius: 0.5rem;
box-shadow: 5px 5px 5px gray;
padding: 1rem;
font-size: x-large;
text-align: center;
margin: 1rem 0px;
min-height: 2em;
}
#explanation, #score {
padding: 1rem;
text-align: center;
}
#options {
width: 100%;
display: flex;
flex-direction: column;
}
button {
padding: 0.5rem;
font-size: medium;
border-radius: 5px;
}
.correct {
background-color: lightgreen;
}
.incorrect {
background-color: lightpink;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<header>
<h1>Quiz.js</h1>
<p>Do you know JS? Find out!</p>
<div id="score">
Score: <span id="correct">0</span> / <span id="completed">0</span>
</div>
</header>
<main>
<div id="statement">
</div>
<div id="options">
<button name="true" value="true" >true</button>
<button name="false" value="false" >false</button>
</div>
<div id="explanation" class="hidden">
</div>
<button id="next-question" name="next-question" >Next Question</button>
</main>
</body>
<script type="text/javascript">
const facts = [
{
"statement": "JavaScript was invented in 1995",
"answer": "true",
"explanation": "Brendan Eich created JS at Netscape in 1995. The initial version of the language was written in just 10 days."
},
{
"statement": "Strings in JS are editable values",
"answer": "false",
"explanation": "In JavaScript strings are immutable values, meaning they cannot be edited; however, they can replaced with new, different strings."
},
{
"statement": "1 + 1 === 2",
"answer": "true",
"explanation": "The plus operator gives the sum of two numbers."
},
{
"statement": "'1' + '1' === '2'",
"answer": "false",
"explanation": "The plus operator concatenates (joins together) strings, so '1' + '1' === '11'."
},
{
"statement": "typeof ['J', 'S'] === 'array'",
"answer": "false",
"explanation": "Arrays have the type 'object'. In JS, everything is either a primitive data type (e.g. 'string', 'number') or an object. Arrays are a kind of object with some special properties. "
}
];
function hide(element) {
element.classList.add("hidden");
}
function show(element) {
element.classList.remove("hidden");
}
function disable(button) {
button.setAttribute("disabled", "");
}
function enable(button) {
button.removeAttribute("disabled");
}
let correct = 0;
let completed = 0;
let fact;
const explanation = document.getElementById("explanation");
const nextButton = document.getElementById("next-question");
const optionButtons = document.getElementById("options").children;
function getNextFact() {
fact = facts.shift(); // get the first fact in our array (shortening the array)
// set the question text to the current fact's statement
document.getElementById("statement").textContent = fact.statement;
// hide any previous explanation
hide(explanation);
for (let option of optionButtons) {
// clear any previous classes
option.classList.remove("correct");
option.classList.remove("incorrect");
// make sure buttons are enabled
enable(option);
}
// disable next-question button
disable(nextButton);
}
nextButton.addEventListener("click", getNextFact);
for (let option of optionButtons) {
option.addEventListener("click", e => {
// When this option is clicked...
// disable all the option buttons
for (let button of optionButtons) {
disable(button);
}
// enable the 'next question' button, if we still have facts left
if (facts.length > 0) {
enable(nextButton);
} else {
nextButton.textContent = "No more questions!"
}
const guess = e.target.value;
if (guess === fact.answer) {
// correct answer!
e.target.classList.add("correct");
correct += 1;
} else {
// wrong answer!
e.target.classList.add("incorrect");
}
// display the explanation
explanation.textContent = fact.explanation;
show(explanation);
// update the score
completed += 1;
document.getElementById("correct").textContent = correct;
document.getElementById("completed").textContent = completed;
})
}
getNextFact();
</script>
</html>