Skip to content

Commit db4559d

Browse files
committed
Alarm clock function implemented
1 parent 084497d commit db4559d

File tree

3 files changed

+117
-5
lines changed

3 files changed

+117
-5
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,95 @@
1-
function setAlarm() {}
1+
// -------- Alarm Clock Implementation --------
2+
let countdownInterval = null;
3+
let flashInterval = null;
4+
let timeLeft = 0;
5+
let paused = false;
6+
7+
function setAlarm() {
8+
const input = document.getElementById("alarmSet");
9+
const heading = document.getElementById("timeRemaining");
10+
11+
const secondsInput = parseInt(input.value, 10);
12+
13+
if (isNaN(secondsInput) || secondsInput < 0) return;
14+
15+
// Reset previous timer if any
16+
if (countdownInterval) clearInterval(countdownInterval);
17+
stopFlashing();
18+
paused = false;
19+
timeLeft = secondsInput;
20+
21+
updateHeading(timeLeft);
22+
23+
countdownInterval = setInterval(() => {
24+
if (!paused) {
25+
timeLeft--;
26+
if (timeLeft <= 0) {
27+
clearInterval(countdownInterval);
28+
countdownInterval = null;
29+
updateHeading(0);
30+
playAlarm();
31+
startFlashing();
32+
} else {
33+
updateHeading(timeLeft);
34+
}
35+
}
36+
}, 1000);
37+
}
38+
39+
function pauseAlarm() {
40+
paused = true;
41+
if (audio) audio.pause();
42+
stopFlashing();
43+
}
44+
45+
function resumeAlarm() {
46+
if (timeLeft > 0) paused = false;
47+
}
48+
49+
function stopAlarm() {
50+
paused = true;
51+
if (audio) audio.pause();
52+
stopFlashing();
53+
clearInterval(countdownInterval);
54+
countdownInterval = null;
55+
timeLeft = 0;
56+
updateHeading(0);
57+
}
58+
59+
function resetAlarm() {
60+
paused = false;
61+
clearInterval(countdownInterval);
62+
countdownInterval = null;
63+
stopFlashing();
64+
timeLeft = 0;
65+
updateHeading(0);
66+
}
67+
68+
// -------- Helper Functions --------
69+
function updateHeading(seconds) {
70+
const heading = document.getElementById("timeRemaining");
71+
const mins = Math.floor(seconds / 60)
72+
.toString()
73+
.padStart(2, "0");
74+
const secs = (seconds % 60).toString().padStart(2, "0");
75+
heading.innerText = `Time Remaining: ${mins}:${secs}`;
76+
}
77+
78+
// ---------------- Flashing Screen ----------------
79+
function startFlashing() {
80+
const body = document.body;
81+
let isBlue = false;
82+
flashInterval = setInterval(() => {
83+
body.style.backgroundColor = isBlue ? "white" : "#add8e6";
84+
isBlue = !isBlue;
85+
}, 500);
86+
}
87+
88+
function stopFlashing() {
89+
clearInterval(flashInterval);
90+
flashInterval = null;
91+
document.body.style.backgroundColor = "white";
92+
}
293

394
// DO NOT EDIT BELOW HERE
495

@@ -9,16 +100,28 @@ function setup() {
9100
setAlarm();
10101
});
11102

12-
document.getElementById("stop").addEventListener("click", () => {
103+
document.getElementById("pause").addEventListener("click", () => {
13104
pauseAlarm();
14105
});
106+
107+
document.getElementById("resume").addEventListener("click", () => {
108+
resumeAlarm();
109+
});
110+
111+
document.getElementById("stop").addEventListener("click", () => {
112+
stopAlarm();
113+
});
114+
115+
document.getElementById("reset").addEventListener("click", () => {
116+
resetAlarm();
117+
});
15118
}
16119

17120
function playAlarm() {
18121
audio.play();
19122
}
20123

21-
function pauseAlarm() {
124+
function pauseAlarmSound() {
22125
audio.pause();
23126
}
24127

Sprint-3/alarmclock/alarmclock.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
There are some Tests in this file that will help you work out if your code is working.
33
*/
44

5+
require("@testing-library/jest-dom");
56
const path = require("path");
67
const { JSDOM } = require("jsdom");
78

@@ -102,3 +103,6 @@ test("should play audio when the timer reaches zero", () => {
102103

103104
expect(mockPlayAlarm).toHaveBeenCalledTimes(1);
104105
});
106+
107+
108+
// In alarmclock.test.js tests passed

Sprint-3/alarmclock/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"license": "CC-BY-SA-4.0",
55
"description": "You must update this package",
66
"scripts": {
7-
"test": "jest --config=../jest.config.js alarmclock"
7+
"test": "jest --config=jest.config.js alarmclock"
8+
89
},
910
"repository": {
1011
"type": "git",
@@ -13,5 +14,9 @@
1314
"bugs": {
1415
"url": "https://github.com/CodeYourFuture/CYF-Coursework-Template/issues"
1516
},
16-
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme"
17+
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
18+
"devDependencies": {
19+
"@testing-library/jest-dom": "^6.9.1",
20+
"jsdom": "^20.0.3"
21+
}
1722
}

0 commit comments

Comments
 (0)