-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-May | Yonatan Teklemariam | Sprint 3 | Alarm Clock #1403
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
28a696f
cff19af
9e95480
ebfc715
8e5af85
eb2df34
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,106 @@ | ||
| function setAlarm() {} | ||
| // This is the countdown number that will go down every second | ||
| let countdownTimer; | ||
|
|
||
| function startFlashing() { | ||
| document.body.classList.add("flash-background"); | ||
| } | ||
|
|
||
| function stopFlashing() { | ||
| document.body.classList.remove("flash-background"); | ||
| } | ||
|
|
||
| function setAlarm() { | ||
| const rawInput = document.getElementById("alarmSet").value.trim(); | ||
|
|
||
| // Reject empty input | ||
| if (rawInput === "") { | ||
| alert("Please enter a time."); | ||
| return; | ||
| } | ||
|
|
||
| // 2. Parse input → returns either a number of seconds or null | ||
| const totalSeconds = parseInput(rawInput); | ||
|
|
||
| // 3. Reject invalid parsed values | ||
| if (totalSeconds === null || totalSeconds <= 0) { | ||
| alert("Please enter a valid positive time."); | ||
| return; | ||
| } | ||
|
|
||
| // 4. Reject extremely large values | ||
| if (totalSeconds > 36000) { | ||
| alert("Please enter a time less than 10 hours."); | ||
| return; | ||
| } | ||
|
|
||
| stopFlashing(); | ||
| pauseAlarm(); | ||
|
|
||
| // 5. If we reach here → input is valid | ||
| startCountdown(totalSeconds); | ||
| } | ||
|
|
||
| function parseInput(rawInput) { | ||
| if (rawInput.includes(":")) { | ||
| const [minsStr, secsStr] = rawInput.split(":"); | ||
|
|
||
| const mins = Number(minsStr); | ||
| const secs = Number(secsStr); | ||
|
|
||
| if ( | ||
| Number.isNaN(mins) || | ||
| Number.isNaN(secs) || | ||
|
Comment on lines
+47
to
+52
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. What type of numbers should they be? What if the user enter |
||
| mins < 0 || | ||
| secs < 0 || | ||
| secs >= 60 | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| return mins * 60 + secs; | ||
| } | ||
|
|
||
| const secs = Number(rawInput); | ||
| return Number.isNaN(secs) ? null : secs; | ||
| } | ||
|
|
||
| function startCountdown(totalSeconds) { | ||
| let timeLeft = totalSeconds; | ||
|
|
||
| if (countdownTimer) { | ||
| clearInterval(countdownTimer); | ||
| } | ||
|
|
||
| updateDisplay(timeLeft); | ||
|
|
||
| countdownTimer = setInterval(() => { | ||
| timeLeft--; | ||
| updateDisplay(timeLeft); | ||
|
|
||
| if (timeLeft <= 0) { | ||
| clearInterval(countdownTimer); | ||
| playAlarm(); | ||
| startFlashing(); | ||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| // A simple helper to turn seconds into MM:SS format | ||
| function formatTime(seconds) { | ||
| const mins = Math.floor(seconds / 60); | ||
| const secs = seconds % 60; | ||
|
|
||
| // Make sure both numbers always have two digits | ||
| const paddedMins = String(mins).padStart(2, "0"); | ||
| const paddedSecs = String(secs).padStart(2, "0"); | ||
|
|
||
| return `${paddedMins}:${paddedSecs}`; | ||
| } | ||
|
|
||
| function updateDisplay(seconds) { | ||
| document.getElementById("timeRemaining").innerText = | ||
| `Time Remaining: ${formatTime(seconds)}`; | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
@@ -11,6 +113,7 @@ function setup() { | |
|
|
||
| document.getElementById("stop").addEventListener("click", () => { | ||
| pauseAlarm(); | ||
| stopFlashing(); // stop flashing if running | ||
|
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. An alternative would be to add another event listener that performs only `stop flashing". |
||
| }); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,22 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <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> | ||
| </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" /> | ||
|
|
||
| <button id="set" type="button">Set Alarm</button> | ||
| <button id="stop" type="button">Stop Alarm</button> | ||
| </div> | ||
| <script src="alarmclock.js"></script> | ||
| </body> | ||
| </html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <link rel="stylesheet" href="style.css" /> | ||
| <title>Alarm Clock</title> | ||
|
|
||
| <body> | ||
| <div class="centre"> | ||
| <h1 id="timeRemaining">Time Remaining: 00:00</h1> | ||
| <label for="alarmSet">Set time to:</label> | ||
| <input id="alarmSet" type="number" /> | ||
|
|
||
| <button id="set" type="button">Set Alarm</button> | ||
| <button id="stop" type="button">Stop Alarm</button> | ||
| </div> | ||
| <script src="alarmclock.js"></script> | ||
| </body> | ||
|
|
||
| </html> |
Uh oh!
There was an error while loading. Please reload this page.