West Midlands | 25-ITP-Sept | Mustaf Asani | Sprint 3 | Feature/alarmclock#921
West Midlands | 25-ITP-Sept | Mustaf Asani | Sprint 3 | Feature/alarmclock#921asaniDev wants to merge 15 commits intoCodeYourFuture:mainfrom
Conversation
Sprint-3/alarmclock/alarmclock.js
Outdated
| if (timer) { | ||
| clearInterval(timer); | ||
| timer = null; | ||
| } |
There was a problem hiding this comment.
You may also want to stop the alarm (to reset all application states before starting a new countdown)
There was a problem hiding this comment.
added code to stop the alarm if it was playing
Sprint-3/alarmclock/alarmclock.js
Outdated
| clearInterval(timer); | ||
| timer = null; | ||
| } | ||
| timer = setInterval(countDown, 1000); |
There was a problem hiding this comment.
Line 20 will still get executed even when inputTime is 0.
| if (inputTime === 10) { | ||
| changeBgColor = true; | ||
| } else if (inputTime === 0) { | ||
| displayTime(inputTime); | ||
| playAlarm(); | ||
| document.querySelector("#alarmSet").reset(); | ||
| } | ||
| displayTime(inputTime); | ||
| if (typeof audio !== "undefined" && audio) { | ||
| audio.pause(); | ||
| try { | ||
| audio.currentTime = 0; | ||
| } catch (e) {} | ||
| audio.loop = false; // disable looping if it was set | ||
| } | ||
| if (timer) { | ||
| clearInterval(timer); | ||
| } | ||
| timer = setInterval(countDown, 1000); | ||
| } |
There was a problem hiding this comment.
When inputTime is 0, can you identify all the code between line 11 and 30 that will be executed? And among them, which code are unnecessarily executed?
Consider organising the code in the following maner:
// Step 1: Reset everything (timer, audio, background, ...) before starting a new countdown
// Note: Can also consider implement a function to reset everything and then call the function here
...
// Step 2; Validate input
...
// Step 3: Start a new countdown depending on the value of `inputTime`
...
cjyuan
left a comment
There was a problem hiding this comment.
Code looks much cleaner now.
| } | ||
|
|
||
| displayTime(inputTime); | ||
| timer = setInterval(countDown, 1000); |
There was a problem hiding this comment.
Does the app need to execute the code on line 20 when inputTime is 0?
Can you figure out what would happen when line 20 is executed when inputTime is 0? Even though the side-effect is almost unnoticeable, it is important we (as a programmer) recognise what our code do exactly.
Learners, PR Template
Self checklist
Changelist
Added functions to alarm to make the timer count down from input time and alarm to off when it reaches 0.