-
-
Notifications
You must be signed in to change notification settings - Fork 147
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"); | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should try to complete the task without editing below this line There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
|
@@ -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; |
There was a problem hiding this comment.
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