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
105 changes: 104 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,106 @@
function setAlarm() {}
// This is the countdown number that will go down every second
let countdownTimer;

function startFlashing() {
document.body.classList.add("flash-background");
}

function stopFlashing() {
document.body.classList.remove("flash-background");
}
Comment thread
cjyuan marked this conversation as resolved.

function setAlarm() {
const rawInput = document.getElementById("alarmSet").value.trim();

// Reject empty input
if (rawInput === "") {
alert("Please enter a time.");
return;
}

// 2. Parse input → returns either a number of seconds or null
const totalSeconds = parseInput(rawInput);

// 3. Reject invalid parsed values
if (totalSeconds === null || totalSeconds <= 0) {
alert("Please enter a valid positive time.");
return;
}

// 4. Reject extremely large values
if (totalSeconds > 36000) {
alert("Please enter a time less than 10 hours.");
return;
}

stopFlashing();
pauseAlarm();

// 5. If we reach here → input is valid
startCountdown(totalSeconds);
}

function parseInput(rawInput) {
if (rawInput.includes(":")) {
const [minsStr, secsStr] = rawInput.split(":");

const mins = Number(minsStr);
const secs = Number(secsStr);

if (
Number.isNaN(mins) ||
Number.isNaN(secs) ||
Comment on lines +47 to +52

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What type of numbers should they be? What if the user enter 1.1:2.2 or 1.2345?

mins < 0 ||
secs < 0 ||
secs >= 60
) {
return null;
}

return mins * 60 + secs;
}

const secs = Number(rawInput);
return Number.isNaN(secs) ? null : secs;
}

function startCountdown(totalSeconds) {
let timeLeft = totalSeconds;

if (countdownTimer) {
clearInterval(countdownTimer);
}

updateDisplay(timeLeft);

countdownTimer = setInterval(() => {
timeLeft--;
updateDisplay(timeLeft);

if (timeLeft <= 0) {
clearInterval(countdownTimer);
playAlarm();
startFlashing();
}
}, 1000);
}

// A simple helper to turn seconds into MM:SS format
function formatTime(seconds) {
const mins = Math.floor(seconds / 60);
const secs = seconds % 60;

// Make sure both numbers always have two digits
const paddedMins = String(mins).padStart(2, "0");
const paddedSecs = String(secs).padStart(2, "0");

return `${paddedMins}:${paddedSecs}`;
}

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

// DO NOT EDIT BELOW HERE

Expand All @@ -11,6 +113,7 @@ function setup() {

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
stopFlashing(); // stop flashing if running

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

An alternative would be to add another event listener that performs only `stop flashing".

});
}

Expand Down
36 changes: 19 additions & 17 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<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>
</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" />

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
</div>
<script src="alarmclock.js"></script>
</body>
</html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Alarm Clock</title>

<body>
<div class="centre">
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
<label for="alarmSet">Set time to:</label>
<input id="alarmSet" type="number" />

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
</div>
<script src="alarmclock.js"></script>
</body>

</html>
24 changes: 24 additions & 0 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,27 @@
h1 {
text-align: center;
}

/* flashing effect for the alarm app */
@keyframes colorRipple {
0% {
background-color: #ffffff;
} /* White */
25% {
background-color: #ff6b6b;
} /* Light Coral / Soft Red */
50% {
background-color: #feca57;
} /* Mango / Warm Yellow-Orange */
75% {
background-color: #48dbfb;
} /* Sky Blue / Light Aqua */
100% {
background-color: #ffffff;
} /* White */
}

/* apply the flashing effect to the body when the alarm is triggered */
.flash-background {
animation: colorRipple 0.8s infinite;
}
Loading