LONDON | MAY 2025 | JESUS DEL MORAL | SPRINT3 | ALARM CLOCK#671
LONDON | MAY 2025 | JESUS DEL MORAL | SPRINT3 | ALARM CLOCK#671delmorallopez wants to merge 9 commits intoCodeYourFuture:mainfrom
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
-
After the user clicks the "Set Alarm" button, there is a one second delay before the clock starts counting down. For example, if the input is 1, then the alarm will only sound 2 seconds after the user clicks the "Set Alarm" button. Can you address this issue?
-
Branch is not clean. Can you revert the change made in the
quote-generatorfolder?
| timeSetup = document.querySelector("#alarmSet"); // Access to alarmset | ||
|
|
||
| timeCountdown = document.querySelector("#timeRemaining"); // Access to timeRemaining |
There was a problem hiding this comment.
Where did you first declare these two variables?
There was a problem hiding this comment.
I declared at the begining, before setAlarm function()
There was a problem hiding this comment.
Lines 1 and 3 are not "variable declaration"; they are merely two assignment statements.
Line 5 is a variable declaration.
| const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); | ||
| const seconds = String(totalSeconds % 60).padStart(2, "0"); | ||
|
|
||
| timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; |
There was a problem hiding this comment.
If input is an invalid number, we should avoid changing the clock display (and then immediately replace the clock display by another text (at line 24).
There was a problem hiding this comment.
Line 24 has been modified, if is an invalid number, the display clock won't change
if (isNaN(totalSeconds) || totalSeconds <= 0) {
timeCountdown.innerText = "Time Remaining: 00:00"; // Return if the input is not a number or less than or equal to 0
return;
}
| let countdownInterval; // Variable to setup to 0 the countdown | ||
|
|
||
| function setAlarm() { | ||
| const totalSeconds = parseInt(timeSetup.value, 10); |
There was a problem hiding this comment.
Without sanitizing the input, the display could get corrupted.
There was a problem hiding this comment.
I validate the input
const rawInput = timeSetup.value.trim(); // Remove surrounding spaces
const totalSeconds = parseInt(rawInput, 10);
// Validate the input
if (isNaN(totalSeconds) || totalSeconds < 0) {
timeCountdown.innerText = "Please enter a valid number of seconds.";
document.body.style.backgroundColor = "";
return;
}
cjyuan
left a comment
There was a problem hiding this comment.
These two issues still have not yet been addressed:
-
After the user clicks the "Set Alarm" button, there is a one second delay before the clock starts counting down. For example, if the input is 1, then the alarm will only sound 2 seconds after the user clicks the "Set Alarm" button. Can you address this issue?
-
Branch is not clean. Can you revert the change made in the quote-generator folder?
| timeSetup = document.querySelector("#alarmSet"); // Access to alarmset | ||
|
|
||
| timeCountdown = document.querySelector("#timeRemaining"); // Access to timeRemaining |
There was a problem hiding this comment.
Lines 1 and 3 are not "variable declaration"; they are merely two assignment statements.
Line 5 is a variable declaration.
| const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); | ||
| const seconds = String(totalSeconds % 60).padStart(2, "0"); | ||
|
|
||
| timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; |
There was a problem hiding this comment.
If you define updateDisplay() with a parameter (and define it in the outer scope), you could replace these three lines of code with a function call.
There was a problem hiding this comment.
I called the function updateDisplay() passing the parameter -- updateDisplay(totalSeconds);
I declarated the variable total second in the outer scope, its working fine
| const updateDisplay = () => { | ||
| const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); | ||
| const seconds = String(totalSeconds % 60).padStart(2, "0"); | ||
| timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; | ||
| }; | ||
|
|
||
| updateDisplay(); // Show first tick instantly | ||
|
|
||
| countdownInterval = setInterval(() => { | ||
| totalSeconds--; | ||
|
|
||
| if (totalSeconds < 0) { | ||
| clearInterval(countdownInterval); | ||
| timeCountdown.innerText = "Time's up!"; | ||
| document.body.style.backgroundColor = "#ff4c4c"; | ||
| playAlarm(); | ||
| return; | ||
| } | ||
|
|
||
| updateDisplay(); | ||
| }, 1000); | ||
| } |
There was a problem hiding this comment.
Can you improve the indentation of this code?
|
|
||
| // Validate the input | ||
| if (isNaN(totalSeconds) || totalSeconds < 0) { | ||
| timeCountdown.innerText = "Please enter a valid number of seconds."; |
There was a problem hiding this comment.
Did you ever see your app displaying ""Please enter a valid number of seconds."? (If not, it may imply the code is not needed)
What does the function setAlarm() do? (It currently does not actually set any alarm)
The code that actually set the alarm is in startCountDown().
If you combine the code in setAlarm() and the code in startCountDown(), you should discover some redundant code.
There was a problem hiding this comment.
I modified setalarm(), where the function will call updateDisplay()....
No redundancy code now!
I commited the last changes....
I am struggling to changes made in the previous commit in order to remove the quote generator files... I will ask for help on Saturday to a volunteer
| function startCountdown() { | ||
| let totalSeconds = isValidInput(timeSetup.value); | ||
|
|
||
| if (totalSeconds === null) { | ||
| timeCountdown.innerText = "Time Remaining: 00:00"; | ||
| return; | ||
| } | ||
|
|
||
| clearInterval(countdownInterval); // Stop previous countdown | ||
| updateDisplay(totalSeconds); // Show first tick immediately |
There was a problem hiding this comment.
You can consider implementing startCountdown() in the following way:
function startCountDown(totalSeconds) { // take a parameter
// Delete the code on lines 32-38, 40 (The code in setAlarm() has performed those tasks)
..
}
Then instead of calling startCountDown() on line 65, call it at the end of setAlarm() with the appropriate argument.
| } | ||
|
|
||
| startCountdown(totalSeconds); // Start the countdown timer | ||
| updateDisplay(totalSeconds); |
There was a problem hiding this comment.
You are already calling updateDisplay() in startCoutndown(). There is no need to call it again.
|
I moved the totalSeconds-- after the if check. That way, when totalSeconds reaches 0, the alarm triggers immediately. |
|
Did you push you changes to GitHub? |
|
The changes has been pushed |
|
The latest copy of the file you pushed to GitHub looks like an older version of the file. The recent changes you made are all gone. The current version is like the first version I reviewed. Can you double check to see if you have submitted the correct file? |
|
Last commit has been uploaded, I am sorry @cjyuan |
|
Changes look good. Well done |
| if (!/^\d+$/.test(trimmed)) return null; | ||
|
|
||
| const num = Number(trimmed); | ||
| return num > 0 ? num : null; |
There was a problem hiding this comment.
I think NaN is better than null because NaN is also of type number.
PR of AlarmClock exercise, include 3 files:
I setup the option to change the colour background when the timer reach zero