@@ -275,92 +275,117 @@ function initNumberGuessing() {
275275 } ) ;
276276
277277 function startGame ( diffKey ) {
278- currentDiff = diffKey ;
279- const config = DIFFICULTIES [ diffKey ] ;
280- maxAttempts = config . attempts ;
281- minRange = config . min ;
282- maxRange = config . max ;
283- secretNumber = Math . floor ( Math . random ( ) * ( config . max - config . min + 1 ) ) + config . min ;
284- attempts = 0 ;
285-
286- // Badge
287- difficultyBadge . textContent = config . label ;
288- difficultyBadge . className = `difficulty-badge ${ diffKey } ` ;
289-
290- // Instruction
291- gameInstructions . textContent =
292- `I'm thinking of a number between ${ config . min } and ${ config . max } !` ;
293-
294- // Input bounds
295- guessInput . min = config . min ;
296- guessInput . max = config . max ;
297- guessInput . value = '' ;
298-
299- // Stats
300- attemptsLeftEl . textContent = maxAttempts ;
301- rangeDisplay . textContent = `${ config . min } –${ config . max } ` ;
302- feedback . textContent = '' ;
303- feedback . style . color = '' ;
304-
305- updateBar ( maxAttempts , maxAttempts ) ;
306- updateBestScore ( diffKey ) ;
307-
308- guessInput . disabled = false ;
309- submitBtn . disabled = false ;
310-
311- difficultyScreen . style . display = 'none' ;
312- gameScreen . style . display = '' ;
313- guessInput . focus ( ) ;
314- }
278+ currentDiff = diffKey ;
279+ const config = DIFFICULTIES [ diffKey ] ;
280+ maxAttempts = config . attempts ;
281+ secretNumber = Math . floor ( Math . random ( ) * ( config . max - config . min + 1 ) ) + config . min ;
282+ attempts = 0 ;
283+ minRange = config . min ;
284+ maxRange = config . max ;
285+ guessInput . min = minRange ;
286+ guessInput . max = maxRange ;
287+
288+ // Badge
289+ difficultyBadge . textContent = config . label ;
290+ difficultyBadge . className = `difficulty-badge ${ diffKey } ` ;
291+
292+ // Instruction
293+ gameInstructions . textContent =
294+ `I'm thinking of a number between ${ config . min } and ${ config . max } !` ;
295+
296+ // Input bounds - IMPORTANT!
297+ guessInput . min = config . min ;
298+ guessInput . max = config . max ;
299+ guessInput . value = '' ;
300+
301+ // Stats
302+ attemptsLeftEl . textContent = maxAttempts ;
303+ rangeDisplay . textContent = `${ config . min } –${ config . max } ` ;
304+ feedback . textContent = '' ;
305+ feedback . style . color = '' ;
306+
307+ updateBar ( maxAttempts , maxAttempts ) ;
308+ updateBestScore ( diffKey ) ;
309+
310+ guessInput . disabled = false ;
311+ submitBtn . disabled = false ;
312+
313+ difficultyScreen . style . display = 'none' ;
314+ gameScreen . style . display = '' ;
315+ guessInput . focus ( ) ;
316+ }
315317
316318 // Gameplay
317319 submitBtn . addEventListener ( 'click' , makeGuess ) ;
318320 guessInput . addEventListener ( 'keydown' , e => { if ( e . key === 'Enter' ) makeGuess ( ) ; } ) ;
319321
320322 function makeGuess ( ) {
321- const config = DIFFICULTIES [ currentDiff ] ;
322- const guess = parseInt ( guessInput . value ) ;
323-
324- if ( isNaN ( guess ) || guess < config . min || guess > config . max ) {
325- feedback . textContent = `⚠️ Please enter a number between ${ config . min } and ${ config . max } !` ;
326- feedback . style . color = 'var(--warning-color)' ;
327- return ;
328- }
323+ const config = DIFFICULTIES [ currentDiff ] ;
324+ const guess = parseInt ( guessInput . value ) ;
325+
326+ // Simple validation - just check if it's a number
327+ if ( isNaN ( guess ) ) {
328+ feedback . textContent = `⚠️ Please enter a valid number!` ;
329+ feedback . style . color = 'var(--warning-color)' ;
330+ guessInput . value = '' ;
331+ guessInput . focus ( ) ;
332+ return ;
333+ }
334+
335+ // Check if guess is within current possible range
336+ if ( guess < minRange || guess > maxRange ) {
337+ feedback . textContent = `⚠️ Please enter a number between ${ minRange } and ${ maxRange } !` ;
338+ feedback . style . color = 'var(--warning-color)' ;
339+ guessInput . value = '' ;
340+ guessInput . focus ( ) ;
341+ return ;
342+ }
329343
330- attempts ++ ;
331- const remaining = maxAttempts - attempts ;
332- attemptsLeftEl . textContent = remaining ;
333- updateBar ( remaining , maxAttempts ) ;
334-
335- if ( guess === secretNumber ) {
336- feedback . textContent = `🎉 Correct! You found it in ${ attempts } attempt${ attempts === 1 ? '' : 's' } !` ;
337- feedback . style . color = 'var(--success-color)' ;
338- guessInput . disabled = true ;
339- submitBtn . disabled = true ;
340- saveBestScore ( currentDiff , attempts ) ;
344+ attempts ++ ;
345+ const remaining = maxAttempts - attempts ;
346+ attemptsLeftEl . textContent = remaining ;
347+ updateBar ( remaining , maxAttempts ) ;
348+
349+ if ( guess === secretNumber ) {
350+ feedback . textContent = `🎉 Correct! You found it in ${ attempts } attempt${ attempts === 1 ? '' : 's' } !` ;
351+ feedback . style . color = 'var(--success-color)' ;
352+ guessInput . disabled = true ;
353+ submitBtn . disabled = true ;
354+ saveBestScore ( currentDiff , attempts ) ;
355+ } else {
356+ if ( guess < secretNumber ) {
357+ feedback . textContent = '📈 Too low! Try higher!' ;
358+ minRange = guess + 1 ;
341359 } else {
342- if ( guess < secretNumber ) {
343- feedback . textContent = '📈 Too low! Try higher!' ;
344- feedback . style . color = 'var(--primary-color)' ;
345- minRange = Math . max ( minRange , guess + 1 ) ;
360+ feedback . textContent = '📉 Too high! Try lower!' ;
361+ maxRange = guess - 1 ;
362+ }
363+
364+ feedback . style . color = guess < secretNumber ? 'var(--primary-color)' : 'var(--danger-color)' ;
365+
366+ // Update the input's min/max attributes
367+ guessInput . min = minRange ;
368+ guessInput . max = maxRange ;
369+
370+ // Display narrowed range
371+ rangeDisplay . textContent = `${ minRange } –${ maxRange } ` ;
372+
373+ if ( remaining <= 1 ) {
374+ if ( remaining === 1 ) {
375+ feedback . textContent = `⚠️ Last attempt! The number is between ${ minRange } and ${ maxRange } ` ;
346376 } else {
347- feedback . textContent = '📉 Too high! Try lower!' ;
348- feedback . style . color = 'var(--danger-color)' ;
349- maxRange = Math . min ( maxRange , guess - 1 ) ;
350- }
351- rangeDisplay . textContent = `${ minRange } –${ maxRange } ` ;
352-
353- if ( remaining <= 0 ) {
354377 feedback . textContent = `💀 Out of attempts! The number was ${ secretNumber } .` ;
355378 feedback . style . color = 'var(--danger-color)' ;
356- guessInput . disabled = true ;
357- submitBtn . disabled = true ;
379+ guessInput . disabled = true ;
380+ submitBtn . disabled = true ;
358381 }
359382 }
360-
361- guessInput . value = '' ;
362383 }
363384
385+ guessInput . value = '' ;
386+ guessInput . focus ( ) ;
387+ }
388+
364389 // Progress bar
365390 function updateBar ( left , total ) {
366391 const pct = ( left / total ) * 100 ;
0 commit comments