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
63 changes: 60 additions & 3 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,64 @@
function setAlarm() {}
let timer = null;
let secondsRemaining = 0;

// DO NOT EDIT BELOW HERE
function formatTime(totalSeconds) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
}

function updateDisplay(seconds) {
document.getElementById("timeRemaining").textContent =
`Time Remaining: ${formatTime(seconds)}`;
}

function setAlarm() {
if (timer) {
clearInterval(timer);
timer = null;
}

const inputValue = document.getElementById("alarmSet").value;
const seconds = Number(inputValue);

if (
inputValue === "" ||
Number.isNaN(seconds) ||
!Number.isInteger(seconds) ||
seconds <= 0
) {
document.getElementById("timeRemaining").textContent =

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I noticed we're calling document.getElementById() multiple times for the same elements. Would it make the code easier to read and maintain if those element references were retrieved once and reused throughout the script?

"Please enter a whole number greater than 0.";
return;
}

secondsRemaining = seconds;
updateDisplay(secondsRemaining);

timer = setInterval(() => {
secondsRemaining--;
updateDisplay(secondsRemaining);

if (secondsRemaining <= 0) {
clearInterval(timer);
timer = null;
playAlarm();
}
}, 1000);
}

function stopTimer() {
if (timer) {
clearInterval(timer);
timer = null;
}
}

document.addEventListener("DOMContentLoaded", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since we're already using window.onload, is the DOMContentLoaded listener still necessary for adding this event listener?

document.getElementById("stop").addEventListener("click", stopTimer);
});

// DO NOT EDIT BELOW HERE
var audio = new Audio("alarmsound.mp3");

function setup() {
Expand All @@ -22,4 +79,4 @@ function pauseAlarm() {
audio.pause();
}

window.onload = setup;
window.onload = setup;
3 changes: 3 additions & 0 deletions Sprint-3/alarmclock/alarmclock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
There are some Tests in this file that will help you work out if your code is working.
*/


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

Expand All @@ -15,6 +16,8 @@ beforeEach(async () => {

jest.useFakeTimers();



// do this so students can use element.innerText which jsdom does not implement
Object.defineProperty(page.window.HTMLElement.prototype, "innerText", {
get() {
Expand Down
2 changes: 1 addition & 1 deletion 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 Down
Loading