Skip to content

Commit bf1c808

Browse files
I refactored the functionality of displaying the alarm clock with the raised Qs
1 parent 4c30695 commit bf1c808

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,53 @@
11
let intervalId = null;
22
let totalSeconds = 0;
3+
const input = document.getElementById("alarmSet");
4+
const timeRemaining = document.getElementById("timeRemaining");
5+
const message = document.getElementById("message");
36

4-
function formatTime(seconds){
5-
const minutes = Math.floor(seconds/ 60);
7+
function formatTime(seconds) {
8+
const minutes = Math.floor(seconds / 60);
69
const secs = seconds % 60;
7-
return `${string(minutes).padStart(2,"0")}:${String(secs).padStart(2,"0")}`;
10+
return `${String(minutes).padStart(2, "0")}:${String(secs).padStart(2, "0")}`;
11+
}
12+
function updateDisplay() {
13+
timeRemaining.textContent = `Time Remaining: ${formatTime(totalSeconds)}`;
14+
}
815

16+
function resetAlarm() {
17+
clearInterval(intervalId);
18+
intervalId = null;
19+
audio.pause();
20+
audio.currentTime = 0;
921
}
22+
function getTime() {
23+
const time = Number(input.value);
24+
if (!(Number.isInteger(time) && time >= 1)) {
25+
message.textContent = "Enter a valid number";
26+
return null;
27+
}
28+
return time;
29+
}
30+
1031
function setAlarm() {
11-
// clearing any previous countdown
12-
if (intervalId !== null){
13-
clearInterval(intervalId)
32+
const time = getTime();
33+
if (time === null) {
34+
return;
1435
}
15-
const input = document.getElementById("alarmSet");
16-
totalSeconds = Number(input.value);
1736

18-
document.getElementById("timeRemaining").textContent = `Time Remaining: ${formatTime(totalSeconds)}`;
37+
resetAlarm();
38+
totalSeconds = time;
39+
updateDisplay();
1940

2041
intervalId = setInterval(() => {
2142
totalSeconds--;
22-
document.getElementById("timeRemaining").textContent = `Time Remaining: ${formatTime(totalSeconds)}`;
43+
updateDisplay();
2344

2445
if (totalSeconds <= 0) {
2546
clearInterval(intervalId);
2647
intervalId = null;
2748
playAlarm();
2849
}
2950
}, 1000);
30-
3151
}
3252

3353
// DO NOT EDIT BELOW HERE

‎Sprint-3/alarmclock/index.html‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />
@@ -9,6 +9,7 @@
99
<body>
1010
<div class="centre">
1111
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
12+
<h2 id="message"></h2>
1213
<label for="alarmSet">Set time to:</label>
1314
<input id="alarmSet" type="number" />
1415

0 commit comments

Comments
 (0)