Skip to content
25 changes: 24 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
function setAlarm() {}
function setAlarm() {
let seconds = Number(document.getElementById("alarmSet").value);

function countdown() {
let minutes = Math.floor(seconds / 60);
let remainingSeconds = seconds % 60;

let formattedMinutes = String(minutes).padStart(2, "0");
let formattedSeconds = String(remainingSeconds).padStart(2, "0");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use const instead of let. let only for something that changes


document.getElementById("timeRemaining").textContent =
`Time Remaining: ${formattedMinutes}:${formattedSeconds}`;

if (seconds === 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

2 cases to check

  1. Click Set Alarm with the input left empty. What does Number("") give you?
  2. The input is type="number", so a user can type -5. Try it pls.

playAlarm();
return;
}

seconds--;
setTimeout(countdown, 1000);
}

countdown();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Try in the browser: set the alarm to 30, then straight away change the input to 10 and click Set Alarm again.

What do you expect to happen? What actually happens?

Think about how many countdowns are running at that point, and what each one is doing to the heading. setTimeout returns something when you call it and that might be useful.

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 for the feedback! I've made the requested changes:

Added validation for empty and non-positive input.
Added min="1" to prevent negative values through the number input.
Changed variables to const where they aren't reassigned.
Stored the setTimeout ID.
Prevented users from setting a new alarm while a countdown is already running by disabling the Set Alarm button.
Re-enabled the button once the countdown finishes.

}

// 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
@@ -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 Down
Loading