-
-
Notifications
You must be signed in to change notification settings - Fork 210
West Midlands | 25-ITP-Sep | Baba Yusuf | Sprint 3 | Alarm Clock #900
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
Changes from all commits
11e30f5
3cb29a0
3d4c407
b235bd1
a4fcc54
5aa6e97
80cc2eb
50a0653
9a76928
6948ee0
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,73 @@ | ||
| function setAlarm() {} | ||
| // Constants for time conversion | ||
| const SECONDS_PER_MINUTE = 60; | ||
| const MILLISECONDS_PER_SECOND = 1000; | ||
|
|
||
| let intervalId; | ||
|
|
||
| /** | ||
| * Formats time in seconds to MM:SS format | ||
| */ | ||
| function formatTimeDisplay(totalSeconds) { | ||
| const minutes = Math.floor(totalSeconds / SECONDS_PER_MINUTE); | ||
| const seconds = totalSeconds % SECONDS_PER_MINUTE; | ||
|
|
||
| return ( | ||
| "Time Remaining: " + | ||
| String(minutes).padStart(2, "0") + | ||
| ":" + | ||
| String(seconds).padStart(2, "0") | ||
| ); | ||
| } | ||
|
|
||
| function setAlarm() { | ||
| const input = document.getElementById("alarmSet"); | ||
| const heading = document.getElementById("timeRemaining"); | ||
| const errorMsg = document.getElementById("alarmError"); | ||
|
|
||
| // Reset alarm sound + flashing background before new countdown | ||
| pauseAlarm(); | ||
|
|
||
| let raw = input.value.trim(); | ||
|
|
||
| // Clear previous error | ||
| errorMsg.textContent = ""; | ||
|
|
||
| // STRONG VALIDATION: must be digits only | ||
| if (!/^\d+$/.test(raw)) { | ||
| heading.innerText = "Time Remaining: 00:00"; | ||
| errorMsg.textContent = | ||
| "Invalid input. Please enter a whole number of seconds (e.g., 10, 30, 120). Decimals and text are not allowed."; | ||
| return; | ||
| } | ||
|
|
||
| let totalSeconds = Number(raw); | ||
|
|
||
| // Prevent extremely large or zero values | ||
| if (totalSeconds === 0 || totalSeconds > 86400) { | ||
| heading.innerText = "Time Remaining: 00:00"; | ||
|
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. The same time-update code shows up in multiple places, which is a good signal that it could be factored into a utility function. |
||
| errorMsg.textContent = | ||
| "Please enter a value between 1 and 86,400 seconds. Examples: 10, 45, 300."; | ||
| return; | ||
| } | ||
|
|
||
| // Reset any existing countdown | ||
| clearInterval(intervalId); | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+53
to
+54
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. Could consider placing all the "resetting" code together (code on lines 54 and 28). |
||
|
|
||
| heading.innerText = formatTimeDisplay(totalSeconds); | ||
|
|
||
| intervalId = setInterval(() => { | ||
| totalSeconds--; | ||
|
|
||
| if (totalSeconds <= 0) { | ||
| heading.innerText = formatTimeDisplay(0); | ||
| clearInterval(intervalId); | ||
| playAlarm(); | ||
| return; | ||
| } | ||
|
|
||
| heading.innerText = formatTimeDisplay(totalSeconds); | ||
| }, MILLISECONDS_PER_SECOND); | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
@@ -12,14 +81,21 @@ function setup() { | |
| document.getElementById("stop").addEventListener("click", () => { | ||
| pauseAlarm(); | ||
| }); | ||
|
|
||
| // Allow Enter key to trigger alarm | ||
| document.getElementById("alarmSet").addEventListener("keyup", (e) => { | ||
| if (e.key === "Enter") setAlarm(); | ||
| }); | ||
| } | ||
|
|
||
| function playAlarm() { | ||
| audio.play(); | ||
| document.body.classList.add("alarm-active"); | ||
| } | ||
|
|
||
| function pauseAlarm() { | ||
| audio.pause(); | ||
| document.body.classList.remove("alarm-active"); | ||
| } | ||
|
|
||
| window.onload = setup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,17 +4,42 @@ | |
| <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" /> | ||
| <main class="centre"> | ||
| <h1 id="timeRemaining" aria-live="polite" role="timer"> | ||
| Time Remaining: 00:00 | ||
| </h1> | ||
|
Comment on lines
+12
to
+14
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. Could consider express code on lines 12-14 as: <div id="timeRemaining" aria-live="polite" role="timer">
Time Remaining: <span id="theTime">00:00</span>
</div>Note: Use AI to find out why if needed. |
||
|
|
||
| <label for="alarmSet">Set alarm time (seconds):</label> | ||
|
|
||
| <input | ||
| id="alarmSet" | ||
| type="number" | ||
| min="1" | ||
| step="1" | ||
| inputmode="numeric" | ||
| aria-describedby="alarmError" | ||
| /> | ||
|
|
||
| <!-- Visible and screen-reader friendly error message --> | ||
| <p id="alarmError" class="error-message" aria-live="assertive"></p> | ||
|
|
||
| <button | ||
| id="set" | ||
| type="button" | ||
| aria-label="Set alarm using the entered number of seconds" | ||
| > | ||
| Set Alarm | ||
| </button> | ||
|
|
||
| <button id="stop" type="button" aria-label="Stop the alarm sound"> | ||
| Stop Alarm | ||
| </button> | ||
| </main> | ||
|
|
||
| <button id="set" type="button">Set Alarm</button> | ||
| <button id="stop" type="button">Stop Alarm</button> | ||
| </div> | ||
| <script src="alarmclock.js"></script> | ||
| </body> | ||
| </html> | ||
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.
Could consider combining these two checks into: