44export class Quiz {
55 constructor ( dataQA = [ ] ) {
66 this . dataQA = dataQA ;
7- // initial game values
87 this . QUESTION_VALUE = 100 ;
98 this . QUESTIONS_AMOUNT = dataQA . length ;
109 this . barPercetage = 0 ;
1110 this . score = 0 ;
1211 this . answer = "" ;
1312 this . canClick = true ;
14- // runs here becasue we want to load the first round of questions
13+ this . noOfCorrect = 0 ;
14+ this . TOTAL_CORRECT = 0 ;
15+
1516 this . _renderNewQuestion ( ) ;
1617 }
1718
18- // if there's questions left, return the last one
1919 _newQuestion ( ) {
2020 if ( this . getQuestionsLen === 0 ) this . _endGame ( ) ;
2121 return this . dataQA . pop ( ) ;
2222 }
2323
24- // takes care of managing the percentage green bar on top
2524 _renderPercentage ( ) {
2625 const leftQA = this . dataQA . length ;
2726 const percentage = ( 1 - leftQA / this . QUESTIONS_AMOUNT ) * 100 ;
2827
2928 document . getElementById ( "progressBarFull" ) . style . width = `${ percentage } %` ;
3029 }
3130
32- // need to calculate it based on the total amount, and remaining amount
3331 _renderQuestionNumber ( ) {
3432 const leftQA = this . dataQA . length ;
3533 const currentQA = this . QUESTIONS_AMOUNT - leftQA ;
@@ -38,7 +36,6 @@ export class Quiz {
3836 paragraph . textContent = `Question ${ currentQA } of ${ this . QUESTIONS_AMOUNT } ` ;
3937 }
4038
41- // get's a new question from newQuestion & renders it to the page
4239 _renderNewQuestion ( ) {
4340 const currentQA = this . _newQuestion ( ) ;
4441 if ( ! currentQA ) {
@@ -54,43 +51,47 @@ export class Quiz {
5451
5552 this . answer = currentQA [ "answer" ] ;
5653
57- // render question && all options
5854 document . getElementById ( "question" ) . textContent = currentQA [ "question" ] ;
5955
6056 pArray . forEach ( ( p , i ) => {
6157 p . textContent = currentQA [ `choice${ i + 1 } ` ] ;
6258 } ) ;
6359 }
6460
65- // sends the user to the highscore.html and asks if they want to save their score
6661 _endGame ( ) {
67- // console.log("GAME ENDED");
6862 window . localStorage . setItem ( "mostRecentScore" , this . score ) ;
63+ window . localStorage . setItem ( "noofanswerscorrect" , this . noOfCorrect ) ;
64+ window . localStorage . setItem ( "noofquestions" , this . QUESTIONS_AMOUNT ) ;
65+
6966 window . location . assign ( "/pages/end.html" ) ;
7067 }
68+
69+
70+
71+ _updateCorrectCount ( ) {
72+ this . TOTAL_CORRECT ++ ;
73+ console . log ( "Total Correct:" , this . TOTAL_CORRECT ) ;
74+ }
7175
7276 checkAnswer ( selected = 0 , correct = 0 ) {
7377 const p = document . querySelector ( `[data-number="${ selected } "]` ) ;
7478
7579 if ( ! this . canClick ) {
76- // console.log("WAIT THERE BOI");
7780 return ;
7881 }
7982
8083 if ( selected === correct ) {
8184 p . parentElement . classList . add ( "correct" ) ;
8285 this . score += this . QUESTION_VALUE ;
83- // console.log("NOICE: ", this.score);
86+ this . noOfCorrect ++ ;
87+ this . _updateCorrectCount ( ) ;
8488 } else {
8589 p . parentElement . classList . add ( "incorrect" ) ;
8690 this . score -= this . QUESTION_VALUE ;
87- // console.log("BAKA GA!: ", this.score);
8891 }
8992
90- // render updated score
9193 document . getElementById ( "score" ) . textContent = this . score ;
9294
93- // delay stuff
9495 this . canClick = false ;
9596
9697 setTimeout ( ( ) => {
0 commit comments