Skip to content

Commit eb2df34

Browse files
cleaned and restructured the script.js file, replace the flashing effect with css animation, fixed missing functions and broken format logic
1 parent 8e5af85 commit eb2df34

3 files changed

Lines changed: 102 additions & 66 deletions

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 74 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,107 @@
1-
// This variable will store the interval so we can stop it later
2-
let flashInterval;
1+
// This is the countdown number that will go down every second
2+
let countdownTimer;
33

4-
// This function makes the background flash red and white
54
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
5+
document.body.classList.add("flash-background");
186
}
197

20-
// This function stops the flashing and resets the background
218
function stopFlashing() {
22-
clearInterval(flashInterval); // stop the flashing interval
23-
document.body.style.backgroundColor = "white"; // reset background
9+
document.body.classList.remove("flash-background");
2410
}
2511

2612
function setAlarm() {
27-
// get the value of the input
28-
const input = document.getElementById("alarmSet").value;
13+
const rawInput = document.getElementById("alarmSet").value.trim();
2914

30-
// If nothing was typed, exit the function
31-
if (!input) {
15+
// Reject empty input
16+
if (rawInput === "") {
17+
alert("Please enter a time.");
3218
return;
3319
}
3420

35-
let totalSeconds;
21+
// 2. Parse input → returns either a number of seconds or null
22+
const totalSeconds = parseInput(rawInput);
3623

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]);
24+
// 3. Reject invalid parsed values
25+
if (totalSeconds === null || totalSeconds <= 0) {
26+
alert("Please enter a valid positive time.");
27+
return;
28+
}
4329

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);
30+
// 4. Reject extremely large values
31+
if (totalSeconds > 36000) {
32+
alert("Please enter a time less than 10 hours.");
33+
return;
4934
}
5035

51-
// This is the countdown number that will go down every second
52-
let timeLeft = totalSeconds;
36+
stopFlashing();
37+
pauseAlarm();
38+
39+
// 5. If we reach here → input is valid
40+
startCountdown(totalSeconds);
41+
}
42+
43+
function parseInput(rawInput) {
44+
if (rawInput.includes(":")) {
45+
const [minsStr, secsStr] = rawInput.split(":");
46+
47+
const mins = Number(minsStr);
48+
const secs = Number(secsStr);
49+
50+
if (
51+
Number.isNaN(mins) ||
52+
Number.isNaN(secs) ||
53+
mins < 0 ||
54+
secs < 0 ||
55+
secs >= 60
56+
) {
57+
return null;
58+
}
5359

54-
// If a previous timer was running, stop it
55-
if (window.countdownTimer) {
56-
clearInterval(window.countdownTimer);
60+
return mins * 60 + secs;
5761
}
5862

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+
const secs = Number(rawInput);
64+
return Number.isNaN(secs) ? null : secs;
65+
}
6366

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+
function startCountdown(totalSeconds) {
68+
let timeLeft = totalSeconds;
6769

68-
return `${paddedMins}:${paddedSecs}`;
70+
if (countdownTimer) {
71+
clearInterval(countdownTimer);
6972
}
7073

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
74+
updateDisplay(timeLeft);
7875

79-
// Update the heading each second
80-
document.getElementById("timeRemaining").innerText =
81-
`Time Remaining: ${formatTime(timeLeft)}`;
76+
countdownTimer = setInterval(() => {
77+
timeLeft--;
78+
updateDisplay(timeLeft);
8279

83-
// When the timer reaches zero
8480
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
81+
clearInterval(countdownTimer);
82+
playAlarm();
83+
startFlashing();
9184
}
9285
}, 1000);
9386
}
9487

88+
// A simple helper to turn seconds into MM:SS format
89+
function formatTime(seconds) {
90+
const mins = Math.floor(seconds / 60);
91+
const secs = seconds % 60;
92+
93+
// Make sure both numbers always have two digits
94+
const paddedMins = String(mins).padStart(2, "0");
95+
const paddedSecs = String(secs).padStart(2, "0");
96+
97+
return `${paddedMins}:${paddedSecs}`;
98+
}
99+
100+
function updateDisplay(seconds) {
101+
document.getElementById("timeRemaining").innerText =
102+
`Time Remaining: ${formatTime(seconds)}`;
103+
}
104+
95105
// DO NOT EDIT BELOW HERE
96106

97107
var audio = new Audio("alarmsound.mp3");

‎Sprint-3/alarmclock/index.html‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="utf-8" />
56
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
67
<link rel="stylesheet" href="style.css" />
78
<title>Alarm Clock</title>
8-
</head>
9+
910
<body>
1011
<div class="centre">
1112
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
@@ -17,4 +18,5 @@ <h1 id="timeRemaining">Time Remaining: 00:00</h1>
1718
</div>
1819
<script src="alarmclock.js"></script>
1920
</body>
20-
</html>
21+
22+
</html>

‎Sprint-3/alarmclock/style.css‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,27 @@
1313
h1 {
1414
text-align: center;
1515
}
16+
17+
/* flashing effect for the alarm app */
18+
@keyframes colorRipple {
19+
0% {
20+
background-color: #ffffff;
21+
} /* White */
22+
25% {
23+
background-color: #ff6b6b;
24+
} /* Light Coral / Soft Red */
25+
50% {
26+
background-color: #feca57;
27+
} /* Mango / Warm Yellow-Orange */
28+
75% {
29+
background-color: #48dbfb;
30+
} /* Sky Blue / Light Aqua */
31+
100% {
32+
background-color: #ffffff;
33+
} /* White */
34+
}
35+
36+
/* apply the flashing effect to the body when the alarm is triggered */
37+
.flash-background {
38+
animation: colorRipple 0.8s infinite;
39+
}

0 commit comments

Comments
 (0)