Skip to content
Closed
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
42 changes: 41 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
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 will happen if we add -5 or not add anything to the timer?

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.

I think a negative value will trigger the alarm right away (or nearly immediately) and empty input breaks the timer unless handled. so i correct my code if (isNaN(totalSeconds) || totalSeconds <= 0) {
alert("Please enter a valid positive number.");
return;
} to make sure the code is running smoothly

Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
function setAlarm() {}
let countdownInterval = null;

function setAlarm() {
const input = document.getElementById("alarmSet");
const heading = document.getElementById("timeRemaining");

let totalSeconds = parseInt(input.value);

if (isNaN(totalSeconds) || totalSeconds <= 0) {
alert("Please enter a valid positive number.");
return;
}


if (countdownInterval) clearInterval(countdownInterval);

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

updateHeading();

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

if (totalSeconds <= 0) {
totalSeconds = 0;

updateHeading();

clearInterval(countdownInterval);
playAlarm();
return;
}


updateHeading();
}, 1000);
}
Comment on lines +25 to +41
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The alarm will play before the seconds remaining are set to exactly 0, how can we improve that?

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.

I think the alarm is triggered before the UI clearly shows 00:00, so it feels early. I amend my code in order to update the UI before triggering the final action


// DO NOT EDIT BELOW HERE

Expand Down
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
Loading