Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
47f91e5
Fix: update document title to 'Alarm clock app'
alexandru-pocovnicu Mar 16, 2026
41537a4
Fix: implement setAlarm function to display time remaining
alexandru-pocovnicu Mar 16, 2026
3ab9799
extract the seconds and minutes
alexandru-pocovnicu Mar 16, 2026
8a4ba11
Refactor: enhance setAlarm function to improve countdown display and …
alexandru-pocovnicu Mar 19, 2026
ca62e47
change background to blue when stop alarm is selected
alexandru-pocovnicu Mar 20, 2026
08fbcbd
alternate background color
alexandru-pocovnicu Mar 20, 2026
aa8df3b
deleted code for remove class
alexandru-pocovnicu Mar 20, 2026
950cd4a
added return when the input is 0, so timer doesn't start
alexandru-pocovnicu Apr 2, 2026
4aafd50
added function to clean the initial state
alexandru-pocovnicu Apr 3, 2026
dc665e5
add checks for valid input
alexandru-pocovnicu Apr 3, 2026
b81719d
create pause button,
alexandru-pocovnicu Apr 4, 2026
8354926
refactor CSS for consistency and clarity
alexandru-pocovnicu Apr 4, 2026
063a0fa
addresse reviewer's changes
alexandru-pocovnicu Apr 6, 2026
5d64c66
delete redundant code
alexandru-pocovnicu Apr 6, 2026
c84e2a9
created function to update display
alexandru-pocovnicu Apr 6, 2026
894313e
move countdown logic to finishCountDown()
alexandru-pocovnicu Apr 6, 2026
3b6db49
remove unused pause button styles and empty unpause-alarm class
alexandru-pocovnicu Apr 6, 2026
11d7474
capitalize unpause button text in pauseCountDown function
alexandru-pocovnicu Apr 6, 2026
5209ca9
remove class
alexandru-pocovnicu Apr 6, 2026
4251c18
remove id attribute from pause button in setAlarm function
alexandru-pocovnicu Apr 6, 2026
a65ca54
set pause button text and ensure event listener is added in setAlarm …
alexandru-pocovnicu Apr 6, 2026
6d09014
use display time logic instead of hard coding it
alexandru-pocovnicu Apr 9, 2026
6799a86
refactor cleanInitialState to use updateDisplayedTime for resetting d…
alexandru-pocovnicu Apr 9, 2026
fbb165f
initialize isPaused in cleanInitialState function
alexandru-pocovnicu Apr 9, 2026
74c4c23
remove event listener for pause button before adding a new one in set…
alexandru-pocovnicu Apr 9, 2026
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
89 changes: 88 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,91 @@
function setAlarm() {}
let intervalId;
let isPaused;
let remainingSeconds = 0;
function setAlarm() {

const pauseButton = document.getElementById("pause-button");
pauseButton.textContent = "Pause";
pauseButton.removeEventListener("click",pauseCountDown)
pauseButton.addEventListener("click", pauseCountDown);
Comment on lines +6 to +9
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.

You can reset the isPaused flag and the "Pause" button state in cleanInitialState(), and add event listener only once on page load.

Currently, the same event listener will be added to the button multiple times (once per function call).

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.

but if i move the listener inside cleanInitialState(), which is called everytime setAlarm() is called, isn't that the same thing, how about instead, i remove the listener inside setAlarm just before adding it?
thank you

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.

I mean, perform this in cleanInitialState()

    const pauseButton = document.getElementById("pause-button");
    pauseButton.textContent = "Pause";

and perform this on page load

window.addEventListener("load", function() {
    const pauseButton = document.getElementById("pause-button");
    pauseButton.addEventListener("click", pauseCountDown);
});


const alarmSetEl = document.getElementById("alarmSet");
const timeRemainingEl = document.getElementById("timeRemaining");
let totalSeconds = +alarmSetEl.value;
remainingSeconds = totalSeconds;

if (
totalSeconds <= 0 ||
isNaN(totalSeconds) ||
!Number.isInteger(totalSeconds)
) {
alarmSetEl.value = "";
return;
}
cleanInitialState();

function updateCountDown() {
remainingSeconds = totalSeconds;
updateDisplayedTime(remainingSeconds);

if (totalSeconds <= 0) {
finishCountDown();
return;
}

totalSeconds -= 1;
}

updateCountDown();
intervalId = setInterval(updateCountDown, 1000);
}

function pauseCountDown(e) {
if (isPaused === false) {
clearInterval(intervalId);
e.target.textContent = "Unpause";
isPaused = true;
} else {
e.target.textContent = "Pause";
isPaused = false;
intervalId = setInterval(() => {
if (remainingSeconds <= 0) {
clearInterval(intervalId);
return;
}
remainingSeconds -= 1;
updateDisplayedTime(remainingSeconds);

if (remainingSeconds <= 0) {
finishCountDown();
}
}, 1000);
Comment on lines +50 to +61
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.

It seems the code here is very similar to those perform in setAlarm(). Can you try reusing the code you have in setAlarm()?

}
}

function cleanInitialState() {
isPaused = false;
clearInterval(intervalId);
document.body.classList.remove("finish-countdown");
updateDisplayedTime(0)
document.getElementById("alarmSet").value = null;
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.

.value is a string, so better to write document.getElementById("alarmSet").value = "";.

pauseAlarm();
}

function updateDisplayedTime(totalSeconds) {
let seconds = totalSeconds % 60;
let minutes = (totalSeconds - seconds) / 60;

let paddedSeconds = seconds.toString().padStart(2, "0");
let paddedMinutes = minutes.toString().padStart(2, "0");
document.getElementById("timeRemaining").innerHTML =
`Time Remaining: ${paddedMinutes}:${paddedSeconds}`;
}

function finishCountDown() {
document.body.classList.add("finish-countdown");
playAlarm();
clearInterval(intervalId);
}

// DO NOT EDIT BELOW HERE

Expand Down
3 changes: 2 additions & 1 deletion Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<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 All @@ -14,6 +14,7 @@ <h1 id="timeRemaining">Time Remaining: 00:00</h1>

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
<button id="pause-button">Pause</button>
</div>
<script src="alarmclock.js"></script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion Sprint-3/alarmclock/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ First off, once you've branched off `main`, then update the title element in `in
You will need to write your implementation in `alarmclock.js`.

## How the clock should work

When you click the `Set Alarm` button the counter at the top of the screen should change to the number you entered in the `input` field. For example, if the `input` field says `10` then the title should say `Time Remaining: 00:10`.

Every one second the title should count down by one.
Expand Down
15 changes: 15 additions & 0 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@
h1 {
text-align: center;
}

.finish-countdown {
animation: police 0.5s infinite;
}

@keyframes police {
from {
background-color: blue;
}
to {
background-color: red;
}
}


Loading