-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
162 lines (153 loc) · 5.09 KB
/
script.js
File metadata and controls
162 lines (153 loc) · 5.09 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
const tipOffButton = document.getElementById("start-button")
const questionContainerElement = document.getElementById
("question-container")
const questionElement = document.getElementById("question")
const answerElement = document.getElementById("answer-button")
const newQuestion = document.getElementById("next-button")
let shuffledQuestions, currentQuestionIndex
tipOffButton.addEventListener('click', tipOff)
newQuestion.addEventListener('click', () => {
currentQuestionIndex++
nextQuestion()
})
function tipOff() {
console.log('tipoff')
tipOffButton.classList.add('hide')
shuffledQuestions = questions.sort(() => Math.random() - 1)
currentQuestionIndex = 0
questionContainerElement.classList.remove("hide")
nextQuestion()
}
function nextQuestion() {
resetGame()
showQuestion(shuffledQuestions[currentQuestionIndex])
}
function showQuestion(question) {
questionElement.innerText = question.question
question.answers.forEach(answer => {
const button = document.createElement("button")
button.innerText = answer.text
button.classList.add("answer-button")
if (answer.correct) {
button.dataset.correct = answer.correct
}
button.addEventListener("click", selectAnswer)
answerElement.appendChild(button)
})
}
function selectAnswer(e) {
const selectedButton = e.target
const correct = selectedButton.dataset.correct
setStatusClass(document.body, correct)
Array.from(answerElement.children).forEach(button => {
setStatusClass(button, selectedButton.dataset.correct)
})
if (shuffledQuestions.length > currentQuestionIndex + .5) {
newQuestion.classList.remove("hide")
} else {
newQuestion.innerText = "Restart"
newQuestion.classList.remove("hide")
tipOff()
}
}
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")
}
function resetGame() {
clearStatusClass(document.body)
newQuestion.classList.add("hide")
while (answerElement.firstChild) {
answerElement.removeChild
(answerElement.firstChild)
}
}
const questions = [
{
question: "When Halo 3: ODST was unveiled in 2008, it had a different title. What was the game formally called?",
answers: [
{text: "Halo 3: Recon", correct: true },
{text: "Halo 3: Phantom", correct: false },
{text: "Halo 3: Helljumpers", correct: false},
{text: "Halo 3: Guerilla", correct: false}
]
}, {
question: "In what year was the original Sonic the Hedgehog game released?",
answers: [
{text: "1991", correct: true },
{text: "1996", correct: false },
{text: "1993", correct: false},
{text: "1990", correct: false}
]
}, {
question: "Which of these Counter-Strike maps is a bomb defuse scenario?",
answers: [
{text: "Havana", correct: false},
{text: "Oilrig", correct: false },
{text: "747", correct: false},
{text: "Prodigy", correct: true}
]
}, {
question: "What was the first game ever released that ran on the Source engine?",
answers: [
{text: "Counter-Strike: Source", correct: true },
{text: "Half-Life 2", correct: false },
{text: "Garry's Mod", correct: false},
{text: "Team Fortress 2", correct: false}
]
}, {
question: "How many controllers could a Nintendo GameCube have plugged in at one time?",
answers: [
{text: "8", correct: false },
{text: "2", correct: false },
{text: "4", correct: true},
{text: "6", correct: false}
]
}, {
question: "Shang Tsung is a playable character in Mortal Kombat XL",
answers: [
{text: "True", correct: false},
{text: "False", correct: true }
]
}, {
question: "In Pokemon Diamond, Pearl and Platinum, where can a Munchlax be found?",
answers: [
{text: "Honey Trees", correct: true },
{text: "Trading with an NPC", correct: false },
{text: "Grass on Route 209", correct: false},
{text: "Wayward Cave", correct: false}
]
}, {
question: "What year was the video game streaming platform TwitchTV founded?",
answers: [
{text: "2012", correct: false },
{text: "2013", correct: false },
{text: "2010", correct: false},
{text: "2011", correct: true}
]
}, {
question: "Which game was exclusive to Dreamcast?",
answers: [
{text: "Pen Pen TriIcelon", correct: true },
{text: "Perfect Dark", correct: false },
{text: "Tetrisphere", correct: false},
{text: "Sonic Adventure", correct: false}
]
}, {
question: "Which of the following Mario Kart 8 Deluxe items will NOT make you invincible?",
answers: [
{text: "Bullet Bill", correct: false },
{text: "Star", correct: false },
{text: "Boo", correct: false},
{text: "Golden Mushroom", correct: true}
]
}
]