File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- function setAlarm ( ) { }
1+ // Turns a number of seconds into "MM:SS" format
2+ // e.g. 75 -> "01:15"
3+ function formatTime ( seconds ) {
4+ const minutes = Math . floor ( seconds / 60 ) ;
5+ const remainingSeconds = seconds % 60 ;
6+
7+ // padStart makes sure single digits get a leading zero e.g. 5 -> "05"
8+ const mm = String ( minutes ) . padStart ( 2 , "0" ) ;
9+ const ss = String ( remainingSeconds ) . padStart ( 2 , "0" ) ;
10+
11+ return mm + ":" + ss ;
12+ }
13+
14+ function setAlarm ( ) {
15+ // Get the number the user typed in the input field
16+ let seconds = Number ( document . getElementById ( "alarmSet" ) . value ) ;
17+
18+ // Update the heading to show the starting time
19+ document . getElementById ( "timeRemaining" ) . innerText =
20+ "Time Remaining: " + formatTime ( seconds ) ;
21+
22+ // Count down every 1000ms (1 second)
23+ let intervalId = setInterval ( function ( ) {
24+ seconds = seconds - 1 ;
25+
26+ // Update the heading each second
27+ document . getElementById ( "timeRemaining" ) . innerText =
28+ "Time Remaining: " + formatTime ( seconds ) ;
29+
30+ // When we reach zero, stop counting and play the alarm
31+ if ( seconds === 0 ) {
32+ clearInterval ( intervalId ) ;
33+ playAlarm ( ) ;
34+ }
35+ } , 1000 ) ;
36+ }
237
338// DO NOT EDIT BELOW HERE
439
Original file line number Diff line number Diff line change 11<!DOCTYPE html>
22< html lang ="en ">
3- < head >
4- < meta charset ="utf-8 " />
5- < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
6- < link rel ="stylesheet " href ="style.css " />
7- < title > Title here</ title >
8- </ head >
9- < body >
10- < div class ="centre ">
11- < h1 id ="timeRemaining "> Time Remaining: 00:00</ h1 >
12- < label for ="alarmSet "> Set time to:</ label >
13- < input id ="alarmSet " type ="number " />
143
15- < button id ="set " type ="button "> Set Alarm</ button >
16- < button id ="stop " type ="button "> Stop Alarm</ button >
17- </ div >
18- < script src ="alarmclock.js "> </ script >
19- </ body >
20- </ html >
4+ < head >
5+ < meta charset ="utf-8 " />
6+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
7+ < link rel ="stylesheet " href ="style.css " />
8+ < title > Alarm Clock App</ title >
9+ </ head >
10+
11+ < body >
12+ < div class ="centre ">
13+ < h1 id ="timeRemaining "> Time Remaining: 00:00</ h1 >
14+ < label for ="alarmSet "> Set time to:</ label >
15+ < input id ="alarmSet " type ="number " min ="1 " step ="1 " />
16+
17+ < button id ="set " type ="button "> Set Alarm</ button >
18+ < button id ="stop " type ="button "> Stop Alarm</ button >
19+ </ div >
20+ < script src ="alarmclock.js "> </ script >
21+ </ body >
22+
23+ </ html >
You can’t perform that action at this time.
0 commit comments