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

function setAlarm() {
const inputEl = document.getElementById("alarmSet");
const timeRemainingEl = document.getElementById("timeRemaining");

let totalSeconds = parseInt(inputEl.value, 10);

// If input is invalid or empty, default to 0
if (isNaN(totalSeconds) || totalSeconds <= 0) {
if (timerInterval) clearInterval(timerInterval); // Stop any running timer
timeRemainingEl.innerText = "Please enter a valid time (greater than 0)"; // Inform the user
return;
}
// Clear any existing timer if the button is clicked again
if (timerInterval) {
clearInterval(timerInterval);
}
// Function to render the formatted time (MM:SS) to the DOM
function updateDisplay() {
Comment thread
TTiamiyu marked this conversation as resolved.
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;

const formattedMinutes = String(minutes).padStart(2, "0");
const formattedSeconds = String(seconds).padStart(2, "0");

timeRemainingEl.innerText = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
}
// 1. Immediately set heading on button click
updateDisplay();

// 2. Start the countdown timer
timerInterval = setInterval(() => {
totalSeconds -= 1;

if (totalSeconds >= 0) {
updateDisplay();
}

if (totalSeconds === 0) {
playAlarm();
clearInterval(timerInterval);
}
}, 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>
Loading