-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
165 lines (154 loc) · 4.54 KB
/
app.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
// Function to start quiz
// Remove start btn, h1, p
// Show quiz container
// Function to go to next question
// Function to select answer
// Create list of questions
// Randomize questions, set current question index
const startBtn = document.getElementById("start-btn");
const nxtBtn = document.getElementById("next-btn");
const quizContainerEl = document.getElementById("quiz-container");
const questionEl = document.getElementById("question");
const h1 = document.querySelector("h1");
const p = document.querySelector("p");
const answersEl = document.getElementById("answers");
let randomQuestion, currentQuestion;
startBtn.addEventListener("click", startQuiz);
nxtBtn.addEventListener("click", () => {
currentQuestion++;
nextQuestion();
});
function startQuiz() {
startBtn.classList.add("hide");
h1.classList.add("hide");
p.classList.add("hide");
randomQuestion = questions.sort(() => Math.random() - 0.5);
currentQuestion = 0;
// Show quiz container/1st question
quizContainerEl.classList.remove("hide");
nextQuestion();
}
function nextQuestion() {
resetState();
showQuestion(randomQuestion[currentQuestion]);
}
function showQuestion(question) {
questionEl.innerText = question.question;
question.answers.forEach((answer) => {
const button = document.createElement("button");
button.innerText = answer.text;
button.classList.add("btn");
if (answer.correct) {
button.dataset.correct = answer.correct;
}
button.addEventListener("click", selectAnswer);
answersEl.appendChild(button);
});
}
function resetState() {
clearStatusClass(document.body);
nxtBtn.classList.add("hide");
while (answersEl.firstChild) {
answersEl.removeChild(answersEl.firstChild);
}
}
function selectAnswer(e) {
const selectedButton = e.target;
const correct = selectedButton.dataset.correct;
setStatusClass(document.body, correct);
Array.from(answersEl.children).forEach((button) => {
setStatusClass(button, button.dataset.correct);
});
if (randomQuestion.length > currentQuestion + 1) {
nxtBtn.classList.remove("hide");
} else {
startBtn.innerText = "Restart";
startBtn.classList.remove("hide");
}
}
function setStatusClass(element, correct) {
clearStatusClass(element);
if (correct) {
element.classList.add("correct");
} else {
element.classList.add("wrong");
}
}
function clearStatusClass(element) {
element.classList.remove("correct");
element.classList.remove("wrong");
}
const questions = [
{
question: "What is scope?",
answers: [
{ text: "The aiming device on a gun", correct: false },
{ text: "Where a variable can be accessed", correct: true },
{
text: "A technique for targeting an element in the DOM",
correct: false,
},
{ text: "A type of JavaScript function", correct: false },
],
},
{
question: "What is the 'this' keyword?",
answers: [
{ text: "This refers to that", correct: false },
{ text: "This refers to the function it belongs to", correct: false },
{ text: "This refers to the object that is belongs to", correct: true },
{ text: "This is confusing", correct: false },
],
},
{
question: "How many different data types are there?",
answers: [
{ text: "6", correct: false },
{ text: "10", correct: false },
{ text: "3", correct: false },
{ text: "7", correct: true },
],
},
{
question: "What are the different data types?",
answers: [
{
text: "Number, string, bowling, undeclared, null, calculation, things",
correct: false,
},
{
text: "Number, symbol, boolean, undefined, null, object, string",
correct: true,
},
{
text: "letter, string, icon, operator, assignment, value, variable",
correct: false,
},
{
text: "Number, letter, division, string, null, undeclared, integer",
correct: false,
},
],
},
{
question: "What is an API?",
answers: [
{
text: "Small units of independent, reusable code that are used as the building blocks for creating a JavaScript application ",
correct: false,
},
{
text: "The combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment)",
correct: false,
},
{
text: "A medium to fetch or send data between interfaces",
correct: true,
},
{
text: "a way of storing data that can be changed later on",
correct: false,
},
],
},
];