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
39 changes: 38 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
function setAlarm() {}
// store interval ID so we can stop it later
let alarmInterval = null;
const heading = document.getElementById("timeRemaining");
const input = document.getElementById("alarmSet");
const button = document.getElementById("set");

function setAlarm() {
// get the input value (in seconds)
let secondsRemaining = parseInt(input.value);

// function to format seconds as MM:SS
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
// padStart(2, '0') adds leading zero if needed: 5 becomes '05'
return `Time Remaining: ${String(minutes).padStart(2, "0")}:${String(secs).padStart(2, "0")}`;
}

// update heading immediately with initial value
heading.textContent = formatTime(secondsRemaining);

// clear any existing alarm before starting new one
if (alarmInterval) {
clearInterval(alarmInterval);
}

// start countdown: every 1000 milliseconds = 1 sec
alarmInterval = setInterval(() => {
secondsRemaining--;
heading.textContent = formatTime(secondsRemaining);

// when timer reaches zero - play alarm and stop interval
if (secondsRemaining === 0) {
clearInterval(alarmInterval);
playAlarm();
}
}, 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