London|26-ITP-January|Alexandru Pocovnicu|Sprint 3| alarm clock#1057
London|26-ITP-January|Alexandru Pocovnicu|Sprint 3| alarm clock#1057alexandru-pocovnicu wants to merge 25 commits intoCodeYourFuture:mainfrom
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
Code works fine if a user only clicks the "Set Alarm" button once.
However, if the user enters a time and then clicks the "Set Alarm" button multiple times, the countdown clock will not display properly.
Can you fix the issue?
Currently when starting a new countdown, the application does not always return to a clean initial state, which can lead to inconsistent behaviour between runs.
Consider introducing a dedicated reset function to return the app to a clean initial state to help ensure consistency.
|
when i press stopalarm i want the countdown to go to 0,call pauseAlarm(), and the class to be removed from the body,but on line 48 i have this message // DO NOT EDIT BELOW HERE, I can't really think of a way of doing it without adding code bellow that |
cjyuan
left a comment
There was a problem hiding this comment.
Note: To reset countdown display when the user clicks "Stop", and without modifying existing code, you can add another event listener to the stop button:
document.getElementById("stop").addEventListener("click", () => {
// Your code here ...
});
Sprint-3/alarmclock/alarmclock.js
Outdated
| let paddedSeconds = seconds.toString().padStart(2, "0"); | ||
| let paddedMinutes = minutes.toString().padStart(2, "0"); | ||
| timeRemainingEl.innerHTML = `Time Remaining: ${paddedMinutes}:${paddedSeconds}`; | ||
| alarmSetEl.value = null; |
There was a problem hiding this comment.
Why clear the input field when updating the timer display? When a countdown is active, it will be cleared every second.
There was a problem hiding this comment.
didn't realise it was doing it every second , now there is a dedicated function to clear everything when the alarm is set
Sprint-3/alarmclock/alarmclock.js
Outdated
| const pauseButton = document.getElementById("pause-button"); | ||
| if (pauseButton) { | ||
| pauseButton.removeAttribute("id"); | ||
| pauseButton.classList.add("unpause-alarm"); | ||
| pauseButton.addEventListener("click", pauseCountDown); | ||
| } |
There was a problem hiding this comment.
This is an unusual way to perform the tasks once (or to make the pause button visible when the "Start alarm" button is clicked the first time). Why not perform the task once on page load?
Currently unpause-alarm class does not have any style defined in style.css. (Work in progress?)
There was a problem hiding this comment.
i was thinking to use it , since i was removing the id I needed something to work with, i removed both from css
| cleanInitialState(); | ||
|
|
||
| function updateCountDown() { | ||
| remainingSeconds = totalSeconds; |
There was a problem hiding this comment.
You have already assigned totalSeconds to remainingSeconds on line 16, why not just use only remainingSeconds in the remaining part of the function?
Sprint-3/alarmclock/alarmclock.js
Outdated
| let seconds = totalSeconds % 60; | ||
| let minutes = (totalSeconds - seconds) / 60; | ||
|
|
||
| let paddedSeconds = seconds.toString().padStart(2, "0"); | ||
| let paddedMinutes = minutes.toString().padStart(2, "0"); | ||
| timeRemainingEl.innerHTML = `Time Remaining: ${paddedMinutes}:${paddedSeconds}`; |
There was a problem hiding this comment.
Better to implement the logic to "update the displayed time" in a function, and then use a function call to replace the code on lines 30-35, 66-71, 85.
Pros:
- Centralised logic (improved maintainability)
- Less code
| intervalId = setInterval(() => { | ||
| if (remainingSeconds <= 0) { | ||
| clearInterval(intervalId); | ||
| return; | ||
| } | ||
| remainingSeconds -= 1; | ||
| const seconds = remainingSeconds % 60; | ||
| const minutes = (remainingSeconds - seconds) / 60; | ||
| const paddedSeconds = seconds.toString().padStart(2, "0"); | ||
| const paddedMinutes = minutes.toString().padStart(2, "0"); | ||
| document.getElementById("timeRemaining").innerHTML = | ||
| `Time Remaining: ${paddedMinutes}:${paddedSeconds}`; | ||
|
|
||
| if (remainingSeconds <= 0) { | ||
| document.body.classList.add("finish-countdown"); | ||
| playAlarm(); | ||
| clearInterval(intervalId); | ||
| } | ||
| }, 1000); |
There was a problem hiding this comment.
It seems the code here is very similar to those perform in setAlarm(). Can you try reusing the code you have in setAlarm()?
Sprint-3/alarmclock/alarmclock.js
Outdated
| function cleanInitialState() { | ||
| clearInterval(intervalId); | ||
| document.body.classList.remove("finish-countdown"); | ||
| document.getElementById("timeRemaining").innerHTML = "Time Remaining: 00:00"; |
There was a problem hiding this comment.
Could also call updateDiplayedTime(0) to keep the logic of displaying time in one place.
| const pauseButton = document.getElementById("pause-button"); | ||
| pauseButton.textContent = "Pause"; | ||
| pauseButton.addEventListener("click", pauseCountDown); |
There was a problem hiding this comment.
You can reset the isPaused flag and the "Pause" button state in cleanInitialState(), and add event listener only once on page load.
Currently, the same event listener will be added to the button multiple times (once per function call).
There was a problem hiding this comment.
but if i move the listener inside cleanInitialState(), which is called everytime setAlarm() is called, isn't that the same thing, how about instead, i remove the listener inside setAlarm just before adding it?
thank you
There was a problem hiding this comment.
I mean, perform this in cleanInitialState()
const pauseButton = document.getElementById("pause-button");
pauseButton.textContent = "Pause";
and perform this on page load
window.addEventListener("load", function() {
const pauseButton = document.getElementById("pause-button");
pauseButton.addEventListener("click", pauseCountDown);
});
| clearInterval(intervalId); | ||
| document.body.classList.remove("finish-countdown"); | ||
| updateDisplayedTime(0) | ||
| document.getElementById("alarmSet").value = null; |
There was a problem hiding this comment.
.value is a string, so better to write document.getElementById("alarmSet").value = "";.
| const pauseButton = document.getElementById("pause-button"); | ||
| pauseButton.textContent = "Pause"; | ||
| pauseButton.addEventListener("click", pauseCountDown); |
There was a problem hiding this comment.
I mean, perform this in cleanInitialState()
const pauseButton = document.getElementById("pause-button");
pauseButton.textContent = "Pause";
and perform this on page load
window.addEventListener("load", function() {
const pauseButton = document.getElementById("pause-button");
pauseButton.addEventListener("click", pauseCountDown);
});
Learners, PR Template
Self checklist
Changelist
implemented an alarm clock that counts down from the input value