Repository navigation
London | 26-ITP-Sept | Hugh Mills | Sprint 3 | Alarmclock #1588
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
396b747
47fc7f4
0af3983
23bfced
872a5f3
d761a99
56b0567
05a5f53
a8c4133
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,33 @@ | ||
| function setAlarm() {} | ||
| let timer; | ||
| function setAlarm() { | ||
| if (timer) { | ||
| clearInterval(timer); | ||
| } | ||
| const alarmTime = Number(document.getElementById("alarmSet").value); | ||
| let alarmTimeRemaining = Math.max(0, alarmTime); | ||
| if (alarmTimeRemaining === 0) { | ||
| playAlarm(); | ||
| return; | ||
| } | ||
| const min = Math.floor(alarmTimeRemaining / 60); | ||
| const sec = alarmTimeRemaining % 60; | ||
|
|
||
| document.getElementById("timeRemaining").textContent = | ||
| `Time Remaining: ${String(min).padStart(2, "0")}:${String(sec).padStart(2, "0")}`; | ||
|
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. This is a really complex piece of code for your level. Well done for using it! Can you explain what each section does?
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. Sure, ill try my best.
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. Oh sorry missed the each section bit:
``Time Remaining: ${String(min).padStart(2, "0")}:${String(sec).padStart(2, "0")} This lets me show on the HTML "Time Remaining 00:00" for the h1 area for timeRemaining.
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. I tried to use the ` in there to show code blocks in my reply and i see it messed up a bit ^_^ |
||
|
|
||
| timer = setInterval(() => { | ||
| alarmTimeRemaining = alarmTimeRemaining - 1; | ||
| if (alarmTimeRemaining === 0) { | ||
|
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 happens when the input is left blank or set to To "like" or "dislike" this comment, please follow this link
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. Yes it does as Number() takes a black input and makes it into 0, so it will become true. |
||
| clearInterval(timer); | ||
| playAlarm(); | ||
| } | ||
| const min = Math.floor(alarmTimeRemaining / 60); | ||
| const sec = alarmTimeRemaining % 60; | ||
|
|
||
|
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. Just another note, what happens if a negative number gets entered into the field? perhaps you could think of some ways to ensure this doesnt happen?
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. ah didnt think of that... could have it throw an error just just become 0 instantly... ill sort that out
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. I used Math.max(0, timeRemainig) to do that, this ensures that it cant be bellow 0 and putting in a negative number will trigger the alarm as though a 0 input was given. 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. Interesting choice! I wouldnt have thought of that! 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.
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. I see what I did... I didnt test the app on the site, I used the "npm test --...." I got it sorted out now. Think I'm racing and stressing myself out trying to play catch-up at the moment, need to chill a lille more and focus with a clear head ^_^
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.
I thought about it, if you have a clock jsut showing a screen and somehow put in a negative.. the simple way for a user is to trigger the alarm rather an displaying an error code.
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. I discovered another bug, more inputs before the timer goes off makes it having multiple countdowns in the same number area making it very wierd graphic glitch as the numbers override each other. Also when it hits the first alarm it can skip and start a negitive countdown. So added in clearInterval at the start of the function so every input clears the last. also having "let timer" outside the function so there is just 1 "timer" to run and not one for each time the function starts.... well I think thats how it works ^_^ 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. It does sound lik eyou need a bit of a break. Take a beat and come back to this, thats how the best solutions happen
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. That is what I was planning to do with it. Having it set off the alarm if its at 0 or bellow as you cant ccount down negatives. Should it rather show an error instead or do nothing if giving a negative, or force positive? yeah Having a break from teh coding for a lille, unwind and tackle some more later tonight ^_^ |
||
| document.getElementById("timeRemaining").textContent = | ||
| `Time Remaining: ${String(min).padStart(2, "0")}:${String(sec).padStart(2, "0")}`; | ||
| }, 1000); | ||
| } | ||
|
|
||
| // 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.
okay , this is the last bit you need to fix. Try entering a negative number in the input. What happens?
The alarm starts ringing immediately. Can you tell me why based on this line?
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.
Sure, when a negative number in put in the Math.max makes it 0 as I have 0 as the max that the number can be so anything under 0, in this case a negative, becomes 0. As I have no "return" or anything stopping the code there it goes to the next step which is the "setInterval".
This sees the value of 0 so it triggers the alarm, in the same was if you typed in 0.
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.
If I need to do something different for this please let me know as I can't seam to spot anything to change.
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 alarm starts ringing immediately when you put in a negative number. Thats not the intended functionality and so is a bug. You don't want the user to be able to put in a negative number at all, and you definitely don't want the alarm to start ringing immediately:) does that clear it up?