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
53 changes: 52 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 pass an empty or negative argument as the input?

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.

@hkavalikas I added validation to prevent empty, invalid, or negative inputs from starting the timer.

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 I click on Set alarm multiple times? 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.

@hkavalikas I now clear any existing interval before starting a new one so multiple clicks don’t create overlapping timers.

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

// Keep track of active timer
let intervalId = null;

// Turns a number of seconds into "MM:SS" format
function formatTime(seconds) {
if (isNaN(seconds) || seconds < 0) {
return "00:00";
}
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;

const mm = String(minutes).padStart(2, "0");
const ss = String(remainingSeconds).padStart(2, "0");

return mm + ":" + ss;
}

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

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

seconds = Math.floor(seconds);

if (!timeRemainingEl) {
timeRemainingEl = document.getElementById("timeRemaining");
}

if (intervalId !== null) {
clearInterval(intervalId);
}

timeRemainingEl.innerText = "Time Remaining: " + formatTime(seconds);

intervalId = setInterval(function () {
seconds = seconds - 1;

timeRemainingEl.innerText = "Time Remaining: " + formatTime(seconds);

if (seconds === 0) {
clearInterval(intervalId);
intervalId = null;
playAlarm();
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
37 changes: 20 additions & 17 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<!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>
</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" />

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
</div>
<script src="alarmclock.js"></script>
</body>
</html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<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" min="1" step="1" />

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
</div>
<script src="alarmclock.js"></script>
</body>

</html>
Loading