File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11let remainingSeconds = 0 ;
22let intervalId ;
3+
4+ function formatTime ( totalSeconds ) {
5+ const minutes = Math . floor ( totalSeconds / 60 ) . toString ( ) . padStart ( 2 , "0" ) ;
6+ const seconds = Math . floor ( totalSeconds % 60 ) . toString ( ) . padStart ( 2 , "0" ) ;
7+ return `${ minutes } :${ seconds } ` ;
8+ }
39function setAlarm ( ) {
410 clearInterval ( intervalId ) ;
511
612 const initialSeconds = Number ( document . getElementById ( "alarmSet" ) . value ) ;
13+ const timeRemainingHeading = document . getElementById ( "timeRemaining" ) ;
14+ if ( ! initialSeconds || initialSeconds <= 0 ) {
15+ timeRemainingHeading . innerText = "Please enter a positive number of seconds" ;
16+ return ;
17+ }
718 remainingSeconds = initialSeconds ;
819
9- let minutesDisplay = Math . floor ( initialSeconds / 60 )
10- . toString ( )
11- . padStart ( 2 , "0" ) ;
12- let secondsDisplay = Math . floor ( initialSeconds % 60 )
13- . toString ( )
14- . padStart ( 2 , "0" ) ;
15- let timeRemainingHeading = document . getElementById ( "timeRemaining" ) ;
16- timeRemainingHeading . innerText = `Time Remaining: ${ minutesDisplay } :${ secondsDisplay } ` ;
20+ timeRemainingHeading . innerText = `Time Remaining: ${ formatTime ( remainingSeconds ) } ` ;
1721
1822 intervalId = setInterval ( ( ) => {
1923 remainingSeconds -- ;
20- let currentMinutes = Math . floor ( remainingSeconds / 60 )
21- . toString ( )
22- . padStart ( 2 , "0" ) ;
23- let currentSeconds = Math . floor ( remainingSeconds % 60 )
24- . toString ( )
25- . padStart ( 2 , "0" ) ;
26- timeRemainingHeading . innerText = `Time Remaining: ${ currentMinutes } :${ currentSeconds } ` ;
24+ timeRemainingHeading . innerText = `Time Remaining: ${ formatTime ( remainingSeconds ) } ` ;
2725 if ( remainingSeconds === 0 ) {
2826 clearInterval ( intervalId ) ;
2927 playAlarm ( ) ;
You can’t perform that action at this time.
0 commit comments