Skip to content
Closed
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
59 changes: 58 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,61 @@
function setAlarm() {}
function setAlarm() {
const input = document.getElementById("alarmSet");
const heading = document.getElementById("timeRemaining");

// Read and trim input
const raw = input.value.trim();

// If input is empty, do nothing (don't start the alarm)
if (raw === "") {
heading.innerText = `Time Remaining: 00:00`;
return;
}

// Parse total seconds from input
let totalSeconds = parseInt(raw, 10);
if (Number.isNaN(totalSeconds) || totalSeconds <= 0) {
// invalid or non-positive input -> do nothing
heading.innerText = `Time Remaining: 00:00`;
return;
}

// Format seconds into "MM:SS"
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
return `Time Remaining: ${String(minutes).padStart(2, "0")}:${String(
secs
).padStart(2, "0")}`;
}

// Immediately set the heading to the initial value
heading.innerText = formatTime(totalSeconds);

// If a previous interval exists, clear it so we only have one running
if (window._alarmInterval) {
clearInterval(window._alarmInterval);
window._alarmInterval = null;
}

// Start counting down every 1000ms
window._alarmInterval = setInterval(() => {
totalSeconds -= 1;

if (totalSeconds <= 0) {
// update to 00:00, stop interval and play alarm
heading.innerText = formatTime(0);
clearInterval(window._alarmInterval);
window._alarmInterval = null;
if (typeof playAlarm === "function") {
playAlarm();
}
} else {
// update heading with remaining time
heading.innerText = formatTime(totalSeconds);
}
}, 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
4 changes: 2 additions & 2 deletions Sprint-3/reading-list/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<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>
<link rel="stylesheet" href="style.css">
<title></title>
</head>
<body>
<div id="content">
Expand Down