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 currentTimerId = null;

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

// Sanitise input
if (!Number.isInteger(seconds) || seconds <= 0) {
alert("Please enter a positive whole number");
return;
}

// Reset before starting new countdown
if (currentTimerId !== null) {
clearInterval(currentTimerId);
}

pauseAlarm();

document.body.style.backgroundColor = "";

function updateDisplay(remainingSeconds) {
const minutes = Math.floor(remainingSeconds / 60);
const secs = remainingSeconds % 60;
const formattedTime = `${String(minutes).padStart(2, "0")}:${String(secs).padStart(2, "0")}`;
heading.textContent = `Time Remaining: ${formattedTime}`;
}

// Set initial display
updateDisplay(seconds);

// Start countdown
currentTimerId = setInterval(() => {
seconds--;
updateDisplay(seconds);

if (seconds <= 0) {
clearInterval(currentTimerId);
currentTimerId = null;
playAlarm();
document.body.style.backgroundColor = "red";
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
7 changes: 4 additions & 3 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<!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>
<link rel="icon" href="" />
<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" />
<input id="alarmSet" type="number" min="1" />

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
Expand Down