Skip to content

Commit 8196fd6

Browse files
committed
made alarm clock countdown from user input number and alarm sound when it reaches 0
1 parent e17bf30 commit 8196fd6

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,44 @@
1-
function setAlarm() {}
1+
let inputTime = 0;
2+
let timer = null;
3+
4+
function setAlarm() {
5+
inputTime = Number(document.querySelector("#alarmSet").value);
6+
7+
if (Number.isInteger(inputTime) && !isNaN(inputTime) && inputTime >= 0) {
8+
if (inputTime === 10) {
9+
}
10+
displayTime(inputTime);
11+
if (timer) clearInterval(timer);
12+
timer = setInterval(countDown, 1000);
13+
}
14+
}
15+
16+
function displayTime(totalTime) {
17+
const seconds = totalTime % 60;
18+
const minutes = (totalTime - seconds) / 60;
19+
const timeLeft = document.querySelector("#timeRemaining");
20+
timeLeft.innerText = `Time Remaining: ${minutes
21+
.toString()
22+
.padStart(2, 0)}:${seconds.toString().padStart(2, 0)}`;
23+
}
24+
25+
function countDown() {
26+
inputTime--;
27+
if (inputTime >= 0) {
28+
displayTime(inputTime);
29+
}
30+
31+
if (inputTime === 0) {
32+
playAlarm();
33+
clearInterval(timer);
34+
timer = null;
35+
}
36+
}
37+
/*
38+
the the value for time remaining
39+
check its a valid time (greater than 00:00)
40+
decrease value of time remaining by 1 sec for each sec that passes
41+
*/
242

343
// DO NOT EDIT BELOW HERE
444

@@ -23,3 +63,31 @@ function pauseAlarm() {
2363
}
2464

2565
window.onload = setup;
66+
67+
/*
68+
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
69+
take value from in input field and store it in a variable inputTime
70+
check inputTime value is within the accepted range?
71+
take variable inputTime and display it in time remaining section
72+
73+
74+
Given the alarm is set with a valid time When one second passes Then the “Time Remaining” title should decrement by 1 second
75+
the the value for time remaining
76+
check its a valid time (greater than 00:00)
77+
decrease value of time remaining by 1 sec for each sec that passes
78+
79+
Given the alarm is set with a time of 00:00 When the timer reaches 00:00 Then the alarm sound should play continuously
80+
check time remaining, if it is equal to 00:00 then sound alarm
81+
82+
83+
Given the alarm sound is currently playing When the user clicks the “Stop Alarm” button Then the alarm sound should stop playing
84+
check if stop alarm button has been pressed
85+
if true then check if alarm sound is playing then stop alarm sound if true
86+
87+
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
88+
if inputTime is 00:10 and timer reaches 0 the change background color
89+
play alarm sound
90+
91+
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
92+
when page loads time remaining should 0 and no alarm sound should play
93+
*/

Sprint-3/alarmclock/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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">

0 commit comments

Comments
 (0)