Skip to content

London | May-2025 | Ikenna Agulobi | Data Groups |Sprint-3 Alarm Clock #686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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: 60 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Copy link

Choose a reason for hiding this comment

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

Hi Ikenna, code looks good and it works as intended. For a better user experience, it would be nicer if the background colour change back to white once Stop alarm is clicked. Can you implement this code? If you want to complete the task from here, it's totally fine too. Let me know!

Copy link
Author

Choose a reason for hiding this comment

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

Hi @day-lee, I've implemented the change request. Could you take a look when you are available. Thank you!

Copy link

Choose a reason for hiding this comment

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

Looks good! :)

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your review and feedback!

Original file line number Diff line number Diff line change
@@ -1,4 +1,62 @@
function setAlarm() {}
let timer; //to store the setInterval and use it to clearInterval.
let timerPaused; //to check if timer is currently paused
let secondsLeft = 0; //to keep track of how many seconds left in the countdown

//this function is to start the alarm countdown
function setAlarm() {
//stores user input
let userInput = document.querySelector("#alarmSet");
secondsLeft = parseInt(userInput.value); //converts user input to a num

if (isNaN(secondsLeft) || secondsLeft <= 0) {
alert("Your input is not valid!");
return; // this stops execution if input is invalid
}

updateTheDisplay(secondsLeft); // this immediately displays the initial time on the screen
//setinterval() starts a timer that runs every 1000ms(1 second)
timer = setInterval(() => {
if (!timerPaused) {
secondsLeft--; //if timer is not pause, decrease the timer by 1
}

updateTheDisplay(secondsLeft);//this changes the timer countdown each second

if (secondsLeft <= 0) {
clearInterval(timer); //stops the timer from running
playAlarm();
backGroundColor("#ADD8E6");
userInput.value = ""; //this clears the input field
}
}, 1000);
}

//function for bg color when alarm goes off
function backGroundColor(color) {
document.body.style.backgroundColor = color;
}

// function for updating the display
function updateTheDisplay(seconds) {
const minutesDisplayed = Math.floor(seconds / 60); //get full minutes
const secondsDisplayed = seconds % 60; //get full seconds
// Format minutes and seconds as "mm:ss"
let minutesAndSeconds = `${String(minutesDisplayed).padStart(2,"0")}:${String(secondsDisplayed).padStart(2, "0")}`;
const timeLeft = document.querySelector("#timeRemaining");
timeLeft.innerText = `Time Remaining: ${minutesAndSeconds}`;
}

//function for adding pause and resume
function pauseTheCountDown() {
document.getElementById("pause").addEventListener("click", () => {
timerPaused = !timerPaused; // this is to toggle btw the pause state
const btn = document.getElementById("pause");
btn.textContent = timerPaused ? "Resume" : "Pause";
});
}
pauseTheCountDown();// Run the pause setup function so that the btn can work



// DO NOT EDIT BELOW HERE

Expand All @@ -11,6 +69,7 @@ function setup() {

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
backGroundColor("white");
});
}

Expand Down
4 changes: 2 additions & 2 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
<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" />

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