Skip to content

Commit 4882255

Browse files
committed
Implement function for alarm clock.
1 parent 4222cda commit 4882255

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,37 @@
1-
function setAlarm() {}
1+
let timer;
2+
function setAlarm() {
3+
const input = document.getElementById("alarmSet");
4+
let seconds = Number(input.value);
5+
6+
const timeRemaining = document.getElementById("timeRemaining");
7+
8+
clearInterval(timer);
9+
10+
function updateDisplay() {
11+
const minutes = Math.floor(seconds / 60);
12+
const remainingSeconds = seconds % 60;
13+
14+
const formattedTime =
15+
String(minutes).padStart(2, "0") +
16+
":" +
17+
String(remainingSeconds).padStart(2, "0");
18+
19+
timeRemaining.textContent = `Time Remaining: ${formattedTime}`;
20+
}
21+
22+
updateDisplay();
23+
24+
timer = setInterval(() => {
25+
if (seconds > 0) {
26+
seconds--;
27+
updateDisplay();
28+
} else {
29+
clearInterval(timer);
30+
playAlarm();
31+
document.body.style.backgroundColor = "red";
32+
}
33+
}, 1000);
34+
}
235

336
// DO NOT EDIT BELOW HERE
437

‎Sprint-3/alarmclock/index.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
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)