@@ -5,17 +5,19 @@ const scoreText = document.getElementById('score');
5
5
const progressBarFull = document . getElementById ( 'progressBarFull' ) ;
6
6
const loader = document . getElementById ( 'loader' ) ;
7
7
const game = document . getElementById ( 'game' ) ;
8
+ const categorySelect = document . getElementById ( 'category-select' ) ;
9
+ const startGameBtn = document . getElementById ( 'start-game' ) ;
10
+ const categorySelection = document . getElementById ( 'category-selection' ) ;
8
11
let currentQuestion = { } ;
9
12
let acceptingAnswers = false ;
10
13
let score = 0 ;
11
14
let questionCounter = 0 ;
12
15
let availableQuesions = [ ] ;
13
-
14
16
let questions = [ ] ;
15
-
16
- fetch (
17
- 'https://opentdb.com/api.php?amount=10&category=9&difficulty=easy&type=multiple'
18
- )
17
+ const TIMER_SECONDS = 15 ;
18
+ let timeLeft = null ;
19
+ let timer = null ;
20
+
19
21
. then ( ( res ) => {
20
22
return res . json ( ) ;
21
23
} )
@@ -24,19 +26,16 @@ fetch(
24
26
const formattedQuestion = {
25
27
question : loadedQuestion . question ,
26
28
} ;
27
-
28
29
const answerChoices = [ ...loadedQuestion . incorrect_answers ] ;
29
30
formattedQuestion . answer = Math . floor ( Math . random ( ) * 4 ) + 1 ;
30
31
answerChoices . splice (
31
32
formattedQuestion . answer - 1 ,
32
33
0 ,
33
34
loadedQuestion . correct_answer
34
35
) ;
35
-
36
36
answerChoices . forEach ( ( choice , index ) => {
37
37
formattedQuestion [ 'choice' + ( index + 1 ) ] = choice ;
38
38
} ) ;
39
-
40
39
return formattedQuestion ;
41
40
} ) ;
42
41
@@ -47,53 +46,94 @@ fetch(
47
46
} ) ;
48
47
49
48
//CONSTANTS
50
- const CORRECT_BONUS = 10 ;
51
- const MAX_QUESTIONS = 3 ;
52
-
53
49
startGame = ( ) => {
54
50
questionCounter = 0 ;
55
51
score = 0 ;
56
52
availableQuesions = [ ...questions ] ;
57
53
getNewQuestion ( ) ;
54
+ categorySelection . classList . add ( 'hidden' ) ;
58
55
game . classList . remove ( 'hidden' ) ;
59
56
loader . classList . add ( 'hidden' ) ;
60
57
} ;
58
+ // Add event listener for category selection
59
+ startGameBtn . addEventListener ( 'click' , ( ) => {
60
+ const selectedCategory = categorySelect . value ;
61
+ loader . classList . remove ( 'hidden' ) ;
62
+ categorySelection . classList . add ( 'hidden' ) ;
63
+
64
+ fetchQuestions ( selectedCategory )
65
+ . then ( ( res ) => res . json ( ) )
66
+ . then ( ( loadedQuestions ) => {
67
+ questions = loadedQuestions . results . map ( ( loadedQuestion ) => {
68
+ const formattedQuestion = {
69
+ question : loadedQuestion . question ,
70
+ } ;
71
+ const answerChoices = [ ...loadedQuestion . incorrect_answers ] ;
72
+ formattedQuestion . answer = Math . floor ( Math . random ( ) * 4 ) + 1 ;
73
+ answerChoices . splice (
74
+ formattedQuestion . answer - 1 ,
75
+ 0 ,
76
+ loadedQuestion . correct_answer
77
+ ) ;
78
+ answerChoices . forEach ( ( choice , index ) => {
79
+ formattedQuestion [ 'choice' + ( index + 1 ) ] = choice ;
80
+ } ) ;
81
+ return formattedQuestion ;
82
+ } ) ;
83
+ startGame ( ) ;
84
+ } )
85
+ . catch ( ( err ) => {
86
+ console . error ( err ) ;
87
+ } ) ;
88
+ } ) ;
89
+ loader . classList . add ( 'hidden' ) ;
90
+
61
91
62
92
getNewQuestion = ( ) => {
63
93
if ( availableQuesions . length === 0 || questionCounter >= MAX_QUESTIONS ) {
64
94
localStorage . setItem ( 'mostRecentScore' , score ) ;
65
- //go to the end page
66
95
return window . location . assign ( '/end.html' ) ;
67
96
}
97
+ // Clear existing timer if any
98
+ if ( timer ) {
99
+ clearInterval ( timer ) ;
100
+ }
68
101
questionCounter ++ ;
69
102
progressText . innerText = `Question ${ questionCounter } /${ MAX_QUESTIONS } ` ;
70
- //Update the progress bar
71
103
progressBarFull . style . width = `${ ( questionCounter / MAX_QUESTIONS ) * 100 } %` ;
72
-
73
104
const questionIndex = Math . floor ( Math . random ( ) * availableQuesions . length ) ;
74
105
currentQuestion = availableQuesions [ questionIndex ] ;
75
106
question . innerHTML = currentQuestion . question ;
76
-
77
- choices . forEach ( ( choice ) => {
107
+ choices . forEach ( choice => {
78
108
const number = choice . dataset [ 'number' ] ;
79
109
choice . innerHTML = currentQuestion [ 'choice' + number ] ;
80
110
} ) ;
81
-
82
111
availableQuesions . splice ( questionIndex , 1 ) ;
83
112
acceptingAnswers = true ;
113
+ // Start timer
114
+ timeLeft = TIMER_SECONDS ;
115
+ document . getElementById ( 'timer' ) . textContent = timeLeft ;
116
+ timer = setInterval ( ( ) => {
117
+ timeLeft -- ;
118
+ document . getElementById ( 'timer' ) . textContent = timeLeft ;
119
+ if ( timeLeft <= 0 ) {
120
+ clearInterval ( timer ) ;
121
+ getNewQuestion ( ) ; // Move to next question when time runs out
122
+ }
123
+ } , 1000 ) ;
84
124
} ;
85
-
125
+ choices . forEach ( ( choice ) => {
86
126
choices . forEach ( ( choice ) => {
87
127
choice . addEventListener ( 'click' , ( e ) => {
88
128
if ( ! acceptingAnswers ) return ;
129
+ // Clear the timer
130
+ clearInterval ( timer ) ;
89
131
90
132
acceptingAnswers = false ;
91
133
const selectedChoice = e . target ;
92
134
const selectedAnswer = selectedChoice . dataset [ 'number' ] ;
93
-
94
135
const classToApply =
95
136
selectedAnswer == currentQuestion . answer ? 'correct' : 'incorrect' ;
96
-
97
137
if ( classToApply === 'correct' ) {
98
138
incrementScore ( CORRECT_BONUS ) ;
99
139
}
0 commit comments