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
29 changes: 28 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
function setAlarm() {}
function setAlarm() {
let seconds = parseInt(document.getElementById("alarmSet").value);

if (seconds <= 0) {
Comment on lines +2 to +4
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some invalid input can make the app behave abnormally.

Note: Why requiring min to be 0 here but set the "min" attribute in HTML to 1?

alert("The number of seconds must be more than 0 please");
return;
}
Comment on lines +4 to +7
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Indentation is off.

Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode,
as recommended in
https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md
?


If you have enabled "Format on save" but it is not working, it is likely that you haven't assign a formatter for JS file. This could happen if you have zero or multiple extensions that can format .js file.

If you have installed "Prettier" extension. To assign it as the formatter of JS code, you can try:

  1. Use "Format document" to format the JS file. Sometimes, VSCode will ask you to choose a formatter, and you can manually select "Prettier".
  2. Edit settings.json and set Prettier as the default formatter for JS.

See: https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

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.

Oh, my apologies, I inserted that and did not prettify it after doing so. Tbh I would normally have done that by hand quickly. Just forgot.

function updateDisplay() {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
document.getElementById("timeRemaining").textContent =
`Time Remaining: ${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`;
}

updateDisplay(); // update immediately on click

const countdown = setInterval(() => {
seconds--;
updateDisplay();

if (seconds <= 0) {
clearInterval(countdown);
playAlarm();
document.body.style.backgroundColor = "darkorange";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To better separate presentation logic from application logic, you can consider defining a CSS class, and use classList.toggle() to apply/remove the style. For example,

document.body.classList.toggle("alarm-activated", true);  // apply style
document.body.classList.toggle("alarm-activated", false); // remove style

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.

Ah - this would then also remove the need to do the above recommendation on resetting the background colour, did I see that correctly? (the bit that is currently not respecting the // DO NOT EDIT BELOW HERE marker)

Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Apr 12, 2026

Choose a reason for hiding this comment

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

You can still change background color, just specify a default background color for the body element in style.css, and then define a class, says .alarm-activated with a different background color.

By adding and removing the class from body, you can then switch between two background colors.

Note: What you want to do (besides stopping the alarm) when the user clicks the "Stop" button is up to you.

}
}, 1000);
}

// DO NOT EDIT BELOW HERE
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To respect the // DO NOT EDIT BELOW HERE marker, we could reset the background in a separate callback as:

window.addEventListener("load", function() {
  document.getElementById("stop").addEventListener("click", () => {
    // code to reset background
    ...
  });
});


Expand All @@ -20,6 +46,7 @@ function playAlarm() {

function pauseAlarm() {
audio.pause();
document.body.style.backgroundColor = "cornflowerblue";
}

window.onload = setup;
4 changes: 2 additions & 2 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<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">
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
<label for="alarmSet">Set time to:</label>
<input id="alarmSet" type="number" />
<input id="alarmSet" type="number" min="1" />

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
Expand Down
5 changes: 5 additions & 0 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@
h1 {
text-align: center;
}

body {
background-color: cornflowerblue;
font-family: "Trebuchet MS", Tahoma, Verdana, Helvetica, sans-serif;
}
Loading