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
52 changes: 51 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,54 @@
function setAlarm() {}
let intervalId = null;
let totalSeconds = 0;
const input = document.getElementById("alarmSet");
const timeRemaining = document.getElementById("timeRemaining");
const message = document.getElementById("message");

function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
return `${String(minutes).padStart(2, "0")}:${String(secs).padStart(2, "0")}`;
}
function updateDisplay() {
timeRemaining.textContent = `Time Remaining: ${formatTime(totalSeconds)}`;
}

function resetAlarm() {
clearInterval(intervalId);
intervalId = null;
audio.pause();
audio.currentTime = 0;
}
function getTime() {
const time = Number(input.value);
if (!(Number.isInteger(time) && time >= 1)) {
message.textContent = "Enter a valid number";
return null;
}
return time;
}

function setAlarm() {
const time = getTime();
if (time === null) {
return;
}

resetAlarm();
totalSeconds = time;
updateDisplay();

intervalId = setInterval(() => {
totalSeconds--;
updateDisplay();

if (totalSeconds <= 0) {
clearInterval(intervalId);
intervalId = null;
playAlarm();
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
5 changes: 3 additions & 2 deletions Sprint-3/alarmclock/index.html
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
<!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">
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
<h2 id="message"></h2>
<label for="alarmSet">Set time to:</label>
<input id="alarmSet" type="number" />

Expand Down
Loading