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() {}
timerInterval = null;
function setAlarm() {
// 1. Get the input from the user
const inputField = document.getElementById("alarmSet");
let timeRemaining = parseInt(inputField.value);

// 2. Validate: If no number or 0 is entered, do nothing
if (isNaN(timeRemaining) || timeRemaining <= 0) {
return;
}

// 3. Clear any existing timer; prevent multiple alarms running at once
clearInterval(timerInterval);

// 4. Update the display immediately
updateTimeDisplay(timeRemaining);

// 5. Start the countdown
timerInterval = setInterval(() => {
timeRemaining -= 1;

updateTimeDisplay(timeRemaining);

// 6. Check if timer hit zero
if (timeRemaining <= 0) {
clearInterval(timerInterval);
playAlarm();
document.body.style.backgroundColor = "red";
}
}, 1000);
}

// mm:ss formatting
function updateTimeDisplay(totalSeconds) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;

//00:00 format
const formattedMinutes = minutes.toString().padStart(2, "0");
const formattedSeconds = seconds.toString().padStart(2, "0");

const timeString = `${formattedMinutes}:${formattedSeconds}`;
document.getElementById("timeRemaining").innerText =
`Time Remaining: ${timeString}`;
}

// DO NOT EDIT BELOW HERE

Expand Down
4 changes: 2 additions & 2 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<!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>
<title>Alarm clock app</title>
</head>
<body>
<div class="centre">
Expand Down
Loading