Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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() {}
let intervalId = null;

function setAlarm() {
const input = document.getElementById("alarmSet");
const heading = document.getElementById("timeRemaining");

const value = Number(input.value);
if (!Number.isInteger(value) || value <= 0) {
alert("Please enter a positive whole number of seconds.");
return;
}

resetAlarm();

let remainingSeconds = value;
updateDisplay(remainingSeconds);

intervalId = setInterval(() => {
remainingSeconds -= 1;

if (remainingSeconds > 0) {
updateDisplay(remainingSeconds);
} else {
updateDisplay(0);
clearInterval(intervalId);
playAlarm();
setFlashingBackground(true);
}
}, 1000);
}

function resetAlarm() {
clearInterval(intervalId);
intervalId = null;
updateDisplay(0);
setFlashingBackground(false);
}
Comment on lines +32 to +37

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.

A user may not click the "Stop" button first before starting a new count down. What else should also be reset?

Hint: When the "Stop" button is clicked, what action is performed?


function updateDisplay(seconds) {
const heading = document.getElementById("timeRemaining");
heading.innerText = `Time Remaining: ${formatTime(seconds)}`;
}

function formatTime(totalSeconds) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
const mm = String(minutes).padStart(2, "0");
const ss = String(seconds).padStart(2, "0");
return `${mm}:${ss}`;
}

function setFlashingBackground(isFlashing) {
if (isFlashing) {
document.body.classList.add("flash");
} else {
document.body.classList.remove("flash");
}
}

// 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
10 changes: 10 additions & 0 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@
h1 {
text-align: center;
}

.flash {
animation: flash-bg 0.5s infinite alternate;
}

@keyframes flash-bg {
from { background-color: white; }
to { background-color: red; }
}

Loading