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
65 changes: 64 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
function setAlarm() {}
let countdownTimerId = null;

const finishedBackgroundColor = "#007BA7";
const normalBackgroundColor = "";

function setAlarm() {
// Resets background each time user sets a new alarm (optional but nice)
document.body.style.backgroundColor = normalBackgroundColor;

// Stops any previous countdown + stops any currently playing alarm sound
if (countdownTimerId !== null) clearInterval(countdownTimerId);
pauseAlarm();

const input = document.getElementById("alarmSet");

// only allows positive integers
const seconds = Number(input.value);
const isValid = Number.isInteger(seconds) && seconds > 0;

if (!isValid) {
input.setCustomValidity("Please enter a whole number greater than 0.");
input.reportValidity();
return;
}
input.setCustomValidity("");

// Reads the number of seconds the user typed
let totalSecRemaining =
parseInt(document.getElementById("alarmSet").value, 10) || 0;

// Element that displays: "Time Remaining: MM:SS"
const timeRemainingTitle = document.getElementById("timeRemaining");

// Updates the title based on totalSecRemaining
const renderTimeRemaining = () => {
const minutesRemaining = String(
Math.floor(totalSecRemaining / 60)
).padStart(2, "0");
const secondsRemaining = String(totalSecRemaining % 60).padStart(2, "0");
timeRemainingTitle.textContent = `Time Remaining: ${minutesRemaining}:${secondsRemaining}`;
};

// Shows the starting value after clicking "Set Alarm"
renderTimeRemaining();

// Runs every second countdown tick
countdownTimerId = setInterval(() => {
// If time is up, stops ticking and starts the alarm sound
if (totalSecRemaining <= 0) {
clearInterval(countdownTimerId);
countdownTimerId = null;

// Change background when alarm finishes
document.body.style.backgroundColor = finishedBackgroundColor;

playAlarm();
return;
}

// Otherwise subtracts 1 second and update the display
totalSecRemaining -= 1;
renderTimeRemaining();
}, 1000);
}

// DO NOT EDIT BELOW HERE

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

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

</html>