Skip to content

Commit e7e990d

Browse files
saved file
1 parent 71f2fa0 commit e7e990d

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,80 @@
1-
21
// Only define Audio if it doesn't exist (Node environment)
3-
if (typeof Audio === "undefined") { // In a Node define a mock Audio class to prevent errors when calling play() and pause()
4-
global.Audio = class { // Mock Audio class
5-
constructor(src) { // Takes a source for the audio file, but we won't load audio in this mock
6-
this.src = src; // stores the source (unused in the mock)
2+
if (typeof Audio === "undefined") {
3+
// In a Node define a mock Audio class to prevent errors when calling play() and pause()
4+
global.Audio = class {
5+
// Mock Audio class
6+
constructor(src) {
7+
// Takes a source for the audio file, but we won't load audio in this mock
8+
this.src = src; // stores the source (unused in the mock)
79
}
8-
play() {} // no-op in this mock
9-
pause() {} // no-op in this mock
10+
play() {} // no-op in this mock
11+
pause() {} // no-op in this mock
1012
};
1113
}
1214

13-
let countdownIntervalId; //stores timer ID so we can clear it when needed
15+
let countdownIntervalId; //stores timer ID so we can clear it when needed
1416

15-
const formatTime = (seconds) => { // takes a number of seconds and formats it into a string in the format "MM:SS"
16-
const minutes = Math.floor(seconds / 60); // calculates whole minutes
17-
const remainingSeconds = seconds % 60; // calculates remaining seconds
18-
return `${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; // formats as MM:SS
17+
const formatTime = (seconds) => {
18+
// takes a number of seconds and formats it into a string in the format "MM:SS"
19+
const minutes = Math.floor(seconds / 60); // calculates whole minutes
20+
const remainingSeconds = seconds % 60; // calculates remaining seconds
21+
return `${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; // formats as MM:SS
1922
};
2023

21-
function setAlarm() { // called "Set" button is clicked
22-
const alarmInput = document.getElementById("alarmSet"); // gets input for seconds
23-
const timerDisplay = document.getElementById("timeRemaining"); // gets element showing remaining time
24+
function setAlarm() {
25+
// called "Set" button is clicked
26+
const alarmInput = document.getElementById("alarmSet"); // gets input for seconds
27+
const timerDisplay = document.getElementById("timeRemaining"); // gets element showing remaining time
2428

25-
const seconds = parseInt(alarmInput.value, 10); // converts input value to integer
26-
if (isNaN(seconds) || seconds < 0) return; // if ignore invalid or negative input
29+
const seconds = parseInt(alarmInput.value, 10); // converts input value to integer
30+
if (isNaN(seconds) || seconds < 0) return; // if ignore invalid or negative input
2731

2832
let remainingSeconds = seconds; // initializes countdown
2933

30-
timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // shows initial time
34+
timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // shows initial time
3135

3236
if (countdownIntervalId) clearInterval(countdownIntervalId); // stops previous countdown to prevent multiple timers from running simultaneously
3337

34-
countdownIntervalId = setInterval(() => { // timer runs every second
35-
remainingSeconds--; // decrements remaining time by 1 second
38+
countdownIntervalId = setInterval(() => {
39+
// timer runs every second
40+
remainingSeconds--; // decrements remaining time by 1 second
3641

37-
if (remainingSeconds <= 0) { // when time runs out, clear the timer and play the alarm sound
38-
clearInterval(countdownIntervalId); // stops the countdown
42+
if (remainingSeconds <= 0) {
43+
// when time runs out, clear the timer and play the alarm sound
44+
clearInterval(countdownIntervalId); // stops the countdown
3945
timerDisplay.textContent = "Time Remaining: 00:00"; // shows zero time
40-
playAlarm(); // plays alarm sound
46+
playAlarm(); // plays alarm sound
4147
return; // prevents further execution
4248
}
4349
timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // updates countdown display
44-
}, 1000); // repeats every second
50+
}, 1000); // repeats every second
4551
}
4652

4753
// DO NOT EDIT BELOW HERE
4854

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

51-
function setup() { // This function sets up event listeners for the "Set" and "Stop" buttons when the page loads
52-
document.getElementById("set").addEventListener("click", () => { // adds a click event listener to the "Set" button that calls the setAlarm function when clicked
53-
setAlarm(); // calls the setAlarm function to start the timer when the "Set" button is clicked
57+
function setup() {
58+
// This function sets up event listeners for the "Set" and "Stop" buttons when the page loads
59+
document.getElementById("set").addEventListener("click", () => {
60+
// adds a click event listener to the "Set" button that calls the setAlarm function when clicked
61+
setAlarm(); // calls the setAlarm function to start the timer when the "Set" button is clicked
5462
});
5563

56-
document.getElementById("stop").addEventListener("click", () => { // adds a click event listener to the "Stop" button that calls the pauseAlarm function when clicked
57-
pauseAlarm(); // calls the pauseAlarm function to stop the alarm sound when the "Stop" button is clicked
64+
document.getElementById("stop").addEventListener("click", () => {
65+
// adds a click event listener to the "Stop" button that calls the pauseAlarm function when clicked
66+
pauseAlarm(); // calls the pauseAlarm function to stop the alarm sound when the "Stop" button is clicked
5867
});
5968
}
6069

61-
function playAlarm() { // This function plays the alarm sound when called
62-
audio.play(); // calls the play method on the audio object to start playing the alarm sound
70+
function playAlarm() {
71+
// This function plays the alarm sound when called
72+
audio.play(); // calls the play method on the audio object to start playing the alarm sound
6373
}
6474

65-
function pauseAlarm() { // This function pauses the alarm sound when called
66-
audio.pause(); // calls the pause method on the audio object to stop playing the alarm sound
75+
function pauseAlarm() {
76+
// This function pauses the alarm sound when called
77+
audio.pause(); // calls the pause method on the audio object to stop playing the alarm sound
6778
}
6879

69-
window.onload = setup; // sets the setup function to run when the window finishes loading, ensuring that event listeners are set up properly
80+
window.onload = setup; // sets the setup function to run when the window finishes loading, ensuring that event listeners are set up properly

0 commit comments

Comments
 (0)