Skip to content

Commit fcc0cfb

Browse files
committed
feat: implement core functionality and UI enhancements
1 parent 571150c commit fcc0cfb

File tree

3 files changed

+33
-44
lines changed

3 files changed

+33
-44
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
1-
// Use a constant for the one-second interval to make the code easier to read.
21
const ONE_SECOND_IN_MILLISECONDS = 1000;
32

4-
// Defines variables to track the state of the alarm clock.
5-
// Uses 'let' because these values will change as the timer runs.
63
let timeRemainingInSeconds = 0;
74
let alarmTimerIdentifier = null;
85

9-
// DOM Elements (will be fetched dynamically in functions to avoid load issues)
10-
116
/**
12-
* Formats a number of seconds into a standard "MM:SS" string.
13-
* This helps the user read the time easily (e.g., "01:30" instead of "90").
14-
*
15-
* @param {number} totalSeconds - The total number of seconds to format.
16-
* @returns {string} A string formatted as "MM:SS".
7+
* Formats seconds into "MM:SS".
178
*/
189
function formatTime(totalSeconds) {
19-
// Calculates the number of full minutes.
2010
const minutes = Math.floor(totalSeconds / 60);
21-
22-
// Calculates the remaining seconds after removing the minutes.
2311
const seconds = totalSeconds % 60;
2412

25-
// Adds a leading zero if the minutes are less than 10.
26-
// This ensures the clock always looks like "09:00" rather than "9:00".
2713
let formattedMinutes = `${minutes}`;
2814
if (minutes < 10) {
2915
formattedMinutes = `0${minutes}`;
3016
}
3117

32-
// Adds a leading zero if the seconds are less than 10.
3318
let formattedSeconds = `${seconds}`;
3419
if (seconds < 10) {
3520
formattedSeconds = `0${seconds}`;
@@ -40,71 +25,62 @@ function formatTime(totalSeconds) {
4025

4126
/**
4227
* Updates the display and checks if the alarm should sound.
43-
* This function runs every second.
4428
*/
4529
function updateTime() {
46-
// Finds the title element on the web page to update the text.
4730
const titleElement = document.getElementById("timeRemaining");
4831

49-
// Updates the text inside the title element with the new formatted time.
32+
timeRemainingInSeconds = timeRemainingInSeconds - 1;
5033
titleElement.innerText = formatTime(timeRemainingInSeconds);
5134

52-
// Checks if the countdown has finished.
5335
if (timeRemainingInSeconds === 0) {
54-
// Plays the alarm sound to alert the user.
5536
playAlarm();
56-
57-
// Changes the background colour to red (visual alert) by adding a CSS class.
5837
document.body.classList.add("flash");
59-
} else {
60-
// If time is not up, decreases the remaining time by exactly one second.
61-
timeRemainingInSeconds = timeRemainingInSeconds - 1;
38+
clearInterval(alarmTimerIdentifier);
6239
}
6340
}
6441

6542
/**
66-
* Starts the alarm countdown based on the time the user entered.
67-
* This function runs when the "Set Alarm" button is clicked.
43+
* Initialises the alarm countdown.
6844
*/
6945
function setAlarm() {
70-
// check if a timer is already active.
71-
// If so, stops the current timer to prevent two timers running at once.
7246
if (alarmTimerIdentifier) {
7347
clearInterval(alarmTimerIdentifier);
74-
75-
// Resets the background colour in case it was already flashing.
7648
document.body.classList.remove("flash");
7749
}
7850

79-
// Finds the input box where the user typed the number of seconds.
8051
const timeInput = document.getElementById("alarmSet");
52+
const startingTime = parseInt(timeInput.value, 10);
8153

82-
// Converts the text input into a whole number (integer).
83-
const startingTime = parseInt(timeInput.value);
84-
85-
// Validates the input: ensuring it is a number.
8654
if (isNaN(startingTime)) {
8755
return;
8856
}
8957

90-
// Validates the input: ensuring it is not negative.
9158
if (startingTime < 0) {
9259
return;
9360
}
9461

95-
// Updates the state variable with the new start time.
9662
timeRemainingInSeconds = startingTime;
9763

98-
// Updates the screen immediately so the user sees the start time without delay.
64+
// Initialise the audio context to enable autoplay.
65+
audio.play();
66+
audio.pause();
67+
9968
const titleElement = document.getElementById("timeRemaining");
10069
titleElement.innerText = formatTime(timeRemainingInSeconds);
10170

102-
// Starts a repeating timer that calls 'updateTime' every second (1000ms).
10371
alarmTimerIdentifier = setInterval(() => {
10472
updateTime();
10573
}, ONE_SECOND_IN_MILLISECONDS);
10674
}
10775

76+
// Add a listener to stop the flashing effect without modifying the protected pauseAlarm function below.
77+
const stopButton = document.getElementById("stop");
78+
if (stopButton) {
79+
stopButton.addEventListener("click", () => {
80+
document.body.classList.remove("flash");
81+
});
82+
}
83+
10884
// DO NOT EDIT BELOW HERE
10985

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

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 container">

Sprint-3/alarmclock/style.css

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,24 @@ body {
1010
}
1111

1212
.flash {
13-
background-color: #ffcccc !important; /* Pastel red for alarm */
13+
animation: flash-animation 1s infinite;
14+
}
15+
16+
/* Flashing background colour animation */
17+
@keyframes flash-animation {
18+
0% {
19+
background-color: #ffcccc;
20+
}
21+
50% {
22+
background-color: #ff0000;
23+
}
24+
100% {
25+
background-color: #ffcccc;
26+
}
1427
}
1528

1629
.container {
17-
background-color: #ffffff;
30+
background-color: #fff;
1831
border: 1px solid #ccc;
1932
border-radius: 8px;
2033
padding: 30px;

0 commit comments

Comments
 (0)