@@ -294,30 +294,66 @@ function initQuiz() {
294294 const form = document . getElementById ( 'bihar-quiz' ) ;
295295 if ( ! form ) return ;
296296 const result = document . getElementById ( 'quiz-result' ) ;
297- const answers = {
298- q1 : 'bodh' ,
299- q2 : 'nalanda' ,
300- q3 : 'chhath' ,
301- q4 : 'patna' ,
302- q5 : 'valmiki'
303- } ;
297+ const container = document . getElementById ( 'quiz-questions' ) ;
298+ if ( ! container ) return ;
304299
305- form . addEventListener ( 'submit' , function ( e ) {
306- e . preventDefault ( ) ;
307- let score = 0 ;
308- for ( const [ q , correct ] of Object . entries ( answers ) ) {
309- const group = form . elements [ q ] ;
310- if ( group && group . value === correct ) score ++ ;
311- }
300+ fetch ( 'quiz_questions.json' )
301+ . then ( res => res . json ( ) )
302+ . then ( questions => {
303+ renderQuizQuestions ( container , questions ) ;
304+
305+ const answers = Object . fromEntries ( questions . map ( q => [ q . id , q . answer ] ) ) ;
306+
307+ form . addEventListener ( 'submit' , function ( e ) {
308+ e . preventDefault ( ) ;
309+ let score = 0 ;
310+ for ( const [ q , correct ] of Object . entries ( answers ) ) {
311+ const group = form . elements [ q ] ;
312+ if ( group && group . value === correct ) score ++ ;
313+ }
312314
313- let message = '' ;
314- if ( score === 5 ) message = 'Incredible! You’re a Bihar Boss!' ;
315- else if ( score >= 4 ) message = 'Great job! Bhojpuri Bravo!' ;
316- else if ( score >= 3 ) message = 'Nice! You’re on the Patna path.' ;
317- else if ( score >= 2 ) message = 'Not bad! Keep exploring Bihar.' ;
318- else message = 'Time to tour Bihar’s wonders!' ;
315+ let message = '' ;
316+ if ( score === 5 ) message = 'Incredible! You’re a Bihar Boss!' ;
317+ else if ( score >= 4 ) message = 'Great job! Bhojpuri Bravo!' ;
318+ else if ( score >= 3 ) message = 'Nice! You’re on the Patna path.' ;
319+ else if ( score >= 2 ) message = 'Not bad! Keep exploring Bihar.' ;
320+ else message = 'Time to tour Bihar’s wonders!' ;
321+
322+ if ( result ) result . textContent = 'You scored ' + score + '/5. ' + message ;
323+ } ) ;
324+ } )
325+ . catch ( err => {
326+ console . error ( 'Failed to load quiz questions:' , err ) ;
327+ if ( result ) result . textContent = 'Unable to load quiz. Please try again later.' ;
328+ } ) ;
329+
330+ form . addEventListener ( 'reset' , function ( ) {
331+ if ( result ) result . textContent = '' ;
332+ } ) ;
333+ }
334+
335+ // Render quiz questions dynamically
336+ function renderQuizQuestions ( container , questions ) {
337+ container . innerHTML = '' ;
338+ questions . forEach ( ( q , qi ) => {
339+ const fs = document . createElement ( 'fieldset' ) ;
340+ const legend = document . createElement ( 'legend' ) ;
341+ legend . textContent = ( qi + 1 ) + ') ' + q . question ;
342+ fs . appendChild ( legend ) ;
343+
344+ q . options . forEach ( ( opt , oi ) => {
345+ const label = document . createElement ( 'label' ) ;
346+ const input = document . createElement ( 'input' ) ;
347+ input . type = 'radio' ;
348+ input . name = q . id ;
349+ input . value = opt . value ;
350+ if ( oi === 0 ) input . required = true ; // require a choice per group
351+ label . appendChild ( input ) ;
352+ label . append ( ' ' + opt . label ) ;
353+ fs . appendChild ( label ) ;
354+ } ) ;
319355
320- if ( result ) result . textContent = 'You scored ' + score + '/5. ' + message ;
356+ container . appendChild ( fs ) ;
321357 } ) ;
322358}
323359
0 commit comments