Skip to content

Commit 2454086

Browse files
committed
removed redundant code, added code to stop alarm if runnng
1 parent da00a5e commit 2454086

File tree

1 file changed

+23
-44
lines changed

1 file changed

+23
-44
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,37 @@ let changeBgColor = false;
55
function setAlarm() {
66
inputTime = Number(document.querySelector("#alarmSet").value);
77

8-
if (Number.isInteger(inputTime) && inputTime >= 0) {
9-
if (inputTime === 10) {
10-
changeBgColor = true;
11-
} else if (inputTime === 0) {
12-
displayTime(inputTime);
13-
playAlarm();
14-
document.querySelector("#alarmSet").value = "";
15-
}
8+
if (!Number.isInteger(inputTime) || inputTime < 0) {
9+
return;
10+
}
11+
if (inputTime === 10) {
12+
changeBgColor = true;
13+
} else if (inputTime === 0) {
1614
displayTime(inputTime);
17-
if (timer) {
18-
clearInterval(timer);
19-
timer = null;
20-
}
21-
timer = setInterval(countDown, 1000);
15+
playAlarm();
16+
document.querySelector("#alarmSet").reset();
2217
}
18+
displayTime(inputTime);
19+
if (typeof audio !== "undefined" && audio) {
20+
audio.pause();
21+
try {
22+
audio.currentTime = 0;
23+
} catch (e) {}
24+
audio.loop = false; // disable looping if it was set
25+
}
26+
if (timer) {
27+
clearInterval(timer);
28+
}
29+
timer = setInterval(countDown, 1000);
2330
}
2431

2532
function displayTime(totalTime) {
2633
const seconds = totalTime % 60;
2734
const minutes = (totalTime - seconds) / 60;
28-
const timeLeft = document.querySelector("#timeRemaining");
29-
timeLeft.innerText = `Time Remaining: ${minutes
35+
const timeLeft = document.querySelector("#theTime");
36+
timeLeft.innerText = `${minutes.toString().padStart(2, 0)}:${seconds
3037
.toString()
31-
.padStart(2, 0)}:${seconds.toString().padStart(2, 0)}`;
38+
.padStart(2, 0)}`;
3239
}
3340

3441
function countDown() {
@@ -83,31 +90,3 @@ function pauseAlarm() {
8390
}
8491

8592
window.onload = setup;
86-
87-
/*
88-
Given the user has entered a number in the input field When the user clicks the “Set Alarm” button Then the “Time Remaining” title should update to show the entered number in mm:ss format
89-
take value from in input field and store it in a variable inputTime
90-
check inputTime value is within the accepted range?
91-
take variable inputTime and display it in time remaining section
92-
93-
94-
Given the alarm is set with a valid time When one second passes Then the “Time Remaining” title should decrement by 1 second
95-
the the value for time remaining
96-
check its a valid time (greater than 00:00)
97-
decrease value of time remaining by 1 sec for each sec that passes
98-
99-
Given the alarm is set with a time of 00:00 When the timer reaches 00:00 Then the alarm sound should play continuously
100-
check time remaining, if it is equal to 00:00 then sound alarm
101-
102-
103-
Given the alarm sound is currently playing When the user clicks the “Stop Alarm” button Then the alarm sound should stop playing
104-
check if stop alarm button has been pressed
105-
if true then check if alarm sound is playing then stop alarm sound if true
106-
107-
Given the alarm is set with a time of 00:10 When the timer reaches 00:00 Then the background color should change And the alarm sound should play
108-
if inputTime is 00:10 and timer reaches 0 the change background color
109-
play alarm sound
110-
111-
Given the user has not set an alarm When the page first loads Then the “Time Remaining” title should show 00:00 And no alarm sound should play
112-
when page loads time remaining should 0 and no alarm sound should play
113-
*/

0 commit comments

Comments
 (0)