Skip to content

Sheffield| May 2025| Mayowa Fadare| Sprint 3| Alarm Clock #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
60 changes: 47 additions & 13 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,59 @@
function setAlarm() {}
let timerId = null;
let timeLeft = 0;
let audio;

// DO NOT EDIT BELOW HERE
function startAlarm() {
const input = document.getElementById("alarmSet");
timeLeft = parseInt(input.value, 10);

var audio = new Audio("alarmsound.mp3");
if (isNaN(timeLeft) || timeLeft <= 0) {
document.getElementById("timeRemaining").innerText = "Please enter a valid time.";
return;
}
document.getElementById("alarmSet").disabled = true;
document.getElementById("start").disabled = true;
document.getElementById("stop").disabled = false;
document.getElementById("timeRemaining").innerText = formatTime(timeLeft);
timerId = setInterval(() => {
timeLeft--;
if (timeLeft <= 0) {
clearInterval(timerId);
timerId = null;
playAlarm();
}
}, 1000);
}

function setup() {
document.getElementById("set").addEventListener("click", () => {
setAlarm();
});
function stopAlarm() {
if (timerId) {
clearInterval(timerId);
timerId = null;
}
if (audio) {
audio.pause();
audio.currentTime = 0;
}
document.getElementById("alarmSet").disabled = false;
document.getElementById("start").disabled = false;
document.getElementById("stop").disabled = true;
document.getElementById("timeRemaining").innerText = formatTime(0);
}

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
});
function formatTime(seconds) {
const minutes = String(Math.floor(seconds / 60)).padStart(2, "0");
const secs = String(seconds % 60).padStart(2, "0");
return `Time Remaining: ${minutes}:${secs}`;
}

function playAlarm() {
audio.loop = true;
audio.play();
}

function pauseAlarm() {
audio.pause();
function setup() {
audio = document.getElementById("alarmAudio");
document.getElementById("start").addEventListener("click", startAlarm);
document.getElementById("stop").addEventListener("click", stopAlarm);
}

window.onload = setup;
window.onload = setup;
31 changes: 15 additions & 16 deletions Sprint-3/alarmclock/alarmclock.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { getTimeLeft } = require("./alarmclock");
//jest.advanceTimersByTime((getTimeLeft() + 1) * 1000);
/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
*/
Expand Down Expand Up @@ -26,11 +28,14 @@ beforeEach(async () => {
});

return new Promise((res) => {
page.window.document.addEventListener("load", res);
page.window.addEventListener("load", res);
});
});

afterEach(() => {
// Reset the timers after each test
jest.clearAllTimers();

jest.useRealTimers();
page = null;
});
Expand Down Expand Up @@ -75,30 +80,24 @@ test("should update the heading while counting down", () => {
test("should count down every 1000 ms", () => {
const input = page.window.document.querySelector("#alarmSet");
const button = page.window.document.querySelector("#set");

const mockTimer = jest.fn();
page.window.setTimeout = mockTimer;
page.window.setInterval = mockTimer;

const mockTimer = jest.spyOn(page.window, "setInterval");
mockTimer.mockClear();
input.value = "19";
button.click();

// Record the number of times setInterval has been called
expect(mockTimer).toHaveBeenCalledTimes(1);
expect(mockTimer).toHaveBeenLastCalledWith(expect.any(Function), 1000);
});
});

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

page.window.playAlarm = mockPlayAlarm;
input.value = "10";
const mockPlayAlarm = jest.spyOn(page.window, 'playAlarm').mockImplementation(() => {});
input.value = "1";
button.click();

expect(mockPlayAlarm).toHaveBeenCalledTimes(0);

jest.runAllTimers();

jest.advanceTimersByTime(12000);
expect(mockPlayAlarm).toHaveBeenCalledTimes(1);
mockPlayAlarm.mockRestore();
});

40 changes: 24 additions & 16 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<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>
</head>
<body>
<div class="centre">
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
<label for="alarmSet">Set time to:</label>
<input id="alarmSet" type="number" />

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Alarm clock app</title>
</head>

<body>
<div class="centre">
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
<label for="alarmSet">Set time to:</label>
<input id="alarmSet" type="number" min="Enter seconds" />

<div class="buttons">
<button id="start">Start Alarm</button>
<button id="stop" disabled>Stop Alarm</button>
</div>
<script src="alarmclock.js"></script>
</body>
</html>

<audio id="alarmAudio" src="alarmsound.mp3" preload="auto"></audio>
<p id="alarmMessage" aria-live="polite"></p>
</div>
<script src="alarmclock.js"></script>
</body>

</html>
2 changes: 1 addition & 1 deletion Sprint-3/alarmclock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
"devDependencies": {
"@testing-library/dom": "^8.19.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/user-event": "^13.5.0",
"jest": "^29.2.2",
"jest-environment-jsdom": "^29.2.2"
Expand Down
24 changes: 24 additions & 0 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,27 @@
h1 {
text-align: center;
}
button {
padding: 10px 20px;
font-size: 1rem;
margin: 5px;
border: none;
border-radius: 5px;
background-color: #007acc;
color: white;
cursor: pointer;
transition: background-color 0.3s ease;
}

button:hover:enabled {
background-color: #005fa3;
}

button:disabled {
background-color: #ccc;
cursor: not-allowed;
}
.flash {
background-color: red;
transition: background-color 0.5s ease;
}
Loading