-
-
Notifications
You must be signed in to change notification settings - Fork 286
Manchester| 25-ITP-Sep| Fithi Teklom| Sprint 3 | Alarm Clock #908
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 10 commits
8085c0c
b3ad0d8
d33934f
9c3ee51
bffd4b4
7ded280
c9aa8f1
0b85c7a
e618de2
76f1971
b6af9d4
fc92b97
0c30fdd
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,105 @@ | ||
| function setAlarm() {} | ||
|
|
||
|
|
||
|
|
||
|
|
||
| let timeLeft = 0; | ||
| let timer = null; | ||
| let flashing = null; | ||
|
|
||
| // DOM references | ||
| window.addEventListener("DOMContentLoaded", () => { | ||
|
|
||
| const stopButton = document.getElementById("stop"); | ||
|
|
||
| // Event listeners | ||
|
|
||
| if (stopButton) stopButton.addEventListener("click", stopAlarm); | ||
|
|
||
| // Show 00:00 on load | ||
| updateDisplay(0); | ||
| }); | ||
|
|
||
| // ------------------------------- | ||
| // FUNCTIONS | ||
| // ------------------------------- | ||
|
|
||
| function setAlarm() { | ||
| const inputEl = document.getElementById("alarmSet"); | ||
| const input = inputEl.value.trim(); | ||
|
|
||
| // Check empty input | ||
| if (input === "") { | ||
| alert("Please enter a number of seconds."); | ||
| return; | ||
| } | ||
|
|
||
| const parsed = parseInt(input, 10); | ||
|
|
||
| // Check invalid or negative number | ||
| if (isNaN(parsed) || parsed < 0) { | ||
| alert("Please enter a valid non-negative number."); | ||
| return; | ||
| } | ||
|
|
||
| timeLeft = parsed; | ||
|
|
||
| // Update display immediately | ||
| updateDisplay(timeLeft); | ||
|
|
||
| // Clear previous countdown | ||
| clearInterval(timer); | ||
| clearInterval(flashing); // stop flashing if it was active | ||
| flashing = null; | ||
| document.body.style.backgroundColor = ""; | ||
| pauseAlarm(); // stop any playing audio | ||
| audio.currentTime = 0; | ||
|
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 this "reset" code in a function and call the function here. |
||
|
|
||
| function tick() { | ||
| if (timeLeft > 0) { | ||
| timeLeft--; | ||
| updateDisplay(timeLeft); | ||
| } else { | ||
| clearInterval(timer); | ||
| startAlarm(); | ||
| } | ||
| } | ||
|
|
||
|
cjyuan marked this conversation as resolved.
|
||
| // Run once immediately for consistency | ||
| tick(); | ||
| timer = setInterval(tick, 1000); | ||
|
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. Considering the following two cases: Case 1: When the input is 0 (i.e., Case 2: When the input is 1 (i.e., Suggestion: Since 0 is such a special case, you can consider doing something like |
||
| } | ||
|
|
||
| function updateDisplay(seconds) { | ||
| const mins = String(Math.floor(seconds / 60)).padStart(2, "0"); | ||
| const secs = String(seconds % 60).padStart(2, "0"); | ||
|
|
||
| const display = document.getElementById("timeRemaining"); | ||
| if (!display) return; | ||
| display.textContent = `Time Remaining: ${mins}:${secs}`; | ||
| } | ||
|
|
||
| function startAlarm() { | ||
| playAlarm(); | ||
|
|
||
| // Flashing background | ||
| if (!flashing){ | ||
| flashing = setTimeout(() => { | ||
| document.body.style.backgroundColor = | ||
| document.body.style.backgroundColor === "red" ? "orange" : "red"; | ||
| }, 300); | ||
| } | ||
| } | ||
|
|
||
| function stopAlarm() { | ||
|
|
||
| clearInterval(flashing); | ||
| flashing = null; | ||
| document.body.style.backgroundColor = ""; | ||
| } | ||
|
|
||
|
|
||
| // module.exports= setAlarm; | ||
|
|
||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
@@ -20,6 +121,6 @@ function playAlarm() { | |
|
|
||
| function pauseAlarm() { | ||
| audio.pause(); | ||
| } | ||
| } | ||
|
|
||
| window.onload = setup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /** @type {import('jest').Config} */ | ||
| module.exports = { | ||
| testEnvironment: "jsdom", | ||
| verbose: true, | ||
| testMatch: ["**/*.test.js"], | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.