Skip to content
45 changes: 40 additions & 5 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
function setAlarm() {}
let countdownId;

// moved to outer scope, takes seconds as a parameter
function updateDisplay(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
document.getElementById("timeRemaining").textContent =
`Time Remaining: ${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`;
}

// reset before starting new countdown
function resetAlarm() {
clearInterval(countdownId);
updateDisplay(0); // replaces the manual textContent line
document.body.classList.toggle("alarm-activated", false);
}
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.

This function is never called.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Apologies. Due to time constraints I gave up on the idea of making a pause button, but I didn't clean up my code accordingly


function setAlarm() {
let seconds = parseInt(document.getElementById("alarmSet").value);

if (!seconds || seconds < 1) {
alert("The number of seconds must be higher than 0 please");
return;
}

updateDisplay(seconds);
// pass seconds as argument and update immediately on click

countdownId = setInterval(() => {
seconds--;
updateDisplay(seconds);
// pass seconds as argument

if (seconds <= 0) {
clearInterval(countdownId);
playAlarm();
document.body.classList.toggle("alarm-activated", true);
}
}, 1000);
}

// DO NOT EDIT BELOW HERE
Comment thread
cjyuan marked this conversation as resolved.

Expand All @@ -18,8 +57,4 @@ function playAlarm() {
audio.play();
}

function pauseAlarm() {
audio.pause();
}

window.onload = setup;
4 changes: 2 additions & 2 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<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>
<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
7 changes: 7 additions & 0 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
.alarm-activated {
background-color: darkorange;
}

#alarmSet {
Expand All @@ -13,3 +15,8 @@
h1 {
text-align: center;
}

body {
background-color: cornflowerblue;
font-family: "Trebuchet MS", Tahoma, Verdana, Helvetica, sans-serif;
}
Loading