Skip to content

Commit 71f2fa0

Browse files
updated variable names and amended code and added comments
1 parent b2c81bf commit 71f2fa0

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
1-
function setAlarm() {}
1+
22
// Only define Audio if it doesn't exist (Node environment)
3-
if (typeof Audio === "undefined") {
4-
global.Audio = class {
5-
constructor(src) {
6-
this.src = src;
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)
77
}
8-
play() {}
9-
pause() {}
8+
play() {} // no-op in this mock
9+
pause() {} // no-op in this mock
1010
};
1111
}
1212

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

15-
const formatTime = (s) => {
16-
const mins = Math.floor(s / 60);
17-
const secs = s % 60;
18-
return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`;
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
1919
};
2020

21-
function setAlarm() {
22-
const input = document.getElementById("alarmSet");
23-
const timer = document.getElementById("timeRemaining");
24-
25-
const seconds = parseInt(input.value);
26-
if (isNaN(seconds) || seconds < 0) return;
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
2724

28-
let remaining = seconds;
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
2927

30-
// DO NOT EDIT BELOW HERE
31-
timer.textContent = `Time Remaining: ${formatTime(remaining)}`;
28+
let remainingSeconds = seconds; // initializes countdown
3229

33-
if (intervalId) clearInterval(intervalId);
30+
timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // shows initial time
3431

35-
intervalId = setInterval(() => {
36-
remaining--;
32+
if (countdownIntervalId) clearInterval(countdownIntervalId); // stops previous countdown to prevent multiple timers from running simultaneously
3733

38-
timer.textContent = `Time Remaining: ${formatTime(remaining)}`;
34+
countdownIntervalId = setInterval(() => { // timer runs every second
35+
remainingSeconds--; // decrements remaining time by 1 second
3936

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

4747
// DO NOT EDIT BELOW HERE
4848

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

51-
function setup() {
52-
document.getElementById("set").addEventListener("click", () => {
53-
setAlarm();
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
5454
});
5555

56-
document.getElementById("stop").addEventListener("click", () => {
57-
pauseAlarm();
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
5858
});
5959
}
6060

61-
function playAlarm() {
62-
audio.play();
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
6363
}
6464

65-
function pauseAlarm() {
66-
audio.pause();
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
6767
}
6868

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

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)