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
43 changes: 42 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
function setAlarm() {}
function setAlarm(){
const input = document.getElementById("alarmSet");
const heading = document.getElementById("timeRemaining");

let seconds = parseInt(input.value, 10);
if (!Number.isFinite(seconds) || seconds < 0) seconds = 0;

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 am not sure if the expected user behaviour for invalid inputs is to set the seconds to 0. What else could you do instead?


function formatMMSS(s) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good job putting the code for formatting into an own function. This makes it reusable and easier to change the formtting if needed

const mm = Math.floor(s / 60)
.toString()
.padStart(2, "0");
const ss = (s % 60).toString().padStart(2, "0");
return `Time Remaining: ${mm}:${ss}`;
}

heading.innerText = formatMMSS(seconds);

if (window._alarmIntervalId) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why are you adding _alarmIntervalId to the window object?

clearInterval(window._alarmIntervalId);
window._alarmIntervalId = null;
}

// If it's already zero, play immediately and don't start an interval
if (seconds === 0) {
playAlarm();
return;
}

window._alarmIntervalId = setInterval(() => {
seconds -= 1;

if (seconds > 0) {
heading.innerText = formatMMSS(seconds);
} else {
// Ensure the heading shows 00:00, stop the interval and play the alarm once
heading.innerText = formatMMSS(0);
clearInterval(window._alarmIntervalId);
window._alarmIntervalId = null;
playAlarm();
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
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,8 @@
<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>
<script defer src="alarmclock.js"></script>
</head>
<body>
<div class="centre">
Expand All @@ -15,6 +16,5 @@ <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>
</body>
</html>
Loading