@@ -8,6 +8,7 @@ document.addEventListener('DOMContentLoaded', function() {
88 initInteractiveElements ( ) ;
99 initAccessibility ( ) ;
1010 initProgressiveEnhancement ( ) ;
11+ initQuiz ( ) ; // attach quiz logic if present
1112} ) ;
1213
1314// Navigation enhancements
@@ -288,6 +289,74 @@ function initProgressiveEnhancement() {
288289 }
289290}
290291
292+ // Quiz initialization and logic
293+ function initQuiz ( ) {
294+ const form = document . getElementById ( 'bihar-quiz' ) ;
295+ if ( ! form ) return ;
296+ const result = document . getElementById ( 'quiz-result' ) ;
297+ const container = document . getElementById ( 'quiz-questions' ) ;
298+ if ( ! container ) return ;
299+
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+ }
314+
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+ } ) ;
355+
356+ container . appendChild ( fs ) ;
357+ } ) ;
358+ }
359+
291360// Utility functions for future enhancements
292361const BiharCulture = {
293362 // Notification system
0 commit comments