Skip to content

Commit 9c3ee51

Browse files
committed
passed all the tests
1 parent d33934f commit 9c3ee51

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ let timer = null;
77
let flashing = null;
88

99
// DOM references
10-
const display = document.getElementById("timeRemaining");
10+
window.addEventListener("DOMContentLoaded", () => {
11+
// const display = document.getElementById("timeRemaining");
1112
const setButton = document.getElementById("set");
1213
const stopButton = document.getElementById("stop");
1314

1415
// Event listeners
15-
setButton.addEventListener("click", setAlarm);
16-
stopButton.addEventListener("click", stopAlarm);
16+
// if (setButton) setButton.addEventListener("click", () => playAlarm());
17+
if (stopButton) stopButton.addEventListener("click", stopAlarm);
1718

1819
// Show 00:00 on load
1920
updateDisplay(0);
21+
});
2022

2123
// -------------------------------
2224
// FUNCTIONS
@@ -53,18 +55,23 @@ function setAlarm() {
5355
function updateDisplay(seconds) {
5456
const mins = String(Math.floor(seconds / 60)).padStart(2, "0");
5557
const secs = String(seconds % 60).padStart(2, "0");
56-
display.innerText = `Time Remaining: ${mins}:${secs}`;
58+
59+
const display = document.getElementById("timeRemaining");
60+
if (!display) return;
61+
display.textContent = `Time Remaining: ${mins}:${secs}`;
5762
}
5863

5964
function startAlarm() {
60-
if (typeof playAlarm === "function") playAlarm();
65+
playAlarm();
6166

6267
// Flashing background
63-
flashing = setInterval(() => {
68+
if (!flashing){
69+
flashing = setTimeout(() => {
6470
document.body.style.backgroundColor =
6571
document.body.style.backgroundColor === "red" ? "orange" : "red";
6672
}, 300);
6773
}
74+
}
6875

6976
function stopAlarm() {
7077
if (typeof pauseAlarm === "function") pauseAlarm();
@@ -74,7 +81,8 @@ function stopAlarm() {
7481
document.body.style.backgroundColor = "";
7582
}
7683

77-
module.exports= setAlarm;
84+
85+
// module.exports= setAlarm;
7886

7987

8088
// DO NOT EDIT BELOW HERE

Sprint-3/alarmclock/alarmclock.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ There are some Tests in this file that will help you work out if your code is wo
33
*/
44

55
const path = require("path");
6+
// const { setAlarm } = require("./alarmclock.js");
7+
68
const { JSDOM } = require("jsdom");
79

810
let page = null;
@@ -91,16 +93,16 @@ test("should count down every 1000 ms", () => {
9193

9294
test("should play audio when the timer reaches zero", () => {
9395
const input = page.window.document.querySelector("#alarmSet");
94-
const button = page.window.document.querySelector("#set");
96+
const startButton = page.window.document.querySelector("#set");
9597
const mockPlayAlarm = jest.fn();
9698

9799
page.window.playAlarm = mockPlayAlarm;
98100
input.value = "10";
99-
button.click();
101+
startButton.click();
100102

101103
expect(mockPlayAlarm).toHaveBeenCalledTimes(0);
102104

103105
jest.runAllTimers();
104-
106+
105107
expect(mockPlayAlarm).toHaveBeenCalledTimes(1);
106108
});

Sprint-3/alarmclock/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ <h1 id="timeRemaining">Time Remaining: 00:00</h1>
1515
<button id="set" type="button">Set Alarm</button>
1616
<button id="stop" type="button">Stop Alarm</button>
1717
</div>
18-
<script src="alarmclock.js"></script>
18+
<script src="alarmclock.js" defer></script>
1919
</body>
2020
</html>

Sprint-3/alarmclock/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@
1313
"bugs": {
1414
"url": "https://github.com/CodeYourFuture/CYF-Coursework-Template/issues"
1515
},
16-
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme"
16+
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
17+
"dependencies": {
18+
"@testing-library/jest-dom": "^6.9.1",
19+
"jest": "^27.5.1"
20+
}
1721
}

0 commit comments

Comments
 (0)