@@ -6,8 +6,6 @@ let intervalId;
66
77/**
88 * Formats time in seconds to MM:SS format
9- * @param {number } totalSeconds - The time in seconds to format
10- * @returns {string } Formatted time string in "Time Remaining: MM:SS" format
119 */
1210function formatTimeDisplay ( totalSeconds ) {
1311 const minutes = Math . floor ( totalSeconds / SECONDS_PER_MINUTE ) ;
@@ -24,15 +22,35 @@ function formatTimeDisplay(totalSeconds) {
2422function setAlarm ( ) {
2523 const input = document . getElementById ( "alarmSet" ) ;
2624 const heading = document . getElementById ( "timeRemaining" ) ;
25+ const errorMsg = document . getElementById ( "alarmError" ) ;
2726
28- let totalSeconds = Number ( input . value ) ;
27+ // Reset alarm sound + flashing background before new countdown
28+ pauseAlarm ( ) ;
2929
30- // Input validation: check for invalid or non-positive numbers
31- if ( isNaN ( totalSeconds ) || totalSeconds <= 0 ) {
32- heading . innerText = "Please enter a valid positive number of seconds" ;
30+ let raw = input . value . trim ( ) ;
31+
32+ // Clear previous error
33+ errorMsg . textContent = "" ;
34+
35+ // STRONG VALIDATION: must be digits only
36+ if ( ! / ^ \d + $ / . test ( raw ) ) {
37+ heading . innerText = "Time Remaining: 00:00" ;
38+ errorMsg . textContent =
39+ "Invalid input. Please enter a whole number of seconds (e.g., 10, 30, 120). Decimals and text are not allowed." ;
40+ return ;
41+ }
42+
43+ let totalSeconds = Number ( raw ) ;
44+
45+ // Prevent extremely large or zero values
46+ if ( totalSeconds === 0 || totalSeconds > 86400 ) {
47+ heading . innerText = "Time Remaining: 00:00" ;
48+ errorMsg . textContent =
49+ "Please enter a value between 1 and 86,400 seconds. Examples: 10, 45, 300." ;
3350 return ;
3451 }
3552
53+ // Reset any existing countdown
3654 clearInterval ( intervalId ) ;
3755
3856 heading . innerText = formatTimeDisplay ( totalSeconds ) ;
@@ -63,14 +81,21 @@ function setup() {
6381 document . getElementById ( "stop" ) . addEventListener ( "click" , ( ) => {
6482 pauseAlarm ( ) ;
6583 } ) ;
84+
85+ // Allow Enter key to trigger alarm
86+ document . getElementById ( "alarmSet" ) . addEventListener ( "keyup" , ( e ) => {
87+ if ( e . key === "Enter" ) setAlarm ( ) ;
88+ } ) ;
6689}
6790
6891function playAlarm ( ) {
6992 audio . play ( ) ;
93+ document . body . classList . add ( "alarm-active" ) ;
7094}
7195
7296function pauseAlarm ( ) {
7397 audio . pause ( ) ;
98+ document . body . classList . remove ( "alarm-active" ) ;
7499}
75100
76101window . onload = setup ;
0 commit comments