Skip to content
58 changes: 57 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,60 @@
function setAlarm() {}
let interval;
let timeRemaining = 0;
let flashInterval;

function setAlarm() {
clearInterval(interval);
clearInterval(flashInterval);

const input = document.getElementById("alarmSet");
const display = document.getElementById("timeRemaining");

timeRemaining = parseInt(input.value);

if (isNaN(timeRemaining) || timeRemaining <= 0) return;
updateDisplay();

interval = setInterval(() => {
if (timeRemaining <= 0) {
clearInterval(interval);
timeRemaining = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is this needed?

updateDisplay();
playAlarm();
// Start flashing background
flashInterval = setInterval(() => {
document.body.style.backgroundColor =
document.body.style.backgroundColor === "green" ? "white" : "green";
}, 500);
const stopButton = document.getElementById("stop");
stopButton.addEventListener(
"click",
() => {
clearInterval(flashInterval);
document.body.style.backgroundColor = "white";
},
{ once: true }
);

return;
}

// Decrement only if above 0

timeRemaining--;

updateDisplay();
}, 1000);

function updateDisplay() {
const minutes = Math.floor(timeRemaining / 60);
const seconds = timeRemaining % 60;

const mm = String(minutes).padStart(2, "0");
const ss = String(seconds).padStart(2, "0");

display.textContent = `Time Remaining: ${mm}:${ss}`;
}
Comment on lines +48 to +56
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. 👍

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This logic looks good. 👍

Thank you very much.

}

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