-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-May | Vito Moratti | Sprint 3 | Alarm-clock #1384
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
790e0b9
bbddfb4
d4e0bf2
0c860c9
7ecd8ad
9c1e20b
0150302
3e64600
db90544
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,89 @@ | ||
| function setAlarm() {} | ||
| //Given the user has entered a number in the input field | ||
| //When the user clicks the "Set Alarm" button | ||
| //Then the "Time Remaining" title should update to show the entered number in mm:ss format | ||
|
|
||
| //Given the alarm is set with a valid time | ||
| //When one second passes | ||
| //Then the "Time Remaining" title should decrement by 1 second | ||
|
|
||
| //Given the alarm is set with a time of 00:00 | ||
| //When the timer reaches 00:00 | ||
| //Then the alarm sound should play continuously | ||
|
|
||
| //Given the alarm sound is currently playing | ||
| //When the user clicks the "Stop Alarm" button | ||
| //Then the alarm sound should stop playing | ||
|
|
||
| //Given the alarm is set with a time of 00:10 | ||
| //When the timer reaches 00:00 | ||
| //Then the background colour should change | ||
| //And the alarm sound should play | ||
|
|
||
| //Given the user has not set an alarm | ||
| //When the page first loads | ||
| //Then the "Time Remaining" title should show 00:00 | ||
| // And no alarm sound should play | ||
|
|
||
| // 1. create variable that holds time entered by user | ||
| // 2. create a variable we are counting to | ||
| // 3. convert time to minutes and seconds and store it | ||
| // in variable | ||
| // 4. display the time in the "Time Remaining" title. | ||
| // when "Set alarm" button is clicked. | ||
| // 5. decrement the time by 1 second every second | ||
| // 6. trigger the alarm sound when the time reaches 00:00 | ||
| let remainingSeconds; | ||
| let timer; | ||
| const alarmDisplay = document.getElementById("timeRemaining"); | ||
| function formatTime(remainingSeconds) { | ||
| const minutes = Math.floor(remainingSeconds / 60); | ||
| const paddedMinutes = minutes.toString().padStart(2, "0"); | ||
|
|
||
| const seconds = remainingSeconds % 60; | ||
| const paddedSeconds = seconds.toString().padStart(2, "0"); | ||
|
|
||
| return `${paddedMinutes}:${paddedSeconds}`; | ||
| } | ||
| function setAlarm() { | ||
| //1.this function clears the timer for the first use | ||
| //2. gets the entered number and stores in in the variable | ||
| //3. checks for invalid input | ||
| //4. displays the initial timer in minutes and seconds | ||
| //5. starts timer | ||
| clearInterval(timer); | ||
| resetBackground(); | ||
| remainingSeconds = Number(document.getElementById("alarmSet").value); | ||
| if (remainingSeconds < 1) { | ||
| alarmDisplay.textContent = `Enter a valid number of seconds greater than 0`; | ||
| return pauseAlarm(); | ||
| } | ||
|
|
||
| alarmDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; | ||
| timer = setInterval(updateDisplay, 1000); | ||
| } | ||
| function changeBackground() { | ||
| document.body.classList.add("alarm-finished"); | ||
| } | ||
| function resetBackground() { | ||
| document.body.classList.remove("alarm-finished"); | ||
| } | ||
|
|
||
| function updateDisplay() { | ||
| //this function: | ||
| //1. when timer reaches zero it stops the timer. | ||
| //2. it then sounds alarm | ||
| //3. otherwise it decrements the timer and displays the result. | ||
| if (remainingSeconds < 1) { | ||
| clearInterval(timer); | ||
| changeBackground(); | ||
| return playAlarm(); | ||
| } | ||
| remainingSeconds = remainingSeconds - 1; | ||
| alarmDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; | ||
| } | ||
| function stopTimer() { | ||
|
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. Is stopTimer() still needed? I don't see it being called anywhere.
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. the stopTime() function is being used by HTML file on line 16 by "Stop alarm" button. |
||
| clearInterval(timer); | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
||
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.
Is it possible to make this function act like a toggle? Currently it turns red and doesn't return to the original state when setting a new alarm.
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.
I have made the adjustments to accommodate that