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
@@ -1,4 +1,65 @@
function setAlarm() {}
const display = document.querySelector("#timeRemaining");
const totalSeconds = document.querySelector("#alarmSet");
const setButton = document.querySelector("#set");
const stopButton = document.querySelector("#stop");
let conuntdownInterval;

// time management function
function formatTime(totalSeconds) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;

const paddedMinutes = String(minutes).padStart(2, "0");
const paddedSeconds = String(seconds).padStart(2, "0");
return `Time Remaining: ${paddedMinutes}:${paddedSeconds}`;
}

// to clear every change made with conditions
function clearEverything() {
display.style.color = "black";
setButton.disabled = false;
document.body.style.background = "";
}
Comment on lines +19 to +23

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What about calling it "reset..."?

Could also consider resetting the display time to "00:00" here.


// button event handler
setButton.addEventListener("click", () => {
let timeleft = Number(totalSeconds.value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some unusual input values can make your app behave abnormally. Can you add code to sanitise them?

setButton.disabled = true;

// time 0

if (timeleft <= 0) {
alert("please enter a number greater than 0!");
return;
}
Comment on lines +28 to +35

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it be better to disable the button only when a countdown is successfully set?


display.textContent = formatTime(timeleft);

conuntdownInterval = setInterval(() => {
if (timeleft == 0) {
clearInterval(conuntdownInterval);
document.body.style.backgroundColor = "grey";
playAlarm();
}

if (timeleft <= 10) {
display.style.color = "red";
}
Comment on lines +46 to +48

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What does this 10 represent? Numbers other than 0 and 1 are generally considered magic numbers, so it is good practice to replace them with named constants.


if (timeleft >= 0) {
display.textContent = formatTime(timeleft);
timeleft--;
}
}, 1000);
Comment on lines +39 to +54

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When a user clicks the "Set" button with 1 second as input, the user will only see "00:00" 2 seconds later. Is the extra 1 second delay at the beginning by design?

});

//stop button clicked
stopButton.addEventListener("click", () => {
clearInterval(conuntdownInterval);
clearEverything();
pauseAlarm();
});

// 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