Skip to content

Commit eeb63cd

Browse files
committed
cleaning code and fixing pre validation
1 parent e30ba62 commit eeb63cd

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const setButton = document.querySelector("#set");
55
const stopButton = document.querySelector("#stop");
66
let conuntdownInterval;
77

8-
// time management function
98
function formatTime(totalSeconds) {
109
const minutes = Math.floor(totalSeconds / 60);
1110
const seconds = totalSeconds % 60;
@@ -15,49 +14,50 @@ function formatTime(totalSeconds) {
1514
return `Time Remaining: ${paddedMinutes}:${paddedSeconds}`;
1615
}
1716

18-
// to clear every change made with conditions
19-
function clearEverything() {
17+
function reset() {
18+
clearInterval(conuntdownInterval);
19+
display.textContent = "Time Remaining: 00:00";
2020
display.style.color = "black";
2121
setButton.disabled = false;
2222
document.body.style.background = "";
2323
}
24-
25-
// button event handler
24+
let countdownInterval;
2625
setButton.addEventListener("click", () => {
2726
let timeleft = Number(totalSeconds.value);
28-
setButton.disabled = true;
2927

30-
// time 0
31-
32-
if (timeleft <= 0) {
33-
alert("please enter a number greater than 0!");
28+
if (timeleft <= 0 || isNaN(timeleft)) {
29+
alert("please enter a number greater than zero (0)!");
3430
return;
3531
}
32+
setButton.disabled = true;
3633

3734
display.textContent = formatTime(timeleft);
3835

36+
const warningTime = 10; // seconds
37+
clearInterval(conuntdownInterval);
38+
3939
conuntdownInterval = setInterval(() => {
40-
if (timeleft == 0) {
41-
clearInterval(conuntdownInterval);
42-
document.body.style.backgroundColor = "grey";
43-
playAlarm();
40+
timeleft--;
41+
42+
if (timeleft >= 0) {
43+
display.textContent = formatTime(timeleft);
4444
}
4545

46-
if (timeleft <= 10) {
46+
if (timeleft <= warningTime) {
4747
display.style.color = "red";
4848
}
4949

50-
if (timeleft >= 0) {
51-
display.textContent = formatTime(timeleft);
52-
timeleft--;
50+
if (timeleft <= 0) {
51+
clearInterval(conuntdownInterval);
52+
document.body.style.backgroundColor = "grey";
53+
playAlarm();
5354
}
5455
}, 1000);
5556
});
5657

57-
//stop button clicked
5858
stopButton.addEventListener("click", () => {
5959
clearInterval(conuntdownInterval);
60-
clearEverything();
60+
reset();
6161
pauseAlarm();
6262
});
6363

‎Sprint-3/alarmclock/index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ <h1 id="timeRemaining">Time Remaining: 00:00</h1>
1515
<button id="set" type="button">Set Alarm</button>
1616
<button id="stop" type="button">Stop Alarm</button>
1717
</div>
18-
<script src="alarmclock.js"></script>
18+
<script src="alarmclock.js" defer></script>
1919
</body>
2020
</html>

0 commit comments

Comments
 (0)