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

window.onload = setup;

const timeRemainingElement = document.getElementById("timeRemaining");
const alarmSetElement = document.getElementById("alarmSet");
const setButtonElement = document.getElementById("set");
const stopButtonElement = document.getElementById("stop");
const bodyElement = document.querySelector("body");
const alarmSoundElement = document.getElementById("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?

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 clear line between functions is a good idea

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 placed one line inside functions.

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

function setAlarm() {
Comment thread
LonMcGregor marked this conversation as resolved.
clearInterval(intervalID);
const alarmTime = alarmSetElement.value;
if (alarmTime === "") {
return;
} //input validation (for emptry input)
remainingTime = Number(alarmTime);
if (remainingTime < 0) {
return;
} //input validation(for -ve number input)
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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I see, I was confused that you have a triggerAlarm in the setAlarm function. if you also have it in the countDown function, is it necessary to have it in the setAlarm function also?

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.

if (remainingTime <= 0) {
clearInterval(intervalID);
triggerAlarm();
return;
}
this condition serves different purpose inside setAlarm() function. I commented out every possible confusing condition in my code. Please let me know if there any confusion again.

triggerAlarm(); //valid input, when input = 0 then immediately triggers alarm
} else {
intervalID = setInterval(countDown, 1000);
} //valid input, when input = 10 then countdown starts
}
setButtonElement.addEventListener("click", setAlarm);
function countDown() {
if (remainingTime <= 0) {
clearInterval(intervalID);
triggerAlarm();
return;
} /*to avoid any chance of -ve counting after countdown reached 0 from 10.
when countdown = 0 alarm triggers immediately and returns from the code */

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