Skip to content

Commit 1d5456c

Browse files
Implement alarm functionality with countdown and validation
1 parent 333486d commit 1d5456c

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
1-
function setAlarm() {}
1+
timerInterval = null;
2+
function setAlarm() {
3+
// 1. Get the input from the user
4+
const inputField = document.getElementById("alarmSet");
5+
let timeRemaining = parseInt(inputField.value);
6+
7+
// 2. Validate: If no number or 0 is entered, do nothing
8+
if (isNaN(timeRemaining) || timeRemaining <= 0) {
9+
return;
10+
}
11+
12+
// 3. Clear any existing timer; prevent multiple alarms running at once
13+
clearInterval(timerInterval);
14+
15+
// 4. Update the display immediately
16+
updateTimeDisplay(timeRemaining);
17+
18+
// 5. Start the countdown
19+
timerInterval = setInterval(() => {
20+
timeRemaining -= 1;
21+
22+
updateTimeDisplay(timeRemaining);
23+
24+
// 6. Check if timer hit zero
25+
if (timeRemaining <= 0) {
26+
clearInterval(timerInterval);
27+
playAlarm();
28+
document.body.style.backgroundColor = "red";
29+
}
30+
}, 1000);
31+
}
32+
33+
// mm:ss formatting
34+
35+
//00:00 format
236

337
// DO NOT EDIT BELOW HERE
438

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)