Skip to content

Commit 32b655e

Browse files
committed
complete alarmclock.js - all tests pass
1 parent c12f544 commit 32b655e

1 file changed

Lines changed: 36 additions & 9 deletions

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,49 @@ const input = document.querySelector("#alarmSet");
33
const startBtn = document.querySelector("#set");
44
const stopBtn = document.querySelector("#stop");
55

6+
let timer = null;
7+
8+
function renderTime(totalSeconds) {
9+
const minutes = Math.floor(totalSeconds / 60);
10+
const seconds = totalSeconds % 60;
11+
const mm = String(minutes).padStart(2, "0");
12+
const ss = String(seconds).padStart(2, "0");
13+
heading.innerText = `Time Remaining: ${mm}:${ss}`;
14+
}
15+
616
function setAlarm() {
7-
let totalSeconds = Number(input.value);
8-
let timer = null;
17+
const rawValue = input.value.trim();
18+
if (rawValue === "") {
19+
return;
20+
}
21+
22+
let totalSeconds = Number(rawValue);
23+
if (Number.isNaN(totalSeconds) || totalSeconds < 0) {
24+
return;
25+
}
26+
27+
if (timer !== null) {
28+
clearInterval(timer);
29+
}
30+
31+
renderTime(totalSeconds);
32+
33+
if (totalSeconds === 0) {
34+
playAlarm();
35+
timer = null;
36+
input.value = "";
37+
return;
38+
}
939

1040
timer = setInterval(() => {
11-
const minutes = Math.floor(totalSeconds / 60);
12-
const seconds = totalSeconds % 60;
13-
const mm = String(minutes).padStart(2, "0");
14-
const ss = String(seconds).padStart(2, "0");
15-
heading.innerText = `Time Remaining: ${mm}:${ss}`;
41+
totalSeconds--;
42+
renderTime(totalSeconds);
43+
1644
if (totalSeconds === 0) {
45+
clearInterval(timer);
1746
timer = null;
1847
playAlarm();
19-
return;
2048
}
21-
totalSeconds--;
2249
}, 1000);
2350

2451
input.value = "";

0 commit comments

Comments
 (0)