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,5 +1,49 @@
function setAlarm() {}
const input = document.getElementById("alarmSet");
const timeRemaining = document.getElementById("timeRemaining");
const message = document.getElementById("message");

let intervalId = null;

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

function updateTime(time) {
let minutes = Math.floor(time / 60);
let seconds = time % 60;
minutes = minutes.toString().padStart(2, "0");
seconds = seconds.toString().padStart(2, "0");
timeRemaining.textContent = `Time Remaining: ${minutes}:${seconds}`;
}

function setAlarm() {
let timeInput = getTime();
if (timeInput === null) {
return;
}
resetAlarm();
updateTime(timeInput);
intervalId = setInterval(() => {
timeInput--;
updateTime(timeInput);
if (timeInput === 0) {
playAlarm();
clearInterval(intervalId);
intervalId = null;
}
}, 1000);
}

function resetAlarm() {
clearInterval(intervalId);
intervalId = null;
updateTime(0);
}
// DO NOT EDIT BELOW HERE

var audio = new Audio("alarmsound.mp3");
Expand Down
35 changes: 18 additions & 17 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@

<!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>
<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>
<h2 id="message"></h2>
<label for="alarmSet">Set time to:</label>
<input id="alarmSet" type="number" min="1" />
<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
</div>
<script src="alarmclock.js"></script>
</body>
</html>