Skip to content

London | May-2025 | Mohamed Ibrahim | Module-Data-Groups | Alarm Clock #563

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 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
45 changes: 44 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
function setAlarm() {}
let countdown = null;
let remainingSeconds = 0;

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

const seconds = parseInt(input.value, 10);

if (isNaN(seconds) || seconds <= 0) {
alert("Enter a number greater than 0");

Choose a reason for hiding this comment

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

nice job thinking about how to handle invalid input

return;
}

// Stop any previous countdown
if (countdown !== null) {
clearInterval(countdown);
}

remainingSeconds = seconds;
updateTimeDisplay();

countdown = setInterval(() => {
remainingSeconds--;

updateTimeDisplay();

if (remainingSeconds <= 0) {
clearInterval(countdown);
countdown = null;
playAlarm();
document.body.style.backgroundColor = "#f99"; // change background
}
}, 1000);
}

function updateTimeDisplay() {
const minutes = String(Math.floor(remainingSeconds / 60)).padStart(2, "0");
const seconds = String(remainingSeconds % 60).padStart(2, "0");
document.getElementById("timeRemaining").textContent = `Time Remaining: ${minutes}:${seconds}`;
}

// DO NOT EDIT BELOW HERE

Choose a reason for hiding this comment

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

We should try to complete the task without editing below this line

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the feedback! I moved them above the line as suggested, so nothing below was changed.


Expand All @@ -11,15 +51,18 @@ function setup() {

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
document.body.style.backgroundColor = ""; // reset background
});
}

function playAlarm() {
audio.loop = true;
audio.play();
}

function pauseAlarm() {
audio.pause();
audio.currentTime = 0;
}

window.onload = setup;
11 changes: 7 additions & 4 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
<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">
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
<label for="alarmSet">Set time to:</label>
<input id="alarmSet" type="number" />
<h1>Time Remaining: <span id="timeRemaining">00:00</span></h1>

<label for="alarmSet">Set time (seconds):</label>
<input id="alarmSet" type="number" min="1" />

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
</div>

<script src="alarmclock.js"></script>
</body>
</html>