Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 98 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,101 @@
function setAlarm() {}


// function setAlarm() {}

let timeLeft = 0;
let timer = null;
let flashing = null;

// DOM references
window.addEventListener("DOMContentLoaded", () => {
// const display = document.getElementById("timeRemaining");
const setButton = document.getElementById("set");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still need this variable?

const stopButton = document.getElementById("stop");

// Event listeners
// if (setButton) setButton.addEventListener("click", () => playAlarm());
if (stopButton) stopButton.addEventListener("click", stopAlarm);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callback added using .addEventListener() will not replace existing callbacks.
So now whenever the "stop" button is clicked, both pauseAlarm() (called on line 110) and stopAlarm() will be called; you don't have to call pauseAlarm() again in stopAlarm().


// Show 00:00 on load
updateDisplay(0);
});

// -------------------------------
// FUNCTIONS
// -------------------------------

function setAlarm() {
const inputEl = document.getElementById("alarmSet");
const input = inputEl.value.trim();

// Check empty input
if (input === "") {
alert("Please enter a number of seconds.");
return;
}

const parsed = parseInt(input, 10);

// Check invalid or negative number
if (isNaN(parsed) || parsed < 0) {
alert("Please enter a valid non-negative number.");
return;
}

timeLeft = parsed;

// Update display immediately
updateDisplay(timeLeft);

// Clear previous countdown
clearInterval(timer);

Comment on lines +50 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you think of anything else that should also be reset before starting a new countdown clock?

// Start countdown every 1000ms
timer = setInterval(() => {
if (timeLeft > 0) {
timeLeft--;
updateDisplay(timeLeft);
}

if (timeLeft === 0) {
clearInterval(timer);
startAlarm();
}
}, 1000);
}
Comment on lines +54 to +65
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alarm will start 1 second after the alarm is set when the input is either 0 or 1.
That is, when the input is zero, the alarm will only start 1 second afterward (and not immediately).
Can you improve the consistency?


function updateDisplay(seconds) {
const mins = String(Math.floor(seconds / 60)).padStart(2, "0");
const secs = String(seconds % 60).padStart(2, "0");

const display = document.getElementById("timeRemaining");
if (!display) return;
display.textContent = `Time Remaining: ${mins}:${secs}`;
}

function startAlarm() {
playAlarm();

// Flashing background
if (!flashing){
flashing = setTimeout(() => {
document.body.style.backgroundColor =
document.body.style.backgroundColor === "red" ? "orange" : "red";
}, 300);
}
}

function stopAlarm() {
if (typeof pauseAlarm === "function") pauseAlarm();

clearInterval(flashing);
flashing = null;
document.body.style.backgroundColor = "";
}


// module.exports= setAlarm;


// DO NOT EDIT BELOW HERE

Expand Down
10 changes: 7 additions & 3 deletions Sprint-3/alarmclock/alarmclock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ There are some Tests in this file that will help you work out if your code is wo
*/

const path = require("path");
// const { setAlarm } = require("./alarmclock.js");

const { JSDOM } = require("jsdom");

let page = null;
Expand Down Expand Up @@ -35,6 +37,8 @@ afterEach(() => {
page = null;
});



test("should set heading when button is clicked", () => {
const heading = page.window.document.querySelector("#timeRemaining");
const input = page.window.document.querySelector("#alarmSet");
Expand Down Expand Up @@ -89,16 +93,16 @@ test("should count down every 1000 ms", () => {

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

page.window.playAlarm = mockPlayAlarm;
input.value = "10";
button.click();
startButton.click();

expect(mockPlayAlarm).toHaveBeenCalledTimes(0);

jest.runAllTimers();

expect(mockPlayAlarm).toHaveBeenCalledTimes(1);
});
4 changes: 2 additions & 2 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Title here</title>
<title>Alarm clock-app</title>
</head>
<body>
<div class="centre">
Expand All @@ -15,6 +15,6 @@ <h1 id="timeRemaining">Time Remaining: 00:00</h1>
<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
</div>
<script src="alarmclock.js"></script>
<script src="alarmclock.js" defer></script>
</body>
</html>
6 changes: 6 additions & 0 deletions Sprint-3/alarmclock/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('jest').Config} */
module.exports = {
testEnvironment: "jsdom",
verbose: true,
testMatch: ["**/*.test.js"],
};
6 changes: 5 additions & 1 deletion Sprint-3/alarmclock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
"bugs": {
"url": "https://github.com/CodeYourFuture/CYF-Coursework-Template/issues"
},
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme"
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
"dependencies": {
"@testing-library/jest-dom": "^6.9.1",
"jest": "^27.5.1"
}
}