@@ -93,75 +93,129 @@ TASK 3
9393 вызывая методы объекта нужно создать dom-элементы
9494*/
9595
96- const quizz = {
97- create ( ) {
98- const wraper = document . createElement ( 'div' ) ;
99- const quizzBlock = document . createElement ( 'div' ) ;
100- const quizTitle = document . createElement ( 'header' ) ;
101- const quizzFooter = document . createElement ( 'footer' ) ;
102- const quizzList = document . createElement ( 'ol' ) ;
103-
104- wraper . setAttribute ( 'class' , 'wraper' ) ;
105- quizzBlock . setAttribute ( 'class' , 'quizz-block' ) ;
106- quizzList . setAttribute ( 'class' , 'quizz-list' ) ;
107- quizTitle . setAttribute ( 'class' , 'quizz-title' ) ;
108- quizTitle . textContent = 'Test of Programming' ;
96+ const questions = [
97+ {
98+ questionName : 'question 1' ,
99+ answers : [
100+ 'answer 1' ,
101+ 'answer 2' ,
102+ 'answer 3'
103+ ] ,
104+ rigthAnswer : [ 0 ]
105+ } ,
106+ {
107+ questionName : 'question 2' ,
108+ answers : [
109+ 'answer 1' ,
110+ 'answer 2' ,
111+ 'answer 3'
112+ ] ,
113+ rigthAnswer : [ 0 ]
114+ } ,
115+ {
116+ questionName : 'question 3' ,
117+ answers : [
118+ 'answer 1' ,
119+ 'answer 2' ,
120+ 'answer 3'
121+ ] ,
122+ rigthAnswer : [ 0 ]
123+ } ,
124+ ] ;
125+
126+ const app = {
127+ testHeaderName : 'Test of Programming' ,
128+
129+ newElem ( tagName ) {
130+ return document . createElement ( tagName ) ;
131+ } ,
132+
133+ setClass ( target , className ) {
134+ target . setAttribute ( 'class' , className ) ;
135+ } ,
136+
137+ setTypeCheckBox ( target ) {
138+ target . setAttribute ( 'type' , 'checkbox' ) ;
139+ } ,
140+
141+ createStructure ( ) {
142+ /*make general structure for insertion*/
143+ this . wraper = this . newElem ( 'div' ) ;
144+ this . quizzBlock = this . newElem ( 'div' ) ;
145+ this . quizTitle = this . newElem ( 'header' ) ;
146+ this . quizzList = this . newElem ( 'ol' ) ;
147+ this . quizzFooter = this . newElem ( 'footer' ) ;
109148
110- const questionTitles = [ 'What is the best program language?' , 'What is your name?' , 'What is the best car?' ] ;
111- const answers = [
112- [ 'JS' , 'JavaScript' , 'HTML' ] ,
113- [ 'Vasya' , 'Petr' , 'Vitya' ] ,
114- [ 'Lada Sedan' , 'Juguli' , 'Priora' ]
115- ] ;
149+ /*bind those with CSS selectors, and assign testHeaderName*/
150+ this . setClass ( this . wraper , 'wraper' ) ;
151+ this . setClass ( this . quizzBlock , 'quizz-block' ) ;
152+ this . setClass ( this . quizTitle , 'quizz-title' ) ;
153+ this . setClass ( this . quizzList , 'quizz-list' ) ;
154+ this . setClass ( this . quizzFooter , 'quizz-footer' ) ;
155+ this . quizTitle . textContent = this . testHeaderName ;
156+ } ,
157+
158+ fillQuizzList ( ) {
159+ const li = this . newElem ( 'li' ) ;
160+ this . setClass ( li , 'question-block' ) ;
116161
117- function fillQuizzList ( ) {
118- for ( let i = 0 ; i < questionTitles . length ; i ++ ) {
119- const li = document . createElement ( 'li' ) ;
120- const header = document . createElement ( 'header' ) ;
121- const main = document . createElement ( 'main' ) ;
162+ questions . forEach ( question => {
163+ const header = this . newElem ( 'header' ) ;
164+ const main = this . newElem ( 'main' ) ;
165+
166+ this . setClass ( header , 'question-title' ) ;
167+ this . setClass ( main , 'question-body' ) ;
168+
169+ header . textContent = question . questionName ;
170+
171+ question . answers . forEach ( answer => {
172+ const label = this . newElem ( 'label' ) ;
173+ const input = this . newElem ( 'input' ) ;
174+ const p = this . newElem ( 'p' ) ;
122175
123- li . setAttribute ( 'class' , 'question-block' ) ;
124- header . setAttribute ( 'class' , 'question-title' ) ;
125- main . setAttribute ( 'class' , 'question-body' ) ;
126- header . textContent = questionTitles [ i ] ;
176+ this . setClass ( label , 'answer' ) ;
177+ this . setTypeCheckBox ( input ) ;
127178
128- for ( let j = 0 ; j < answers [ i ] . length ; j ++ ) {
129- const label = document . createElement ( 'label' ) ;
130- const input = document . createElement ( 'input' ) ;
131- const p = document . createElement ( 'p' ) ;
132-
133- label . setAttribute ( 'class' , 'answer' ) ;
134- p . textContent = answers [ i ] [ j ] ;
135- input . setAttribute ( 'type' , 'checkbox' ) ;
136- label . appendChild ( input ) ;
137- label . appendChild ( p ) ;
138- main . appendChild ( label ) ;
139- }
179+ p . textContent = answer ;
140180
141- li . appendChild ( header ) ;
142- li . appendChild ( main ) ;
143- quizzList . appendChild ( li ) ;
144- }
145- }
146- fillQuizzList ( ) ;
181+ label . appendChild ( input ) ;
182+ label . appendChild ( p ) ;
183+ main . appendChild ( label ) ;
184+ } ) ;
185+
186+ li . appendChild ( header ) ;
187+ li . appendChild ( main ) ;
188+ } ) ;
147189
148- function fillQuizzFooter ( ) {
149- const span = document . createElement ( 'span' ) ;
150- quizzFooter . setAttribute ( 'class' , 'quizz-footer' ) ;
151- span . setAttribute ( 'class' , 'check-answers btn-shadow-effect' ) ;
152- span . textContent = 'Check Answers' ;
153- quizzFooter . appendChild ( span ) ;
154- }
155- fillQuizzFooter ( ) ;
190+ this . quizzList . appendChild ( li ) ;
191+ } ,
192+
193+ fillQuizzFooter ( ) {
194+ const span = this . newElem ( 'span' ) ;
156195
157- quizzBlock . appendChild ( quizTitle ) ;
158- quizzBlock . appendChild ( quizzList ) ;
159- quizzBlock . appendChild ( quizzFooter ) ;
196+ this . setClass ( span , 'check-answers btn-shadow-effect' ) ;
197+ span . textContent = 'Check Answers' ;
160198
161- wraper . appendChild ( quizzBlock ) ;
199+ this . quizzFooter . appendChild ( span ) ;
200+ } ,
201+
202+ compileAllStructure ( ) {
203+ this . createStructure ( ) ;
204+ this . fillQuizzList ( ) ;
205+ this . fillQuizzFooter ( ) ;
162206
163- document . body . appendChild ( wraper ) ;
207+ this . quizzBlock . appendChild ( this . quizTitle ) ;
208+ this . quizzBlock . appendChild ( this . quizzList ) ;
209+ this . quizzBlock . appendChild ( this . quizzFooter ) ;
210+
211+ this . wraper . appendChild ( this . quizzBlock ) ;
212+
213+ document . body . appendChild ( this . wraper ) ;
214+ } ,
215+
216+ render ( ) {
217+ this . compileAllStructure ( ) ;
164218 }
165- }
219+ } ;
166220
167- quizz . create ( ) ;
221+ app . render ( ) ;
0 commit comments