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

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

var audio = new Audio("alarmsound.mp3");
function parseInputTime(value) {
if (!/^\d{2}:\d{2}$/.test(value)) return null;
const [m, s] = value.split(":").map(Number);
if (s > 59) return null;
return m * 60 + s;
}

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

function incrementTime(amount) {
totalSeconds += amount;
if (totalSeconds < 0) totalSeconds = 0;
updateDisplay();
}

function setAlarm() {
const input = document.getElementById("alarmSet");
const parsed = parseInputTime(input.value);
if (parsed === null) {
alert("Use MM:SS format");
return;
}

function setup() {
document.getElementById("set").addEventListener("click", () => {
setAlarm();
});
totalSeconds = parsed;
updateDisplay();

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
});
if (timer) clearInterval(timer);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you think of anything else that should also be reset before starting a new countdown?


timer = setInterval(() => {
totalSeconds--;
updateDisplay();
if (totalSeconds <= 0) {
clearInterval(timer);
timer = null;
playAlarm();
}
}, 1000);
}

function stopTimer() {
if (timer) {
clearInterval(timer);
timer = null;
}
totalSeconds = 0;
updateDisplay();
pauseAlarm();
}

// attach to window so HTML buttons can call them
window.incrementTime = incrementTime;
window.setAlarm = setAlarm;
window.stopTimer = stopTimer;
Comment on lines +63 to +65
Copy link
Contributor

@cjyuan cjyuan Dec 1, 2025

Choose a reason for hiding this comment

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

Are you aware of some of the pros and cons between these two approaches of invoking a callback function?
(1) The approach you are using
(2) Assign an ID to the element and attach a callback function to the element as

document.getElementbyId('id-of-the-element').addEventListener('event-name', callback);

window.playAlarm = playAlarm;
window.pauseAlarm = pauseAlarm;

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

function playAlarm() {
audio.play();
}
Expand All @@ -22,4 +77,5 @@ function pauseAlarm() {
audio.pause();
}

window.onload = setup;


92 changes: 84 additions & 8 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,94 @@
<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>
<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" />
<div
style="
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
gap: 15px;
"
>
<label for="alarmSet" style="font-size: 44px; font-weight: bold"
>Set time to:</label
>

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
<input
id="alarmSet"
type="text"
maxlength="5"
placeholder="00:00"
value="00:00"
style="
font-size: 64px;
text-align: center;
width: 200px;
padding: 15px;
border-radius: 10px;
border: 3px solid #0904b0;
"
/>

<div style="display: flex; gap: 10px">
<button
onclick="incrementTime(5)"
style="
font-size: 20px;
padding: 10px 15px;
border-radius: 6px;
border: 2px solid #444;
"
>
▲ +5
</button>
<button
onclick="incrementTime(-5)"
style="
font-size: 20px;
padding: 10px 15px;
border-radius: 6px;
border: 2px solid #444;
"
>
▼ -5
</button>
</div>

<button
onclick="setAlarm()"
style="
font-size: 30px;
padding: 10px 20px;
border-radius: 8px;
border: 3px solid green;
"
>
SET
</button>

<h1 id="timeRemaining" style="font-size: 36px; font-weight: 900">
Time Remaining:<br />00:00
</h1>

<button
onclick="stopTimer()"
style="
font-size: 40px;
padding: 15px 30px;
border-radius: 10px;
border: 4px solid red;
"
>
STOP
</button>
</div>

<script src="alarmclock.js"></script>
</body>
</html>
66 changes: 57 additions & 9 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
@@ -1,15 +1,63 @@
.centre {
position: fixed;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
.container {
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
gap: 15px;
}

#alarmSet {
margin: 20px;
.time-label {
font-size: 44px;
font-weight: bold;
}

h1 {
.time-input {
font-size: 64px;
text-align: center;
width: 200px;
padding: 15px;
border-radius: 10px;
border: 3px solid #0904b0;
}

.increment-buttons {
display: flex;
gap: 10px;
}

.increment-buttons button {
font-size: 20px;
padding: 10px 15px;
border-radius: 6px;
border: 2px solid #444;
}

.set-button {
font-size: 30px;
padding: 10px 20px;
border-radius: 8px;
border: 3px solid green;
}

.time-remaining {
font-size: 36px;
font-weight: 900;
text-align: center;
}

.stop-button {
font-size: 40px;
padding: 15px 30px;
border-radius: 10px;
border: 4px solid red;
}

.stop-large-button {
font-size: 40px;
padding: 15px 30px;
border-radius: 10px;
border: 4px solid red;
}