1
- const question = document . getElementById ( " question" ) ;
2
- const choices = Array . from ( document . getElementsByClassName ( " choice-text" ) ) ;
3
- const progressText = document . getElementById ( " progressText" ) ;
4
- const scoreText = document . getElementById ( " score" ) ;
5
- const progressBarFull = document . getElementById ( " progressBarFull" ) ;
6
- const loader = document . getElementById ( " loader" ) ;
7
- const game = document . getElementById ( " game" ) ;
1
+ const question = document . getElementById ( ' question' ) ;
2
+ const choices = Array . from ( document . getElementsByClassName ( ' choice-text' ) ) ;
3
+ const progressText = document . getElementById ( ' progressText' ) ;
4
+ const scoreText = document . getElementById ( ' score' ) ;
5
+ const progressBarFull = document . getElementById ( ' progressBarFull' ) ;
6
+ const loader = document . getElementById ( ' loader' ) ;
7
+ const game = document . getElementById ( ' game' ) ;
8
8
let currentQuestion = { } ;
9
9
let acceptingAnswers = false ;
10
10
let score = 0 ;
@@ -14,101 +14,100 @@ let availableQuesions = [];
14
14
let questions = [ ] ;
15
15
16
16
fetch (
17
- " https://opentdb.com/api.php?amount=10&category=9&difficulty=easy&type=multiple"
17
+ ' https://opentdb.com/api.php?amount=10&category=9&difficulty=easy&type=multiple'
18
18
)
19
- . then ( res => {
20
- return res . json ( ) ;
21
- } )
22
- . then ( loadedQuestions => {
23
- console . log ( loadedQuestions . results ) ;
24
- questions = loadedQuestions . results . map ( loadedQuestion => {
25
- const formattedQuestion = {
26
- question : loadedQuestion . question
27
- } ;
28
-
29
- const answerChoices = [ ...loadedQuestion . incorrect_answers ] ;
30
- formattedQuestion . answer = Math . floor ( Math . random ( ) * 4 ) + 1 ;
31
- answerChoices . splice (
32
- formattedQuestion . answer - 1 ,
33
- 0 ,
34
- loadedQuestion . correct_answer
35
- ) ;
36
-
37
- answerChoices . forEach ( ( choice , index ) => {
38
- formattedQuestion [ "choice" + ( index + 1 ) ] = choice ;
39
- } ) ;
40
-
41
- return formattedQuestion ;
19
+ . then ( ( res ) => {
20
+ return res . json ( ) ;
21
+ } )
22
+ . then ( ( loadedQuestions ) => {
23
+ questions = loadedQuestions . results . map ( ( loadedQuestion ) => {
24
+ const formattedQuestion = {
25
+ question : loadedQuestion . question ,
26
+ } ;
27
+
28
+ const answerChoices = [ ...loadedQuestion . incorrect_answers ] ;
29
+ formattedQuestion . answer = Math . floor ( Math . random ( ) * 4 ) + 1 ;
30
+ answerChoices . splice (
31
+ formattedQuestion . answer - 1 ,
32
+ 0 ,
33
+ loadedQuestion . correct_answer
34
+ ) ;
35
+
36
+ answerChoices . forEach ( ( choice , index ) => {
37
+ formattedQuestion [ 'choice' + ( index + 1 ) ] = choice ;
38
+ } ) ;
39
+
40
+ return formattedQuestion ;
41
+ } ) ;
42
+
43
+ startGame ( ) ;
44
+ } )
45
+ . catch ( ( err ) => {
46
+ console . error ( err ) ;
42
47
} ) ;
43
48
44
- startGame ( ) ;
45
- } )
46
- . catch ( err => {
47
- console . error ( err ) ;
48
- } ) ;
49
-
50
49
//CONSTANTS
51
50
const CORRECT_BONUS = 10 ;
52
51
const MAX_QUESTIONS = 3 ;
53
52
54
53
startGame = ( ) => {
55
- questionCounter = 0 ;
56
- score = 0 ;
57
- availableQuesions = [ ...questions ] ;
58
- getNewQuestion ( ) ;
59
- game . classList . remove ( " hidden" ) ;
60
- loader . classList . add ( " hidden" ) ;
54
+ questionCounter = 0 ;
55
+ score = 0 ;
56
+ availableQuesions = [ ...questions ] ;
57
+ getNewQuestion ( ) ;
58
+ game . classList . remove ( ' hidden' ) ;
59
+ loader . classList . add ( ' hidden' ) ;
61
60
} ;
62
61
63
62
getNewQuestion = ( ) => {
64
- if ( availableQuesions . length === 0 || questionCounter >= MAX_QUESTIONS ) {
65
- localStorage . setItem ( " mostRecentScore" , score ) ;
66
- //go to the end page
67
- return window . location . assign ( " /end.html" ) ;
68
- }
69
- questionCounter ++ ;
70
- progressText . innerText = `Question ${ questionCounter } /${ MAX_QUESTIONS } ` ;
71
- //Update the progress bar
72
- progressBarFull . style . width = `${ ( questionCounter / MAX_QUESTIONS ) * 100 } %` ;
73
-
74
- const questionIndex = Math . floor ( Math . random ( ) * availableQuesions . length ) ;
75
- currentQuestion = availableQuesions [ questionIndex ] ;
76
- question . innerText = currentQuestion . question ;
77
-
78
- choices . forEach ( choice => {
79
- const number = choice . dataset [ " number" ] ;
80
- choice . innerText = currentQuestion [ " choice" + number ] ;
81
- } ) ;
82
-
83
- availableQuesions . splice ( questionIndex , 1 ) ;
84
- acceptingAnswers = true ;
63
+ if ( availableQuesions . length === 0 || questionCounter >= MAX_QUESTIONS ) {
64
+ localStorage . setItem ( ' mostRecentScore' , score ) ;
65
+ //go to the end page
66
+ return window . location . assign ( ' /end.html' ) ;
67
+ }
68
+ questionCounter ++ ;
69
+ progressText . innerText = `Question ${ questionCounter } /${ MAX_QUESTIONS } ` ;
70
+ //Update the progress bar
71
+ progressBarFull . style . width = `${ ( questionCounter / MAX_QUESTIONS ) * 100 } %` ;
72
+
73
+ const questionIndex = Math . floor ( Math . random ( ) * availableQuesions . length ) ;
74
+ currentQuestion = availableQuesions [ questionIndex ] ;
75
+ question . innerText = currentQuestion . question ;
76
+
77
+ choices . forEach ( ( choice ) => {
78
+ const number = choice . dataset [ ' number' ] ;
79
+ choice . innerText = currentQuestion [ ' choice' + number ] ;
80
+ } ) ;
81
+
82
+ availableQuesions . splice ( questionIndex , 1 ) ;
83
+ acceptingAnswers = true ;
85
84
} ;
86
85
87
- choices . forEach ( choice => {
88
- choice . addEventListener ( " click" , e => {
89
- if ( ! acceptingAnswers ) return ;
86
+ choices . forEach ( ( choice ) => {
87
+ choice . addEventListener ( ' click' , ( e ) => {
88
+ if ( ! acceptingAnswers ) return ;
90
89
91
- acceptingAnswers = false ;
92
- const selectedChoice = e . target ;
93
- const selectedAnswer = selectedChoice . dataset [ " number" ] ;
90
+ acceptingAnswers = false ;
91
+ const selectedChoice = e . target ;
92
+ const selectedAnswer = selectedChoice . dataset [ ' number' ] ;
94
93
95
- const classToApply =
96
- selectedAnswer == currentQuestion . answer ? " correct" : " incorrect" ;
94
+ const classToApply =
95
+ selectedAnswer == currentQuestion . answer ? ' correct' : ' incorrect' ;
97
96
98
- if ( classToApply === " correct" ) {
99
- incrementScore ( CORRECT_BONUS ) ;
100
- }
97
+ if ( classToApply === ' correct' ) {
98
+ incrementScore ( CORRECT_BONUS ) ;
99
+ }
101
100
102
- selectedChoice . parentElement . classList . add ( classToApply ) ;
101
+ selectedChoice . parentElement . classList . add ( classToApply ) ;
103
102
104
- setTimeout ( ( ) => {
105
- selectedChoice . parentElement . classList . remove ( classToApply ) ;
106
- getNewQuestion ( ) ;
107
- } , 1000 ) ;
108
- } ) ;
103
+ setTimeout ( ( ) => {
104
+ selectedChoice . parentElement . classList . remove ( classToApply ) ;
105
+ getNewQuestion ( ) ;
106
+ } , 1000 ) ;
107
+ } ) ;
109
108
} ) ;
110
109
111
- incrementScore = num => {
112
- score += num ;
113
- scoreText . innerText = score ;
110
+ incrementScore = ( num ) => {
111
+ score += num ;
112
+ scoreText . innerText = score ;
114
113
} ;
0 commit comments