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
61 changes: 60 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,63 @@
function setAlarm() {}
let countdownInterval;

function setAlarm() {
const input = document.getElementById("alarmSet");
let seconds = Number(input.value);

// Validation: Check if the input is a valid positive number
if (isNaN(seconds) || seconds <= 0) {
alert("Please enter a valid positive number for seconds.");
return; // Stop the function if the input is invalid
}

updateAlarmDisplay(seconds);

// Change the background color to green when the timer starts
document.body.style.backgroundColor = "green";

// Clear any existing interval
clearInterval(countdownInterval);

countdownInterval = window.setInterval(() => {
seconds--;
if (seconds <= 0) {
clearInterval(countdownInterval);
updateAlarmDisplay(0);
// Change the background color to red when alarm hits zero
document.body.style.backgroundColor = "red";
playAlarm(); // Assuming you have a playAlarm function defined elsewhere
} else {
updateAlarmDisplay(seconds);
}
}, 1000);
}

function updateAlarmDisplay(seconds) {
const display = document.getElementById("timeRemaining");
const minutes = Math.floor(seconds / 60)
.toString()
.padStart(2, "0");
const secs = (seconds % 60).toString().padStart(2, "0");
display.textContent = `Time Remaining: ${minutes}:${secs}`;
}

function stopAlarm() {
// Clear the countdown interval
clearInterval(countdownInterval);
countdownInterval = null;

// Reset the background color and the displayed time
document.body.style.backgroundColor = "";
updateAlarmDisplay(0); // Reset to 00:00
}

// Add event listeners to buttons
document.getElementById("set").addEventListener("click", setAlarm);
document.getElementById("stop").addEventListener("click", stopAlarm);

window.setAlarm = setAlarm;
window.updateAlarmDisplay = updateAlarmDisplay;
window.stopAlarm = stopAlarm;

// DO NOT EDIT BELOW HERE

Expand Down
2 changes: 1 addition & 1 deletion Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<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>alarmclock</title>
</head>
<body>
<div class="centre">
Expand Down