Skip to content

WESTMIDLANDS | ITP-MAY2025 | Peter Lui | Sprint3 Alarm Clock #738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
52 changes: 51 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,54 @@
function setAlarm() {}
function setAlarm() {
// Get the input value
let time = parseInt(document.getElementById("alarmSet").value) || 0;

// Check for negative numbers
if (time < 0) {
document.getElementById("timeRemaining").textContent =
"Time Remaining: 00:00";
return; // Exit the function if the input is negative
}

// Clear any existing interval
if (window.alarmInterval) {
clearInterval(window.alarmInterval);
}

// Update the display immediately
document.getElementById("timeRemaining").textContent =
"Time Remaining: " + formatTime(time);

// Start countdown
window.alarmInterval = setInterval(function () {
// Decrement time first
time--;

// Check if time is zero or negative
if (time <= 0) {
clearInterval(window.alarmInterval);
document.getElementById("timeRemaining").textContent =
"Time Remaining: 00:00";
playAlarm();
return;
}

// Update display after checking
document.getElementById("timeRemaining").textContent =
"Time Remaining: " + formatTime(time);
}, 1000);
}

// Helper function to format time as MM:SS
function formatTime(seconds) {
var minutes = Math.floor(seconds / 60);
var remainingSeconds = seconds % 60;
return pad(minutes) + ":" + pad(remainingSeconds);
}

// Helper function to pad single digits with leading zero
function pad(num) {
return num < 10 ? "0" + num : num;
}

// 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