Skip to content

Commit d33934f

Browse files
committed
added a jest.config.js file
1 parent b3ad0d8 commit d33934f

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,81 @@
1-
function setAlarm() {}
1+
2+
3+
// function setAlarm() {}
4+
5+
let timeLeft = 0;
6+
let timer = null;
7+
let flashing = null;
8+
9+
// DOM references
10+
const display = document.getElementById("timeRemaining");
11+
const setButton = document.getElementById("set");
12+
const stopButton = document.getElementById("stop");
13+
14+
// Event listeners
15+
setButton.addEventListener("click", setAlarm);
16+
stopButton.addEventListener("click", stopAlarm);
17+
18+
// Show 00:00 on load
19+
updateDisplay(0);
20+
21+
// -------------------------------
22+
// FUNCTIONS
23+
// -------------------------------
24+
25+
function setAlarm() {
26+
const input = document.getElementById("alarmSet").value;
27+
const parsed = parseInt(input, 10);
28+
29+
if (isNaN(parsed) || parsed < 0) return;
30+
31+
timeLeft = parsed;
32+
33+
// Update display immediately
34+
updateDisplay(timeLeft);
35+
36+
// Clear previous countdown
37+
clearInterval(timer);
38+
39+
// Start countdown every 1000ms
40+
timer = setInterval(() => {
41+
if (timeLeft > 0) {
42+
timeLeft--;
43+
updateDisplay(timeLeft);
44+
}
45+
46+
if (timeLeft === 0) {
47+
clearInterval(timer);
48+
startAlarm();
49+
}
50+
}, 1000);
51+
}
52+
53+
function updateDisplay(seconds) {
54+
const mins = String(Math.floor(seconds / 60)).padStart(2, "0");
55+
const secs = String(seconds % 60).padStart(2, "0");
56+
display.innerText = `Time Remaining: ${mins}:${secs}`;
57+
}
58+
59+
function startAlarm() {
60+
if (typeof playAlarm === "function") playAlarm();
61+
62+
// Flashing background
63+
flashing = setInterval(() => {
64+
document.body.style.backgroundColor =
65+
document.body.style.backgroundColor === "red" ? "orange" : "red";
66+
}, 300);
67+
}
68+
69+
function stopAlarm() {
70+
if (typeof pauseAlarm === "function") pauseAlarm();
71+
72+
clearInterval(flashing);
73+
flashing = null;
74+
document.body.style.backgroundColor = "";
75+
}
76+
77+
module.exports= setAlarm;
78+
279

380
// DO NOT EDIT BELOW HERE
481

Sprint-3/alarmclock/alarmclock.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ afterEach(() => {
3535
page = null;
3636
});
3737

38+
39+
3840
test("should set heading when button is clicked", () => {
3941
const heading = page.window.document.querySelector("#timeRemaining");
4042
const input = page.window.document.querySelector("#alarmSet");

Sprint-3/alarmclock/jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** @type {import('jest').Config} */
2+
module.exports = {
3+
testEnvironment: "jsdom",
4+
verbose: true,
5+
testMatch: ["**/*.test.js"],
6+
};

0 commit comments

Comments
 (0)