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 timer = null ;
2+ let secondsRemaining = 0 ;
23
34function formatTime ( totalSeconds ) {
45 const minutes = Math . floor ( totalSeconds / 60 ) ;
@@ -14,22 +15,49 @@ function updateDisplay(seconds) {
1415function setAlarm ( ) {
1516 if ( timer ) {
1617 clearInterval ( timer ) ;
18+ timer = null ;
1719 }
1820
19- let seconds = Number ( document . getElementById ( "alarmSet" ) . value ) ;
20- updateDisplay ( seconds ) ;
21+ const inputValue = document . getElementById ( "alarmSet" ) . value ;
22+ const seconds = Number ( inputValue ) ;
23+
24+ if (
25+ inputValue === "" ||
26+ Number . isNaN ( seconds ) ||
27+ ! Number . isInteger ( seconds ) ||
28+ seconds <= 0
29+ ) {
30+ document . getElementById ( "timeRemaining" ) . textContent =
31+ "Please enter a whole number greater than 0." ;
32+ return ;
33+ }
34+
35+ secondsRemaining = seconds ;
36+ updateDisplay ( secondsRemaining ) ;
2137
2238 timer = setInterval ( ( ) => {
23- seconds -- ;
24- updateDisplay ( seconds ) ;
39+ secondsRemaining -- ;
40+ updateDisplay ( secondsRemaining ) ;
2541
26- if ( seconds <= 0 ) {
42+ if ( secondsRemaining <= 0 ) {
2743 clearInterval ( timer ) ;
44+ timer = null ;
2845 playAlarm ( ) ;
2946 }
3047 } , 1000 ) ;
3148}
3249
50+ function stopTimer ( ) {
51+ if ( timer ) {
52+ clearInterval ( timer ) ;
53+ timer = null ;
54+ }
55+ }
56+
57+ document . addEventListener ( "DOMContentLoaded" , ( ) => {
58+ document . getElementById ( "stop" ) . addEventListener ( "click" , stopTimer ) ;
59+ } ) ;
60+
3361// DO NOT EDIT BELOW HERE
3462var audio = new Audio ( "alarmsound.mp3" ) ;
3563
Original file line number Diff line number Diff line change 22There are some Tests in this file that will help you work out if your code is working.
33*/
44
5+
56const path = require ( "path" ) ;
67const { JSDOM } = require ( "jsdom" ) ;
78
@@ -15,6 +16,8 @@ beforeEach(async () => {
1516
1617 jest . useFakeTimers ( ) ;
1718
19+
20+
1821 // do this so students can use element.innerText which jsdom does not implement
1922 Object . defineProperty ( page . window . HTMLElement . prototype , "innerText" , {
2023 get ( ) {
You can’t perform that action at this time.
0 commit comments