generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 210
WM | ITP-JAN-25 | HATEF_EIDI | Module-Data-Groups | Sprint 3| Alarm Clock #405
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| node_modules | ||
| .DS_Store | ||
| .vscode | ||
| **/.DS_Store | ||
| **/.DS_Store | ||
| .qodo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .qodo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Document</title> | ||
| </head> | ||
| <body> | ||
| <script defer src="./script.js"></script> | ||
| <section> | ||
| <h3>Example character limit comment component</h3> | ||
| <label for="comment-input"> | ||
| Please enter your comment in the text area below | ||
| </label> | ||
| <textarea | ||
| id="comment-input" | ||
| name="comment-input" | ||
| rows="5" | ||
| maxlength="200" | ||
| ></textarea> | ||
| <p id="character-limit-info">You have 200 characters remaining</p> | ||
| </section> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Given an textarea and a character limit of 200 | ||
| // When a user types characters into the textarea | ||
| // // Then the interface should update with how many characters they’ve got left. | ||
|
|
||
| //Steps | ||
| // 1. Set a character limit ot the text area | ||
| // 2. Access the textarea in the DOM | ||
| // 3. When an event occurs, we compare characterLimit with the current length of the text area, | ||
| //4. We must calculate the characters left | ||
| //.5 Update the text with the actual characters left | ||
|
|
||
|
|
||
| const characterLimit =200; | ||
| const textArea = document.querySelector('#comment-input'); | ||
|
|
||
|
|
||
|
|
||
| //Keyup event is fired when a key is released | ||
| //React to this event! | ||
|
|
||
| //Run some code, whenever the keyup event takes place? | ||
| textArea.addEventListener('keyup', function updateCharacterLimit(event) { | ||
| const characterLeft = characterLimit - textArea.value.length; | ||
| const characterLimitInfo = document.querySelector('#character-limit-info'); | ||
|
|
||
| characterLimitInfo.innerText = ` You have ${characterLeft} characters remaining `; | ||
| }); | ||
|
|
||
| //We could also create the event function outside of the event listener | ||
| // function updateCharacterLimit(event) { | ||
| // const characterLeft = characterLimit - textArea.value.length; | ||
| // const charLimitInfo = document.querySelector('#character-limit-info'); | ||
|
|
||
| // characterLimitInfo.innerText = ` You have ${characterLeft} characters remaining `; | ||
| // } | ||
|
|
||
| // textArea.addEventListener('keyup',updateCharacterLimit); | ||
|
|
||
| // Notice if we build the function outside of the event listener, we can call it directly from the event listener and we don't use parenthesis anymore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,67 @@ | ||
| function setAlarm() {} | ||
| function setAlarm() { | ||
| //Converting the time of now value to seconds from milliseconds | ||
| //Getting the time value from the input field, converting it into number, this will be in seconds | ||
| const alarmTime = Number(document.getElementById("alarmSet").value); | ||
|
|
||
| document.getElementById('timeRemaining').innerText="Time Remaining: "+timeFormatter(alarmTime); | ||
| let currentTime=0; | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
| const checkAlarm=setInterval(() => { | ||
|
|
||
| //Calculating current time inside the interval | ||
| currentTime++; | ||
|
|
||
| //Calculating the difference between the alarm time and the current time | ||
| const diff = alarmTime - currentTime; | ||
|
|
||
| //Updating the time remaining text in the HTML element | ||
| document.getElementById('timeRemaining').innerText="Time Remaining: "+timeFormatter(diff); | ||
|
|
||
|
|
||
|
|
||
| //If the difference is less than or equal to zero, stop the interval and play the alarm sound | ||
| if(diff <= 0){ | ||
| clearInterval(checkAlarm); | ||
| playAlarm(); | ||
|
|
||
|
|
||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| //formatting remaining seconds in the format of MM:SS | ||
| function timeFormatter(seconds) { | ||
|
Member
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. We normally name functions like verbs because they do things - so |
||
| const remainingSeconds = seconds % 60; | ||
| const remainingMinutes = Math.floor(seconds / 60); | ||
| return timePadder(remainingMinutes, remainingSeconds); | ||
| } | ||
| //Formatting the time in MM:SS format | ||
| function timePadder(min , sec){ | ||
| const minPadded = min.toString().padStart(2, '0'); | ||
| const secPadded = sec.toString().padStart(2, '0'); | ||
| return `${minPadded}:${secPadded}`; | ||
|
|
||
| } | ||
|
|
||
| let flashInterval; // To store the interval ID for flashing | ||
|
|
||
| function flashBackground() { | ||
| let isRed = false; | ||
|
|
||
| flashInterval = setInterval(() => { | ||
| // Toggle the background color between red and white | ||
| document.body.style.backgroundColor = isRed ? '#FDF7FA' : '#D67AB1'; | ||
| isRed = !isRed; | ||
| }, 1000); // Flash every second | ||
| } | ||
|
|
||
|
|
||
| function stopFlashing() { | ||
| clearInterval(flashInterval); // Stop the flashing by clearing the interval | ||
| document.body.style.backgroundColor = 'white'; // Reset the background color | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
| var audio = new Audio("alarmsound.mp3"); | ||
|
|
||
| function setup() { | ||
|
|
@@ -16,10 +76,14 @@ function setup() { | |
|
|
||
| function playAlarm() { | ||
| audio.play(); | ||
| //to flash background | ||
| flashBackground(); | ||
| } | ||
|
|
||
| function pauseAlarm() { | ||
| audio.pause(); | ||
| //to stop flashing background | ||
| stopFlashing(); | ||
| } | ||
|
|
||
| window.onload = setup; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This looks like the same code as is on line 6.
Let's imagine we wanted to change the text it showed from "Time Remaining:" to "Time Left:" - we would need to make sure we remembered to change both lines 6 and 18.
Can you think of a way to make it so that we would only need to change one line of code if we wanted to make that change, rather than two?
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.
The reason is as soon as the user set alarm the value is added to the title, I can only keep line 18, but the title updates after 1 second interval.
Now removed line 6, which means the title is updated after 1 second, if that needs to be changed and you prefer the user to see the title as soon as they click set alarm button, please let me know so I changed it to what it was before.
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 agree that you need to update the element both when the alarm is set, and when it changes. What I'm asking here is: How can you do that without repeating exactly the same code?