Skip to content

Commit 2e8d9f5

Browse files
committed
feat: enhance alarm functionality with input validation and random background color on alarm trigger
1 parent 088efa0 commit 2e8d9f5

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
11
let countdown;
22

33
function setAlarm() {
4-
let seconds = parseInt(document.getElementById("alarmSet").value);
5-
64
clearInterval(countdown);
7-
updateDisplay(seconds);
5+
document.body.style.backgroundColor = "white";
86

9-
countdown = setInterval(() => {
10-
seconds--;
7+
let inputTime = Number(document.getElementById("alarmSet").value);
8+
9+
if (isNaN(inputTime) || inputTime <= 0) {
10+
alert("Please type or select your time 👇⏰");
11+
return;
12+
}
1113

12-
updateDisplay(seconds);
14+
updateDisplay(inputTime);
15+
16+
countdown = setInterval(() => {
17+
inputTime--;
1318

14-
if (seconds <= 0) {
19+
if (inputTime <= 0) {
1520
clearInterval(countdown);
21+
updateDisplay(0);
1622
playAlarm();
23+
24+
let repetitions = 0;
25+
countdown = setInterval(() => {
26+
document.body.style.backgroundColor = `rgb(
27+
${Math.floor(Math.random() * 256)},
28+
${Math.floor(Math.random() * 256)},
29+
${Math.floor(Math.random() * 256)})`;
30+
repetitions++;
31+
32+
if (repetitions > 100) {
33+
clearInterval(countdown);
34+
}
35+
}, 200);
36+
} else {
37+
updateDisplay(inputTime);
1738
}
1839
}, 1000);
1940
}
@@ -29,8 +50,6 @@ function updateDisplay(seconds) {
2950
document.getElementById("timeRemaining").innerText = display;
3051
}
3152

32-
33-
3453
// DO NOT EDIT BELOW HERE
3554

3655
let audio = new Audio("assets/trebolClan.mp3");
@@ -54,8 +73,7 @@ function pauseAlarm() {
5473
audio.pause();
5574
stopAudio.play();
5675
clearInterval(countdown);
76+
// document.body.style.backgroundColor = "white";
5777
}
5878

5979
window.onload = setup;
60-
61-

0 commit comments

Comments
 (0)