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
47 changes: 31 additions & 16 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
function setAlarm() {}
let alarmIntervalId = null;

// DO NOT EDIT BELOW HERE
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?


var audio = new Audio("alarmsound.mp3");
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}`;
}

function setup() {
document.getElementById("set").addEventListener("click", () => {
setAlarm();
});
heading.innerText = formatMMSS(seconds);

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
});
}
if (alarmIntervalId) {
clearInterval(alarmIntervalId);
alarmIntervalId = null;
}

function playAlarm() {
audio.play();
}
if (seconds === 0) {
playAlarm();
return;
}

function pauseAlarm() {
audio.pause();
alarmIntervalId = setInterval(() => {
seconds -= 1;
if (seconds > 0) {
heading.innerText = formatMMSS(seconds);
} else {
heading.innerText = formatMMSS(0);
clearInterval(alarmIntervalId);
alarmIntervalId = null;
playAlarm();
}
}, 1000);
}

window.onload = setup;
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