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
69 changes: 53 additions & 16 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,62 @@
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);

var audio = new Audio("alarmsound.mp3");
// Reject invalid input instead of silently treating it as 0
if (!Number.isFinite(seconds) || seconds < 0) {
heading.innerText = "Please enter a valid number of seconds";
return;
}

function setup() {
document.getElementById("set").addEventListener("click", () => {
setAlarm();
});
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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks.

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

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
});
}
heading.innerText = formatMMSS(seconds);

if (alarmIntervalId) {
clearInterval(alarmIntervalId);
alarmIntervalId = null;
}

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

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

function pauseAlarm() {
audio.pause();
function stopAlarm() {
const heading = document.getElementById("timeRemaining");

if (alarmIntervalId) {
clearInterval(alarmIntervalId);
alarmIntervalId = null;
}

heading.innerText = "Time Remaining: 00:00";
}

window.onload = setup;
document.getElementById("set").addEventListener("click", setAlarm);
document.getElementById("stop").addEventListener("click", stopAlarm);


9 changes: 4 additions & 5 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
<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">
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
<label for="alarmSet">Set time to:</label>
<input id="alarmSet" type="number" />

<input type="number" id="alarmSet" min="0" placeholder="Seconds" />
<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