-
-
Notifications
You must be signed in to change notification settings - Fork 281
Sheffield | 26-ITP-Jan | Seti Mussa | Sprint 3 | Alarm Clock #1178
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 all 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,25 +1,93 @@ | ||
| function setAlarm() {} | ||
| let timeRemaining = 0; | ||
| let timerId = null; | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
| const audio = new Audio("alarmsound.mp3"); | ||
|
|
||
| var audio = new Audio("alarmsound.mp3"); | ||
| // FORMAT mm:ss | ||
| function formatTime(seconds) { | ||
| const mins = Math.floor(seconds / 60); | ||
| const secs = seconds % 60; | ||
|
|
||
| function setup() { | ||
| document.getElementById("set").addEventListener("click", () => { | ||
| setAlarm(); | ||
| }); | ||
| return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; | ||
| } | ||
|
|
||
| document.getElementById("stop").addEventListener("click", () => { | ||
| pauseAlarm(); | ||
| }); | ||
| // UPDATE DISPLAY (MUST MATCH TEST EXACTLY) | ||
| function updateDisplay() { | ||
| const heading = document.getElementById("timeRemaining"); | ||
| heading.textContent = "Time Remaining: " + formatTime(timeRemaining); | ||
| } | ||
|
|
||
| // REQUIRED BY TESTS | ||
| function playAlarm() { | ||
| audio.loop = true; | ||
| audio.currentTime = 0; | ||
| audio.play(); | ||
| } | ||
|
|
||
| function pauseAlarm() { | ||
| // STOP ALARM | ||
| function stopAlarm() { | ||
| audio.pause(); | ||
| audio.currentTime = 0; | ||
| audio.loop = false; | ||
|
|
||
| clearInterval(timerId); | ||
| timerId = null; | ||
|
|
||
| document.body.style.backgroundColor = ""; | ||
| } | ||
|
|
||
| // TRIGGER ALARM | ||
| function triggerAlarm() { | ||
| document.body.style.backgroundColor = "red"; | ||
|
Contributor
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. To better separate presentation logic from application logic, you can consider defining a CSS class, and use
Author
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. I have replaced the inline background style with CSS class and used classList.togg() to separate presentation from logic. |
||
| playAlarm(); | ||
| } | ||
|
|
||
| // START TIMER | ||
| function startTimer() { | ||
| clearInterval(timerId); | ||
|
Contributor
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. Currently when starting a new countdown, the application does not always return to a clean initial state, Hint: a user may not click the "Stop" button first before starting a new count down.
Author
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. I changed the time logic so starting a new countdown resets the app to clean the state before running again. |
||
|
|
||
| timerId = setInterval(() => { | ||
| timeRemaining--; | ||
|
|
||
| updateDisplay(); | ||
|
|
||
| if (timeRemaining <= 0) { | ||
| clearInterval(timerId); | ||
| timerId = null; | ||
|
|
||
| timeRemaining = 0; | ||
| updateDisplay(); | ||
|
|
||
| triggerAlarm(); | ||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| // SET ALARM | ||
| function setAlarm() { | ||
| const input = document.getElementById("alarmSet").value; | ||
|
|
||
| // ensure clean number input | ||
| timeRemaining = parseInt(input, 10); | ||
|
|
||
| if (isNaN(timeRemaining) || timeRemaining < 0) { | ||
| timeRemaining = 0; | ||
| } | ||
|
|
||
| updateDisplay(); | ||
|
|
||
| if (timeRemaining > 0) { | ||
| startTimer(); | ||
| } | ||
| } | ||
|
|
||
| // SETUP | ||
| function setup() { | ||
| document.getElementById("set").addEventListener("click", setAlarm); | ||
| document.getElementById("stop").addEventListener("click", stopAlarm); | ||
|
|
||
| timeRemaining = 0; | ||
| updateDisplay(); | ||
| } | ||
|
|
||
| window.onload = setup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,11 @@ | ||
| .centre { | ||
| position: fixed; | ||
| top: 50%; | ||
| left: 50%; | ||
| -webkit-transform: translate(-50%, -50%); | ||
| transform: translate(-50%, -50%); | ||
| } | ||
|
|
||
| #alarmSet { | ||
| margin: 20px; | ||
| body { | ||
| background-color: blanchedalmond; | ||
| } | ||
|
|
||
| h1 { | ||
| text-align: center; | ||
| } | ||
|
|
||
| #alarmSet { | ||
| margin: 20px; | ||
| } |
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.
Why not include a parameter to
updateDisplay(0)to reset timer display)timeRemaining?
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.
Thanks for the feedback. I have updated updateDisplay(0) to take a parameter so it's reusable and doesn't rely on the global variable.