1
1
//JS Global Vars
2
2
var questionsIndex = 0 ;
3
- var startTime = 75 ;
3
+ var timer ;
4
+ var timerCount ;
5
+ var right = true ;
6
+ var wrong = false ;
4
7
5
8
// TOPS SECTION VARS
6
9
var tops = document . getElementById ( "tops" ) ;
7
10
var topHeader = document . getElementById ( "leaderboard" ) ;
8
11
var highScores = document . getElementById ( "highScores" ) ;
9
- var timeAtZero = document . getElementById ( "timer " ) ;
12
+ var timeAtZero = document . getElementById ( "timeAtZero " ) ;
10
13
11
14
// INTRO SECTION VARS
12
15
var containerIntro = document . getElementById ( "container-intro" ) ;
@@ -17,6 +20,7 @@ var containerQuestions = document.getElementById("container-questions");
17
20
var questionTitle = document . getElementById ( "questionTitle" ) ;
18
21
var questionChoice = document . getElementById ( "choices" ) ;
19
22
var answer = document . getElementById ( "answer" ) ;
23
+ var rightOrWrong = document . getElementById ( "rightOrWrong" )
20
24
var timerCountdown = document . getElementById ( "timerCountdown" ) ;
21
25
22
26
//RESULTS SECTION
@@ -63,37 +67,71 @@ var questions = [
63
67
64
68
// Helper Timer Function
65
69
function countdown ( ) {
66
- startTime -- ; //Decreases the timer by 1 second from 75
67
- timerCountdown . textContent = startTime ; //This line displays the text + remaining time in timer
68
- if ( startTime <= 0 ) { //If timer reaches 0 then the end game fuction is called and the game stops
69
- endGame ( ) ;
70
- }
70
+ timer = setInterval ( function ( ) {
71
+ timerCount -- ;
72
+ timerCountdown . textContent = timerCount ;
73
+ if ( timerCount == 0 ) { //This line displays the text + remaining time in timer
74
+ timerCount = finalScore . textContent ;
75
+ document . getElementById ( 'finalScore' ) . textContent ;
76
+ // clearInterval(timer);
77
+ endGame ( ) ;
78
+ }
79
+ } , 1000 ) ;
71
80
}
72
81
73
- startButton . addEventListener ( "click" , startQuiz ) ;
82
+ startButton . addEventListener ( "click" , startQuiz ) ; //when press srat button we call the startQuiz function
74
83
75
84
// 1. INTRO SECTION STARTS
76
85
function startQuiz ( ) {
77
- containerIntro . setAttribute ( "class" , "hide" ) ;
78
- timeAtZero . setAttribute ( "class" , "hide" ) ;
79
- containerQuestions . setAttribute ( "class" , "show" ) ; //Questions section are visible
80
- var timerInterval = setInterval ( countdown , 1000 ) ; // Sets interval in timer var
81
- timerCountdown . textContent = startTime ; //Timer is activated
82
- getQuestions ( ) ; //get questions function is called
86
+ timerCount = 76 ;
87
+ timeAtZero . style . display = "none" ; //Corner timer at zero hides
88
+ containerIntro . style . display = "none" ; //Container intro hides
89
+ startButton . disabled = true ; //start button is diasbled
90
+ containerQuestions . setAttribute ( "class" , "show" ) ; //Questions section becomes visible
91
+ countdown ( ) ;
92
+ getQuestions ( ) ; //get questions function is called
83
93
}
94
+ // event listener for questions
95
+ containerQuestions . addEventListener ( "click" , function ( event ) {
96
+ console . log ( event ) //check what is happening on event
97
+ console . dir ( event . target ) //Display prooerties of the event target
98
+ if ( event . target . matches ( 'button' ) ) {
99
+ console . log ( 'on question index ' , questionsIndex , ' of ' , ( questions . length - 1 ) ) // Another checkpoint: get user answer which is not event.target but event.target.textContent
100
+ var userAnswer = event . target . textContent . slice ( 2 ) ; // Get rid off number in the beginning of string to effectively compare
101
+ var correctAnswer = questions [ questionsIndex ] . answer ; // get the correct answer
102
+ if ( userAnswer === correctAnswer ) { //if user choice matches answer
103
+ document . getElementById ( 'rightOrWrong' ) . innerHTML = "Correct!" ; //Print Correct
104
+ } else {
105
+ document . getElementById ( 'rightOrWrong' ) . innerHTML = "Wrong!" //else Print Wrong
106
+ }
107
+ questionsIndex = questionsIndex + 1 ; // increment question index
108
+ getQuestions ( ) ; // got to next questions
109
+ }
110
+ } ) ;
84
111
85
112
// 2. QUESTIONS + ANSWERS SECTION
86
113
function getQuestions ( ) {
87
- var currentQ = questions [ questionsIndex ] ; //local variable to hold index of each question in the questions array
88
- questionTitle . textContent = currentQ . title ; //Returning Title variable content
89
- questionChoice . innerHTML = "" ; //clearing the variable
90
- currentQ . choices . forEach ( function ( choice , index ) { //iterating through the choices
91
- var choiceBtn = document . createElement ( "button" ) ; //created button element on demand for each of the choices
92
- choiceBtn . setAttribute ( "value" , choice ) ; //collecting all choices in array choice
93
- choiceBtn . textContent = index + 1 + ". " + choice ; //Displaying the choices with # values in a list
94
- choiceBtn . onclick = rightOrWrongAnswer ; //Calling the right or wrong function to check if answer is correct
95
- questionChoice . appendChild ( choiceBtn ) ; //Inserts selection into the var choiceBtn
96
- } )
114
+ if ( questionsIndex < questions . length ) {
115
+ var currentQ = questions [ questionsIndex ] ; //local variable to hold index of each question in the questions array
116
+ questionTitle . textContent = currentQ . title ; //Returning Title variable content
117
+ questionChoice . innerHTML = "" ; //clearing the variable
118
+ currentQ . choices . forEach ( function ( choice , index ) { //iterating through the choices
119
+ var choiceBtn = document . createElement ( "button" ) ; //created button element on demand for each of the choices
120
+ choiceBtn . setAttribute ( "value" , choice ) ; //collecting all choices in array choice
121
+ choiceBtn . textContent = index + 1 + "." + choice ; //Displaying the choices with # values in a list
122
+ choiceBtn . onclick = rightOrWrongAnswer ; //Calling the right or wrong function to check if answer is correct
123
+ questionChoice . appendChild ( choiceBtn ) ; //Inserts selection into the var choiceBtn
124
+ } )
125
+ } else {
126
+ startButton . disabled = true ; //start button is diasbled
127
+ containerQuestions . setAttribute ( "class" , "hide" ) ;
128
+ results . setAttribute ( "class" , "show" ) ;
129
+ results . style . display = "block" ;
130
+ timerCount = document . getElementById ( "finalScore" ) . innerHTML ;
131
+ document . getElementById ( 'finalScore' ) . textContent = finalScore ;
132
+ finalScore . textContent ;
133
+ console . log ( finalScore ) ;
134
+ }
97
135
}
98
136
99
137
// Helper function
@@ -104,8 +142,8 @@ function rightOrWrongAnswer(event) {
104
142
return ; //stay on question page
105
143
}
106
144
//Keeps count of correct answers
107
- if ( btn . value !== questions [ questionsIndex ] . answer ) { //if the choice does not match the right answer
108
- startTime = startTime - 1 ; //time is decreased by 1
145
+ if ( btn . value != questions [ questionsIndex ] . answer ) { //if the choice does not match the right answer
146
+ startTime = startTime - 5 ; //time is decreased by 1
109
147
if ( startTime <= 0 ) { //if timer is at zero
110
148
startTime = 0 ; //then start time is set to zero
111
149
}
@@ -118,7 +156,21 @@ function rightOrWrongAnswer(event) {
118
156
}
119
157
questionsIndex ++ ; //move to the next question
120
158
if ( ( startTime <= 0 ) || ( questionsIndex === questions . length ) ) { //if start time is zero or there are no more questions to go through
121
- gameEnd ( ) ; // end the game
159
+ startButton . disabled = true ; //start button is diasbled
160
+ containerQuestions . setAttribute ( "class" , "hide" ) ;
161
+ results . style . display = "block" ; //Questions section becomes visible
162
+ console . log ( finalScore ) ;
163
+ // endGame(); // end the game
164
+ } else if ( ( startTime > 0 ) && ( questionsIndex === questions . length ) ) {
165
+ // endGame();
166
+ startButton . disabled = true ; //start button is diasbled
167
+ containerQuestions . setAttribute ( "class" , "hide" ) ;
168
+ results . style . display = "block" ;
169
+ timerCount = finalScore ;
170
+ // document.getElementById('finalScore').innerHTML = finalScore;
171
+ // console.log(finalScore);
172
+ // clearInterval(timer);
173
+ // endGame();
122
174
}
123
175
else {
124
176
getQuestions ( ) ; // else continue
@@ -127,15 +179,18 @@ function rightOrWrongAnswer(event) {
127
179
128
180
// Updates and prints finalScore on results div id=results
129
181
function finalScore ( ) {
130
- finalScore . textContent ;
182
+ timerCount = finalScore . textContent ;
183
+ // document.getElementById('finalScore').textContent;
131
184
}
132
185
133
186
//Used at the results id section page
134
187
function endGame ( ) {
135
- clearInterval ( timerInterval ) ; //The interval is stopped
136
- results . removeAttribute ( 'class' ) ; //The results section shows up
188
+ clearInterval ( timerInterval ) . style . display = "hide" ; //The interval is stopped
189
+ results . style . display = "contents" ;
190
+ //results.removeAttribute('class'); //The results section shows up
137
191
containerQuestions . setAttribute ( 'class' , 'hide' ) ; // Questions dissappear
138
- finalScore . textContent = startTime ; //shows time as the score
192
+ // finalScore.textContent = startTime; //shows time as the score
193
+ console . log ( finalScore ) ;
139
194
initials . textContent = initials ; //shows initials
140
195
}
141
196
0 commit comments