Skip to content
Open
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
56 changes: 56 additions & 0 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,59 @@ function pauseAlarm() {
}

window.onload = setup;

const timeRemainingElement = document.querySelector("#timeRemaining");
const alarmSetElement = document.querySelector("#alarmSet");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you know you are using IDs, can you think of something better tha n querySelector?

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.

Is it getelementID()?

const setButtonElement = document.querySelector("#set");
const stopButtonElement = document.querySelector("#stop");
const bodyElement = document.querySelector("body");
const alarmSoundElement = document.querySelector("#alarmSound");

let remainingTime;
let intervalID;
function triggerAlarm() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A bit more spacing between functions / improved formatting would help readability.

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.

Actually, I used prettier tool according to the style guide for formatting. Should I provide extra spaces in between functions?

bodyElement.style.backgroundColor = "yellow";
alarmSoundElement.play();
}
function setAlarm() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Have you properly handled the input validation here?

@Dipa-Sarker Dipa-Sarker Jul 30, 2026

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.

No, I didn't handle input validation. Now I have tried with empty input and input time < 0.

clearInterval(intervalID);
const alarmTime = alarmSetElement.value;
if (alarmTime === "") {
return;
}
remainingTime = Number(alarmTime);
if (remainingTime < 0) {
return;
}
const showTimeMinutes = Math.floor(remainingTime / 60)
.toString()
.padStart(2, "0");
const showTimeSeconds = (remainingTime % 60).toString().padStart(2, "0");
timeRemainingElement.innerText = showTimeMinutes + ":" + showTimeSeconds;
if (remainingTime === 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.

Do you properly finish the timer countdown after it hits zero?

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.

Sorry, I am unclear about your question. But I think I finished the countdown after hitting zero at here:

function countDown() {
if (remainingTime <= 0) {
clearInterval(intervalID);
triggerAlarm();
return;
}

Which means:

  • Timer reaches 00:00
  • Interval stops
  • No negative counting
  • Alarm starts
  • returns from the code so no extra code runs afterward.

triggerAlarm();
} else {
intervalID = setInterval(countDown, 1000);
}
}
setButtonElement.addEventListener("click", setAlarm);
function countDown() {
if (remainingTime <= 0) {
clearInterval(intervalID);
triggerAlarm();
return;
}
remainingTime -= 1;
const remainingMinutes = Math.floor(remainingTime / 60)
.toString()
.padStart(2, "0");
const remainingSeconds = (remainingTime % 60).toString().padStart(2, "0");
timeRemainingElement.innerText = remainingMinutes + ":" + remainingSeconds;
}
function stopAlarm() {
clearInterval(intervalID);
alarmSoundElement.pause();
alarmSoundElement.currentTime = 0;
bodyElement.style.backgroundColor = "";
}
stopButtonElement.addEventListener("click", stopAlarm);
3 changes: 2 additions & 1 deletion Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
<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" />
<audio id="alarmSound" src="alarmsound.mp3" loop></audio>

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