Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
30 changes: 29 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
function setAlarm() {}
let countdownInterval;

function setAlarm() {
const inputElement = document.getElementById("alarmSet");
const timeDisplay = document.getElementById("timeRemaining");

let timeRemaining = parseInt(inputElement.value, 10) || 0;

clearInterval(countdownInterval);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice that the old interval is cleared when the alarm is set again

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.

Thanks


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

updateDisplay();

countdownInterval = setInterval(() => {
timeRemaining--;

if (timeRemaining <= 0) {
clearInterval(countdownInterval);
playAlarm();
Comment on lines +25 to +26
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What does the countdown show when the alarm plays?

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.

Because updateDisplay() was inside the else block, the screen would freeze at 00:01 while the alarm played, never actually showing 00:00.

} else {
updateDisplay();
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
6 changes: 3 additions & 3 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<!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>
<label for="alarmSet">Set time to:</label>
<input id="alarmSet" type="number" />
<input id="alarmSet" type="number" min="0" />

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