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
64 changes: 60 additions & 4 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
function setAlarm() {}
const display = document.querySelector("#timeRemaining");
const totalSeconds = document.querySelector("#alarmSet");
const setButton = document.querySelector("#set");
const stopButton = document.querySelector("#stop");
let countDownInterval;

function formatTime(totalSeconds) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;

const paddedMinutes = String(minutes).padStart(2, "0");
const paddedSeconds = String(seconds).padStart(2, "0");
return `Time Remaining: ${paddedMinutes}:${paddedSeconds}`;
}

function reset() {
clearInterval(countDownInterval);
display.textContent = "Time Remaining: 00:00";
display.style.color = "black";
setButton.disabled = false;
document.body.style.background = "";
}

setButton.addEventListener("click", () => {
let timeLeft = Number(totalSeconds.value);

if (timeLeft <= 0 || isNaN(timeLeft)) {
alert("please enter a number greater than zero (0)!");
return;
}
setButton.disabled = true;

const warningTime = 10; // seconds

display.textContent = formatTime(timeLeft);
countDownInterval = setInterval(() => {
display.textContent = formatTime(timeLeft);
timeLeft--;

if (timeLeft >= 0) {
display.textContent = formatTime(timeLeft);
}

if (timeLeft <= warningTime) {
display.style.color = "red";
}

if (timeLeft <= 0) {
clearInterval(countDownInterval);
document.body.style.backgroundColor = "grey";
playAlarm();
}
}, 1000);
});

stopButton.addEventListener("click", () => {
clearInterval(countDownInterval);
reset();
pauseAlarm();
});
Comment thread
cjyuan marked this conversation as resolved.

// DO NOT EDIT BELOW HERE

Expand All @@ -8,10 +68,6 @@ function setup() {
document.getElementById("set").addEventListener("click", () => {
setAlarm();
});

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
});
Comment on lines -11 to -14

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an instruction saying "DO NOT EDIT BELOW HERE" on line 63 though. :D

}

function playAlarm() {
Expand Down
6 changes: 3 additions & 3 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 All @@ -15,6 +15,6 @@ <h1 id="timeRemaining">Time Remaining: 00:00</h1>
<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
</div>
<script src="alarmclock.js"></script>
<script src="alarmclock.js" defer></script>
</body>
</html>
Loading