1+ let next = document . querySelector ( '.next' ) ;
2+ let form = document . querySelector ( 'form' ) ;
3+ let prev = document . querySelector ( '.prev' ) ;
4+ let quizElm = document . querySelector ( ".quiz" ) ;
5+ let totalQuestions = document . querySelector ( 'header p' ) ;
6+ let showResult = document . querySelector ( '.show_result' ) ;
7+
8+
9+ class Question {
10+ constructor ( title , options , correctAnswerIndex ) {
11+ this . title = title ;
12+ this . options = options ;
13+ this . correctAnswerIndex = correctAnswerIndex ;
14+ }
15+
16+
17+ isCorrect ( answer ) {
18+ return this . options [ this . correctAnswerIndex ] === answer ;
19+ }
20+
21+ getCorrectAnswer ( ) {
22+ return this . options [ this . correctAnswerIndex ]
23+ }
24+ }
25+
26+ class Quiz {
27+ constructor ( questions = [ ] , score = 0 ) {
28+ this . questions = questions ;
29+ this . score = score ;
30+ this . activeIndex = 0 ;
31+ }
32+
33+ nextQuestion ( ) {
34+ this . activeIndex = this . activeIndex + 1 ;
35+ this . createUi ( ) ;
36+
37+ }
38+
39+ prevQuestion ( ) {
40+ this . activeIndex = this . activeIndex - 1 ;
41+ this . createUi ( ) ;
42+
43+ }
44+
45+ incrementScore ( ) {
46+ this . score = this . score + 1 ;
47+ }
48+
49+
50+
51+ addQuestion ( title , options , answerIndex ) {
52+ let question = new Question ( title , options , answerIndex ) ;
53+ this . questions . push ( question ) ;
54+
55+ }
56+
57+ handleButton ( ) {
58+ if ( this . activeIndex === 0 ) {
59+ prev . style . visibility = "hidden" ;
60+ } else if (
61+ this . activeIndex === this . questions . length - 1 ) {
62+ next . style . visibility = "hidden" ;
63+ showResult . style . visibility = "visible" ;
64+ } else {
65+ prev . style . visibility = "visible"
66+ next . style . visibility = "visible"
67+ showResult . style . visibility = "hidden" ;
68+
69+ }
70+ }
71+
72+ createUi ( ) {
73+ quizElm . innerHTML = "" ;
74+ let activeQuestion = this . questions [ this . activeIndex ] ;
75+ let form = document . createElement ( "form" ) ;
76+ let fieldset = document . createElement ( "fieldset" ) ;
77+ let legend = document . createElement ( "legend" ) ;
78+ legend . innerText = activeQuestion . title ;
79+ let optionsElm = document . createElement ( "div" ) ;
80+ optionsElm . classList . add ( "options" ) ;
81+ let buttonWrapper = document . createElement ( "div" ) ;
82+ let button = document . createElement ( "button" ) ;
83+ button . type = "submit"
84+ button . innerText = "Submit" ;
85+ buttonWrapper . append ( button ) ;
86+
87+
88+ activeQuestion . options . forEach ( ( option , index ) => {
89+ let input = document . createElement ( "input" ) ;
90+ let div = document . createElement ( "div" ) ;
91+ input . id = `option-${ index } ` ;
92+ input . type = "radio" ;
93+ input . name = "options" ;
94+ input . value = option ;
95+ let label = document . createElement ( "label" ) ;
96+ label . for = `option-${ index } ` ;
97+ label . innerText = option ;
98+
99+ form . addEventListener ( "submit" , ( event ) => {
100+ event . preventDefault ( ) ;
101+ if ( input . checked ) {
102+ if ( activeQuestion . isCorrect ( input . value ) ) {
103+ this . incrementScore ( ) ;
104+ }
105+ }
106+ } ) ;
107+ div . append ( input , label ) ;
108+ optionsElm . append ( div ) ;
109+
110+ } ) ;
111+
112+ this . handleButton ( ) ;
113+ totalQuestions . innerText = `Total questions : ${ this . questions . length } ` ;
114+
115+ fieldset . append ( legend , optionsElm , buttonWrapper ) ;
116+ form . append ( fieldset ) ;
117+ quizElm . append ( form ) ;
118+
119+ }
120+
121+ }
122+
123+ let quiz = new Quiz ( ) ;
124+ quizCollection . forEach ( ( question ) => {
125+ quiz . addQuestion ( question . title ,
126+ question . options ,
127+ question . answerIndex ) ;
128+
129+ } ) ;
130+
131+ quiz . createUi ( ) ;
132+
133+ next . addEventListener ( "click" , quiz . nextQuestion . bind ( quiz ) ) ;
134+ prev . addEventListener ( "click" , quiz . prevQuestion . bind ( quiz ) ) ;
135+
136+ showResult . addEventListener ( "click" , ( ) => {
137+ alert ( `Your score is ${ quiz . score } ` ) ;
138+ } ) ;
0 commit comments