1+ const startButton = document . getElementById ( 'start-btn' )
2+
3+ const nextButton = document . getElementById ( 'next-btn' )
4+
5+ const questionContainerElement = document . getElementById ( 'question-container' )
6+
7+ const questionElement = document . getElementById ( 'question' )
8+
9+ const answerButtonsElement = document . getElementById ( 'answer-buttons' )
10+
11+ let shuffledQuestions , currentQuestionIndex
12+
13+ startButton . addEventListener ( 'click' , startGame )
14+
15+ nextButton . addEventListener ( 'click' , ( ) => {
16+
17+ currentQuestionIndex ++
18+
19+ setNextQuestion ( )
20+
21+ } )
22+
23+ function startGame ( ) {
24+
25+ startButton . classList . add ( 'hide' )
26+
27+ shuffledQuestions = questions . sort ( ( ) => Math . random ( ) - .5 )
28+
29+ currentQuestionIndex = 0
30+
31+ questionContainerElement . classList . remove ( 'hide' )
32+
33+ setNextQuestion ( )
34+
35+ }
36+
37+ function setNextQuestion ( ) {
38+
39+ resetState ( )
40+
41+ showQuestion ( shuffledQuestions [ currentQuestionIndex ] )
42+
43+ }
44+
45+ function showQuestion ( question ) {
46+
47+ questionElement . innerText = question . question
48+
49+ question . answers . forEach ( answer => {
50+
51+ const button = document . createElement ( 'button' )
52+
53+ button . innerText = answer . text
54+
55+ button . classList . add ( 'btn' )
56+
57+ if ( answer . correct ) {
58+
59+ button . dataset . correct = answer . correct
60+
61+ }
62+
63+ button . addEventListener ( 'click' , selectAnswer )
64+
65+ answerButtonsElement . appendChild ( button )
66+
67+ } )
68+
69+ }
70+
71+ function resetState ( ) {
72+
73+ clearStatusClass ( document . body )
74+
75+ nextButton . classList . add ( 'hide' )
76+
77+ while ( answerButtonsElement . firstChild ) {
78+
79+ answerButtonsElement . removeChild ( answerButtonsElement . firstChild )
80+
81+ }
82+
83+ }
84+
85+ function selectAnswer ( e ) {
86+
87+ const selectedButton = e . target
88+
89+ const correct = selectedButton . dataset . correct
90+
91+ setStatusClass ( document . body , correct )
92+
93+ Array . from ( answerButtonsElement . children ) . forEach ( button => {
94+
95+ setStatusClass ( button , button . dataset . correct )
96+
97+ } )
98+
99+ if ( shuffledQuestions . length > currentQuestionIndex + 1 ) {
100+
101+ nextButton . classList . remove ( 'hide' )
102+
103+ } else {
104+
105+ startButton . innerText = 'Restart'
106+
107+ startButton . classList . remove ( 'hide' )
108+
109+ }
110+
111+ }
112+
113+ function setStatusClass ( element , correct ) {
114+
115+ clearStatusClass ( element )
116+
117+ if ( correct ) {
118+
119+ element . classList . add ( 'correct' )
120+
121+ } else {
122+
123+ element . classList . add ( 'wrong' )
124+
125+ }
126+
127+ }
128+
129+ function clearStatusClass ( element ) {
130+
131+ element . classList . remove ( 'correct' )
132+
133+ element . classList . remove ( 'wrong' )
134+
135+ }
136+
137+ const questions = [
138+
139+ {
140+
141+ question : 'What is 2 + 2?' ,
142+
143+ answers : [
144+
145+ { text : '4' , correct : true } ,
146+
147+ { text : '22' , correct : false }
148+
149+ ]
150+
151+ } ,
152+
153+ {
154+
155+ question : 'Who is the best YouTuber?' ,
156+
157+ answers : [
158+
159+ { text : 'Web Dev Simplified' , correct : true } ,
160+
161+ { text : 'Traversy Media' , correct : true } ,
162+
163+ { text : 'Dev Ed' , correct : true } ,
164+
165+ { text : 'Fun Fun Function' , correct : true }
166+
167+ ]
168+
169+ } ,
170+
171+ {
172+
173+ question : 'Is web development fun?' ,
174+
175+ answers : [
176+
177+ { text : 'Kinda' , correct : false } ,
178+
179+ { text : 'YES!!!' , correct : true } ,
180+
181+ { text : 'Um no' , correct : false } ,
182+
183+ { text : 'IDK' , correct : false }
184+
185+ ]
186+
187+ } ,
188+
189+ {
190+
191+ question : 'What is 4 * 2?' ,
192+
193+ answers : [
194+
195+ { text : '6' , correct : false } ,
196+
197+ { text : '8' , correct : true }
198+
199+ ]
200+
201+ }
202+
203+ ]
0 commit comments