Skip to content

Commit fd618b8

Browse files
implemented the alarm clock function based on the specified instructions and added flashing effect
1 parent 4a93f1c commit fd618b8

2 files changed

Lines changed: 114 additions & 18 deletions

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,96 @@
1-
function setAlarm() {}
1+
// This variable will store the interval so we can stop it later
2+
let flashInterval;
3+
4+
// This function makes the background flash red and white
5+
function startFlashing() {
6+
let isRed = false; // keeps track of which color we should show
7+
8+
flashInterval = setInterval(function () {
9+
if (isRed) {
10+
document.body.style.backgroundColor = "white";
11+
} else {
12+
document.body.style.backgroundColor = "red";
13+
}
14+
15+
// Switch the color for next time
16+
isRed = !isRed;
17+
}, 500); // run every half second
18+
}
19+
20+
// This function stops the flashing and resets the background
21+
function stopFlashing() {
22+
clearInterval(flashInterval); // stop the flashing interval
23+
document.body.style.backgroundColor = "white"; // reset background
24+
}
25+
26+
function setAlarm() {
27+
// get the value of the input
28+
const input = document.getElementById("alarmSet").value;
29+
30+
// If nothing was typed, exit the function
31+
if (!input) {
32+
return;
33+
}
34+
35+
let totalSeconds;
36+
37+
// If the user typed something like "2:21"
38+
if (input.includes(":")) {
39+
// Split into minutes and seconds
40+
const parts = input.split(":");
41+
const minutes = Number(parts[0]);
42+
const seconds = Number(parts[1]);
43+
44+
// Convert everything into total seconds
45+
totalSeconds = minutes * 60 + seconds;
46+
} else {
47+
// Otherwise treat the input as normal seconds
48+
totalSeconds = Number(input);
49+
}
50+
51+
// This is the countdown number that will go down every second
52+
let timeLeft = totalSeconds;
53+
54+
// If a previous timer was running, stop it
55+
if (window.countdownTimer) {
56+
clearInterval(window.countdownTimer);
57+
}
58+
59+
// A simple helper to turn seconds into MM:SS format
60+
function formatTime(seconds) {
61+
const mins = Math.floor(seconds / 60);
62+
const secs = seconds % 60;
63+
64+
// Make sure both numbers always have two digits
65+
const paddedMins = String(mins).padStart(2, "0");
66+
const paddedSecs = String(secs).padStart(2, "0");
67+
68+
return `${paddedMins}:${paddedSecs}`;
69+
}
70+
71+
// Show the starting time immediately
72+
document.getElementById("timeRemaining").innerText =
73+
`Time Remaining: ${formatTime(timeLeft)}`;
74+
75+
// Start the countdown — runs every 1000ms (1 second)
76+
window.countdownTimer = setInterval(() => {
77+
timeLeft--; // reduce by 1 second
78+
79+
// Update the heading each second
80+
document.getElementById("timeRemaining").innerText =
81+
`Time Remaining: ${formatTime(timeLeft)}`;
82+
83+
// When the timer reaches zero
84+
if (timeLeft <= 0) {
85+
clearInterval(window.countdownTimer); // stop the countdown
86+
clearInterval(flashInterval); // stop the flashing if it was running
87+
document.getElementById("timeRemaining").innerText =
88+
`Time Remaining: 00:00`;
89+
playAlarm(); // play the alarm sound
90+
startFlashing(); // start flashing the background
91+
}
92+
}, 1000);
93+
}
294

395
// DO NOT EDIT BELOW HERE
496

@@ -11,6 +103,7 @@ function setup() {
11103

12104
document.getElementById("stop").addEventListener("click", () => {
13105
pauseAlarm();
106+
stopFlashing(); // stop flashing if running
14107
});
15108
}
16109

‎Sprint-3/alarmclock/index.html‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta charset="utf-8" />
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6-
<link rel="stylesheet" href="style.css" />
7-
<title>Title here</title>
8-
</head>
9-
<body>
10-
<div class="centre">
11-
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
12-
<label for="alarmSet">Set time to:</label>
13-
<input id="alarmSet" type="number" />
143

15-
<button id="set" type="button">Set Alarm</button>
16-
<button id="stop" type="button">Stop Alarm</button>
17-
</div>
18-
<script src="alarmclock.js"></script>
19-
</body>
20-
</html>
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<link rel="stylesheet" href="style.css" />
8+
<title>Alarm Clock</title>
9+
</head>
10+
11+
<body>
12+
<div class="centre">
13+
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
14+
<label for="alarmSet">Set time to:</label>
15+
<input id="alarmSet" type="number" />
16+
17+
<button id="set" type="button">Set Alarm</button>
18+
<button id="stop" type="button">Stop Alarm</button>
19+
</div>
20+
<script src="alarmclock.js"></script>
21+
</body>
22+
23+
</html>

0 commit comments

Comments
 (0)