1- // Use a constant for the one-second interval to make the code easier to read.
21const ONE_SECOND_IN_MILLISECONDS = 1000 ;
32
4- // Defines variables to track the state of the alarm clock.
5- // Uses 'let' because these values will change as the timer runs.
63let timeRemainingInSeconds = 0 ;
74let alarmTimerIdentifier = null ;
85
9- // DOM Elements (will be fetched dynamically in functions to avoid load issues)
10-
116/**
12- * Formats a number of seconds into a standard "MM:SS" string.
13- * This helps the user read the time easily (e.g., "01:30" instead of "90").
14- *
15- * @param {number } totalSeconds - The total number of seconds to format.
16- * @returns {string } A string formatted as "MM:SS".
7+ * Formats seconds into "MM:SS".
178 */
189function formatTime ( totalSeconds ) {
19- // Calculates the number of full minutes.
2010 const minutes = Math . floor ( totalSeconds / 60 ) ;
21-
22- // Calculates the remaining seconds after removing the minutes.
2311 const seconds = totalSeconds % 60 ;
2412
25- // Adds a leading zero if the minutes are less than 10.
26- // This ensures the clock always looks like "09:00" rather than "9:00".
2713 let formattedMinutes = `${ minutes } ` ;
2814 if ( minutes < 10 ) {
2915 formattedMinutes = `0${ minutes } ` ;
3016 }
3117
32- // Adds a leading zero if the seconds are less than 10.
3318 let formattedSeconds = `${ seconds } ` ;
3419 if ( seconds < 10 ) {
3520 formattedSeconds = `0${ seconds } ` ;
@@ -40,71 +25,62 @@ function formatTime(totalSeconds) {
4025
4126/**
4227 * Updates the display and checks if the alarm should sound.
43- * This function runs every second.
4428 */
4529function updateTime ( ) {
46- // Finds the title element on the web page to update the text.
4730 const titleElement = document . getElementById ( "timeRemaining" ) ;
4831
49- // Updates the text inside the title element with the new formatted time.
32+ timeRemainingInSeconds = timeRemainingInSeconds - 1 ;
5033 titleElement . innerText = formatTime ( timeRemainingInSeconds ) ;
5134
52- // Checks if the countdown has finished.
5335 if ( timeRemainingInSeconds === 0 ) {
54- // Plays the alarm sound to alert the user.
5536 playAlarm ( ) ;
56-
57- // Changes the background colour to red (visual alert) by adding a CSS class.
5837 document . body . classList . add ( "flash" ) ;
59- } else {
60- // If time is not up, decreases the remaining time by exactly one second.
61- timeRemainingInSeconds = timeRemainingInSeconds - 1 ;
38+ clearInterval ( alarmTimerIdentifier ) ;
6239 }
6340}
6441
6542/**
66- * Starts the alarm countdown based on the time the user entered.
67- * This function runs when the "Set Alarm" button is clicked.
43+ * Initialises the alarm countdown.
6844 */
6945function setAlarm ( ) {
70- // check if a timer is already active.
71- // If so, stops the current timer to prevent two timers running at once.
7246 if ( alarmTimerIdentifier ) {
7347 clearInterval ( alarmTimerIdentifier ) ;
74-
75- // Resets the background colour in case it was already flashing.
7648 document . body . classList . remove ( "flash" ) ;
7749 }
7850
79- // Finds the input box where the user typed the number of seconds.
8051 const timeInput = document . getElementById ( "alarmSet" ) ;
52+ const startingTime = parseInt ( timeInput . value , 10 ) ;
8153
82- // Converts the text input into a whole number (integer).
83- const startingTime = parseInt ( timeInput . value ) ;
84-
85- // Validates the input: ensuring it is a number.
8654 if ( isNaN ( startingTime ) ) {
8755 return ;
8856 }
8957
90- // Validates the input: ensuring it is not negative.
9158 if ( startingTime < 0 ) {
9259 return ;
9360 }
9461
95- // Updates the state variable with the new start time.
9662 timeRemainingInSeconds = startingTime ;
9763
98- // Updates the screen immediately so the user sees the start time without delay.
64+ // Initialise the audio context to enable autoplay.
65+ audio . play ( ) ;
66+ audio . pause ( ) ;
67+
9968 const titleElement = document . getElementById ( "timeRemaining" ) ;
10069 titleElement . innerText = formatTime ( timeRemainingInSeconds ) ;
10170
102- // Starts a repeating timer that calls 'updateTime' every second (1000ms).
10371 alarmTimerIdentifier = setInterval ( ( ) => {
10472 updateTime ( ) ;
10573 } , ONE_SECOND_IN_MILLISECONDS ) ;
10674}
10775
76+ // Add a listener to stop the flashing effect without modifying the protected pauseAlarm function below.
77+ const stopButton = document . getElementById ( "stop" ) ;
78+ if ( stopButton ) {
79+ stopButton . addEventListener ( "click" , ( ) => {
80+ document . body . classList . remove ( "flash" ) ;
81+ } ) ;
82+ }
83+
10884// DO NOT EDIT BELOW HERE
10985
11086var audio = new Audio ( "alarmsound.mp3" ) ;
0 commit comments