-
-
Notifications
You must be signed in to change notification settings - Fork 195
West Midlands | ITP-Sept-25 | Georgina Rogers | Sprint 3 | Alarm Clock #905
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
dad10c9
264f3e1
2ae42af
9e52377
1879d0e
1e92123
d48ff25
aa73c2b
feb5c66
e57493f
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,97 @@ | ||
| function setAlarm() {} | ||
| let timer = null; | ||
| let totalSeconds = 0; | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
| function formatTime(seconds) { | ||
| const mins = Math.floor(seconds / 60); | ||
| const secs = seconds % 60; | ||
| return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; | ||
| } | ||
|
|
||
| var audio = new Audio("alarmsound.mp3"); | ||
| function parseInputTime(value) { | ||
| if (!/^\d{2}:\d{2}$/.test(value)) return null; | ||
| const [m, s] = value.split(":").map(Number); | ||
| if (s > 59) return null; | ||
| return m * 60 + s; | ||
| } | ||
|
|
||
| function setup() { | ||
| document.getElementById("set").addEventListener("click", () => { | ||
| setAlarm(); | ||
| }); | ||
| function updateDisplay() { | ||
| const heading = document.getElementById("timeRemaining"); | ||
| const input = document.getElementById("alarmSet"); | ||
|
|
||
| const formatted = formatTime(totalSeconds); | ||
|
|
||
| input.value = formatted; | ||
| heading.innerHTML = `Time Remaining:<br><br>${formatted}`; | ||
| } | ||
|
|
||
| document.getElementById("stop").addEventListener("click", () => { | ||
| pauseAlarm(); | ||
| }); | ||
| function incrementTime(amount) { | ||
| totalSeconds += amount; | ||
| if (totalSeconds < 0) totalSeconds = 0; | ||
| updateDisplay(); | ||
| } | ||
|
|
||
| function resetAlarmState() { | ||
| if (timer) clearInterval(timer); | ||
| timer = null; | ||
|
|
||
| audio.pause(); | ||
| audio.currentTime = 0; | ||
| } | ||
|
|
||
| function setAlarm() { | ||
| resetAlarmState(); | ||
| const input = document.getElementById("alarmSet"); | ||
| const parsed = parseInputTime(input.value); | ||
|
|
||
| if (parsed === null) { | ||
| alert("Use MM:SS format"); | ||
| return; | ||
| } | ||
|
|
||
| totalSeconds = parsed; | ||
| updateDisplay(); | ||
|
|
||
| if (totalSeconds === 0) { | ||
| playAlarm(); | ||
| return; | ||
| } //Alarm plays immediately if time is set to 00:00 | ||
|
|
||
| timer = setInterval(() => { | ||
| totalSeconds -= 1; | ||
| if (totalSeconds <= 0) { | ||
| totalSeconds = 0; | ||
| updateDisplay(); | ||
| clearInterval(timer); | ||
| timer = null; | ||
| playAlarm(); | ||
| return; | ||
| } | ||
| updateDisplay(); | ||
| }, 1000); | ||
| } | ||
|
|
||
| function stopTimer() { | ||
| if (timer) clearInterval(timer); | ||
| timer = null; | ||
| totalSeconds = 0; | ||
| updateDisplay(); | ||
| audio.pause(); | ||
| } | ||
|
|
||
| document.getElementById("up").addEventListener("click", () => incrementTime(5)); | ||
| document | ||
| .getElementById("down") | ||
| .addEventListener("click", () => incrementTime(-5)); | ||
| document.getElementById("set").addEventListener("click", setAlarm); | ||
| document.getElementById("stop").addEventListener("click", stopTimer); | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
| var audio = new Audio("alarmsound.mp3"); | ||
|
|
||
| function playAlarm() { | ||
| audio.play(); | ||
| } | ||
|
|
||
| function pauseAlarm() { | ||
| audio.pause(); | ||
| } | ||
|
|
||
| window.onload = setup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,17 +4,35 @@ | |
| <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" /> | ||
| <div class="container"> | ||
| <label for="alarmSet" class="time-label">Set time to:</label> | ||
|
|
||
| <button id="set" type="button">Set Alarm</button> | ||
| <button id="stop" type="button">Stop Alarm</button> | ||
| <input | ||
| id="alarmSet" | ||
| class="time-input" | ||
| type="text" | ||
| maxlength="5" | ||
| placeholder="00:00" | ||
| value="00:00" | ||
| /> | ||
|
|
||
| <div class="increment-buttons"> | ||
| <button id="up">▲ +5</button> | ||
| <button id="down">▼ -5</button> | ||
| </div> | ||
|
|
||
| <button id="set" class="set-button">SET</button> | ||
|
|
||
| <h1 id="timeRemaining" class="time-remaining"> | ||
| Time Remaining:<br />00:00 | ||
| </h1> | ||
|
|
||
| <button id="stop" class="stop-button">STOP</button> | ||
| </div> | ||
|
|
||
| <script src="alarmclock.js"></script> | ||
|
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. (Optional change)
This offers a couple of key advantages:
|
||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,63 @@ | ||
| .centre { | ||
| position: fixed; | ||
| top: 50%; | ||
| left: 50%; | ||
| -webkit-transform: translate(-50%, -50%); | ||
| transform: translate(-50%, -50%); | ||
| .container { | ||
| font-family: "Franklin Gothic Medium", "Arial Narrow", Arial, sans-serif; | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| justify-content: center; | ||
| height: 100vh; | ||
| gap: 15px; | ||
| } | ||
|
|
||
| #alarmSet { | ||
| margin: 20px; | ||
| .time-label { | ||
| font-size: 44px; | ||
| font-weight: bold; | ||
| } | ||
|
|
||
| h1 { | ||
| .time-input { | ||
| font-size: 64px; | ||
| text-align: center; | ||
| width: 200px; | ||
| padding: 15px; | ||
| border-radius: 10px; | ||
| border: 3px solid #0904b0; | ||
| } | ||
|
|
||
| .increment-buttons { | ||
| display: flex; | ||
| gap: 10px; | ||
| } | ||
|
|
||
| .increment-buttons button { | ||
| font-size: 20px; | ||
| padding: 10px 15px; | ||
| border-radius: 6px; | ||
| border: 2px solid #444; | ||
| } | ||
|
|
||
| .set-button { | ||
| font-size: 30px; | ||
| padding: 10px 20px; | ||
| border-radius: 8px; | ||
| border: 3px solid green; | ||
| } | ||
|
|
||
| .time-remaining { | ||
| font-size: 36px; | ||
| font-weight: 900; | ||
| text-align: center; | ||
| } | ||
|
|
||
| .stop-button { | ||
| font-size: 40px; | ||
| padding: 15px 30px; | ||
| border-radius: 10px; | ||
| border: 4px solid red; | ||
| } | ||
|
|
||
| .stop-large-button { | ||
| font-size: 40px; | ||
| padding: 15px 30px; | ||
| border-radius: 10px; | ||
| border: 4px solid red; | ||
| } | ||
|
|
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.
(Optional change)
You could also consider structuring the HTML like this:
This approach has several benefits:
Cleaner JavaScript: The JS only needs to update the dynamic value, not the static label.
Better semantics:
h1is meant for page headings, whiledivorspanis more appropriate for generic UI elements. Font size and visual hierarchy should be handled with CSS instead.More flexible layout: Avoid using
<br>for spacing or layout in application UIs -- CSS provides far greater control and maintainability.