Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely optional and can be personal preference but we might have over-commented some of the lines, some comments are genuinely useful and are great to document code but others might be consider self explanatory.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right about style- I have put most of the comments in as a reminder to myself when I look at it again- not very neat and collaborative, but useful for me while I'm learning and possibly reusing in future modules. If there's a better or more standard way of doing that, I'm happy to comply? Thanks

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @DamianDL , typically when we raise PRs, we would only keep comments for things that really aren't obvious from the code alone, like context or complicated implementation that would require that extra info (ideally the code self-documents what we are trying to do with proper variable and function names), but for learning purposes I don't feel too strongly against keeping them.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Original file line number Diff line number Diff line change
@@ -1,4 +1,70 @@
function setAlarm() {}
let countdownId = null;
const originalBackgroundColor = document.body.style.backgroundColor || "white"; // Store the original background color
let timeRemainingHeading = null;

function getTimeRemainingHeading() {
if (timeRemainingHeading === null) {
timeRemainingHeading = document.getElementById("timeRemaining");
}

return timeRemainingHeading;
}

function formatTime(totalSeconds) {
// Convert total seconds to minutes and seconds
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;

return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
}

function updateHeading(totalSeconds) {
// Update the heading with the formatted time remaining
const heading = getTimeRemainingHeading(); // Query once, then reuse the cached DOM node on each tick
heading.innerText = `Time Remaining: ${formatTime(totalSeconds)}`; // Set the text of the heading to show the time remaining in the format "Time Remaining: MM:SS"
}

function setAlarm() {
const input = document.getElementById("alarmSet"); // Get the input element using its DOM ID
let remainingSeconds = Number(input.value); // Convert the input value to a number representing the total seconds for the countdown

// Guard against edge cases: empty input, non-numeric, negative values, and 0
if (!Number.isFinite(remainingSeconds) || remainingSeconds <= 0) {
// Check if the input is a valid number and strictly greater than 0
alert("Please enter a positive number greater than 0");
return; // Exit early without starting the countdown
}

if (countdownId !== null) {
// If there is an existing countdown, clear it before starting a new one

clearInterval(countdownId);
}


// Reset background and stop alarm sound when setting a new alarm
document.body.style.backgroundColor = originalBackgroundColor;

updateHeading(remainingSeconds); // Update the heading to show the initial time remaining before starting the countdown

countdownId = setInterval(() => {
// Start a new interval that will execute the provided function every 1000 milliseconds (1 second)
remainingSeconds -= 1;

if (remainingSeconds <= 0) {
// If the remaining seconds reach 0 or below, stop the countdown and play the alarm
updateHeading(0);
clearInterval(countdownId);
countdownId = null;
playAlarm();
document.body.style.backgroundColor = "red"; // change background colour
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an awesome touch, minor point is that when the background becomes red, then a new timer could reset it back to the original background (until it's done counting and it goes back to red). How can we implement that?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reset screen to white when timer set.


return; // Exit the function to prevent further execution
}

updateHeading(remainingSeconds);
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
2 changes: 1 addition & 1 deletion Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Title here</title>
<title>Alarm Clock App</title>
</head>
<body>
<div class="centre">
Expand Down