@@ -23,6 +23,10 @@ function getMathQuizHTML() {
2323 <span class="stat-label">Level</span>
2424 <span class="stat-value" id="quizLevel">🟢 Easy</span>
2525 </div>
26+ <div class="stat">
27+ <span class="stat-label">Timer</span>
28+ <span class="stat-value" id="quizTimer">⏳ 10</span>
29+ </div>
2630 </div>
2731
2832 <div class="quiz-board" id="quizBoard">
@@ -329,6 +333,7 @@ function initMathQuiz() {
329333 const scoreEl = document . getElementById ( 'quizScore' ) ;
330334 const streakEl = document . getElementById ( 'quizStreak' ) ;
331335 const levelEl = document . getElementById ( 'quizLevel' ) ;
336+ const timerEl = document . getElementById ( 'quizTimer' ) ;
332337 const board = document . getElementById ( 'quizBoard' ) ;
333338 const optWrap = document . getElementById ( 'quizOptions' ) ;
334339 const msgEl = document . getElementById ( 'quizMessage' ) ;
@@ -337,12 +342,65 @@ function initMathQuiz() {
337342
338343 // ── state ─────────────────────────────────────────────
339344 let lives , score , streak , bestStreak , difficulty , total , gameRunning ;
345+ let timer ;
346+ let timeLeft ;
340347
341348 function resetState ( ) {
342349 lives = 3 ; score = 0 ; streak = 0 ;
343350 bestStreak = 0 ; difficulty = 1 ;
344351 total = 0 ; gameRunning = false ;
345352 }
353+ function getTimerDuration ( ) {
354+ if ( streak >= 9 ) return 5 ;
355+ if ( streak >= 6 ) return 6 ;
356+ if ( streak >= 3 ) return 8 ;
357+ return 10 ;
358+ }
359+ function updateTimerColor ( ) {
360+ if ( timeLeft <= 2 ) {
361+ timerEl . style . color = '#ff3b30' ;
362+ } else if ( timeLeft <= 5 ) {
363+ timerEl . style . color = '#ff9500' ;
364+ } else {
365+ timerEl . style . color = '#34c759' ;
366+ }
367+ }
368+
369+ function startTimer ( ) {
370+ clearInterval ( timer ) ;
371+
372+ timeLeft = getTimerDuration ( ) ;
373+ timerEl . textContent = `⏳ ${ timeLeft } ` ;
374+ updateTimerColor ( ) ;
375+
376+ timer = setInterval ( ( ) => {
377+ timeLeft -- ;
378+
379+ timerEl . textContent = `⏳ ${ timeLeft } ` ;
380+ updateTimerColor ( ) ;
381+
382+ if ( timeLeft <= 0 ) {
383+ clearInterval ( timer ) ;
384+ handleTimeout ( ) ;
385+ }
386+ } , 1000 ) ;
387+ }
388+
389+ function handleTimeout ( ) {
390+ lives -- ;
391+ streak = 0 ;
392+
393+ msgEl . style . color = 'var(--danger-color)' ;
394+ msgEl . textContent = '⏰ Time Up!' ;
395+
396+ updateHUD ( ) ;
397+
398+ if ( lives <= 0 ) {
399+ setTimeout ( gameOver , 900 ) ;
400+ } else {
401+ setTimeout ( showQuestion , 1200 ) ;
402+ }
403+ }
346404
347405 function updateHUD ( ) {
348406 livesEl . textContent = '❤️ ' . repeat ( lives ) + '🖤 ' . repeat ( 3 - lives ) ;
@@ -376,18 +434,20 @@ function initMathQuiz() {
376434 } ) ;
377435
378436 updateHUD ( ) ;
437+ startTimer ( ) ;
379438 }
380439
381440 function handleAnswer ( chosen , correctIdx , correct , chosenVal ) {
441+ clearInterval ( timer ) ;
382442 const btns = optWrap . querySelectorAll ( '.quiz-option-btn' ) ;
383443 btns . forEach ( b => b . disabled = true ) ;
384444 btns [ correctIdx ] . classList . add ( 'correct' ) ;
385445
386446 if ( chosen === correctIdx ) {
387447 streak ++ ;
388448 bestStreak = Math . max ( bestStreak , streak ) ;
389- score += 10 ;
390- let msg = `✅ Correct! +10 ` ;
449+ score += 10 + timeLeft ;
450+ let msg = `✅ Correct! +${ 10 + timeLeft } ` ;
391451
392452 if ( [ 3 , 6 , 9 ] . includes ( streak ) ) {
393453 score += 5 ;
@@ -415,6 +475,9 @@ function initMathQuiz() {
415475 }
416476
417477 function gameOver ( ) {
478+ clearInterval ( timer ) ;
479+ timerEl . textContent = '⏳ 0' ;
480+ timerEl . style . color = '#ff3b30' ;
418481 gameRunning = false ;
419482 optWrap . innerHTML = '' ;
420483 board . innerHTML = `
@@ -441,6 +504,11 @@ function initMathQuiz() {
441504
442505 startBtn . addEventListener ( 'click' , startGame ) ;
443506 resetBtn . addEventListener ( 'click' , ( ) => {
507+ clearInterval ( timer ) ;
508+ gameRunning = false ;
509+ timeLeft = 10 ;
510+ timerEl . textContent = '⏳ 10' ;
511+ timerEl . style . color = '#34c759' ;
444512 resetState ( ) ;
445513 updateHUD ( ) ;
446514 board . innerHTML = '<p class="quiz-start-msg">Press Start to Play! 🎮</p>' ;
0 commit comments