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+ const timeRemaining = document . querySelector ( "#timeRemaining" ) ;
2+
3+ let intervalId ;
4+
5+
6+
7+ function setAlarm ( ) {
8+ // I will use the function showTime twice, one for showing the time just after the user clicks the set button and another one for updating the time every second in countdown function
9+ function showTime ( ) {
10+ // getting te mins and seconds from the inputValue in appropriate format
11+ const minutes = Math . floor ( inputValue / 60 )
12+ . toString ( )
13+ . padStart ( 2 , "0" ) ;
14+ const seconds = ( inputValue % 60 ) . toString ( ) . padStart ( 2 , "0" ) ;
15+ //Gathering everything together and outputing remaining time
16+ timeRemaining . innerText = `Time Remaining: ${ minutes } :${ seconds } ` ;
17+ } ;
18+ // I added the next line in case if we want to reset the time for alarm
19+ clearInterval ( intervalId ) ;
20+ // getting the input value in number format
21+ let inputValue = Number ( document . querySelector ( "#alarmSet" ) . value ) ;
22+ showTime ( ) ;
23+ intervalId = setInterval ( countdown , 1000 ) ;
24+
25+ function countdown ( ) {
26+ if ( inputValue > 0 ) {
27+ //decrease the entered number by 1
28+ inputValue -- ;
29+ showTime ( ) ;
30+ }
31+ // When the inputValue is 0, play the alarm and clear interval so that the function stops initialising
32+ else {
33+ playAlarm ( ) ;
34+ clearInterval ( intervalId ) ;
35+ }
36+ }
37+ }
38+
39+
240
341// DO NOT EDIT BELOW HERE
442
@@ -20,6 +58,8 @@ function playAlarm() {
2058
2159function pauseAlarm ( ) {
2260 audio . pause ( ) ;
61+ //I know it's said to not edit this code, I wonder if it's ok to add this line below in order to pause the time before the time runs out.
62+ clearInterval ( intervalId ) ;
2363}
2464
2565window . onload = setup ;
Original file line number Diff line number Diff line change 44 < meta charset ="utf-8 " />
55 < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
66 < link rel ="stylesheet " href ="style.css " />
7- < title > Title here </ title >
7+ < title > Alarm clock app </ title >
88 </ head >
99 < body >
1010 < div class ="centre ">
Original file line number Diff line number Diff line change 1313 "bugs" : {
1414 "url" : " https://github.com/CodeYourFuture/CYF-Coursework-Template/issues"
1515 },
16- "homepage" : " https://github.com/CodeYourFuture/CYF-Coursework-Template#readme"
16+ "homepage" : " https://github.com/CodeYourFuture/CYF-Coursework-Template#readme" ,
17+ "devDependencies" : {
18+ "@testing-library/jest-dom" : " ^6.9.1" ,
19+ "jest-environment-jsdom" : " ^30.3.0"
20+ }
1721}
You can’t perform that action at this time.
0 commit comments