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
47 changes: 46 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
function setAlarm() {}
//function setAlarm() {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's remove this.


let countdownInterval;

function setAlarm() {
// 1. Get the input value and the display element
const alarmInput = document.getElementById("alarmSet").value;
const timeRemainingDisplay = document.getElementById("timeRemaining");

// Safety check: Ensure input is a number and greater than 0
if (!alarmInput || alarmInput <= 0) return;
Comment on lines +10 to +11
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 check technically allows non-numeric string inputs (e.g. "hello") to pass through. It’s probably fine since the input field appears restricted to numbers, but worth noting.


let timeRemaining = parseInt(alarmInput);

// 2. Clear any existing timers to prevent "speeding up" if clicked multiple times
clearInterval(countdownInterval);

// 3. Initial update so the user sees the time immediately
updateDisplay(timeRemaining, timeRemainingDisplay);

// 4. Start the countdown logic
countdownInterval = setInterval(() => {
timeRemaining -= 1;
updateDisplay(timeRemaining, timeRemainingDisplay);

// 5. Trigger the alarm and stop the counter when 0 is reached
if (timeRemaining <= 0) {
clearInterval(countdownInterval);
playAlarm();
}
}, 1000);
}
Comment on lines +5 to +32
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It seems one of the A/C is missing:

Given the alarm is set with a time of 00:10
When the timer reaches 00:00
Then the background colour should change
And the alarm sound should play

Could you look into it?


/**
* Helper function to format the time as 00:00
*/
function updateDisplay(totalSeconds, displayElement) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;

// Converts numbers to strings and pads with a leading zero if needed
const formattedMinutes = minutes.toString().padStart(2, "0");
const formattedSeconds = seconds.toString().padStart(2, "0");

displayElement.innerText = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
}
Comment on lines +34 to +46
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 logic looks good. 👍


// DO NOT EDIT BELOW HERE

Expand Down
37 changes: 20 additions & 17 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<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>
</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" />

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
</div>
<script src="alarmclock.js"></script>
</body>
</html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<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" />

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
</div>
<script src="alarmclock.js"></script>
</body>

</html>
Loading