Skip to content

Commit e629a85

Browse files
committed
storing time in one variable ans implment required chanes.
1 parent adf628a commit e629a85

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ const userInput = document.querySelector("#alarmSet");
66
const timeRemaining = document.querySelector("#timeRemaining");
77
// get access to the "stop" button
88
const stop = document.querySelector("#stop");
9-
let seconds = 0;
10-
let minutes = 0;
9+
let totalSeconds = 0;
1110

1211
function main() {
1312
// add an event listener to the "input" element that will call the counterUpdate function when clicked on or when the user types in the input field.
@@ -22,24 +21,25 @@ function counterUpdate() {
2221
const userInputValue = userInput.value;
2322
// check if the input is equal to or greater than 0
2423
if (userInputValue >= 0) {
25-
minutes = Math.floor(userInputValue / 60);
26-
seconds = userInputValue % 60;
27-
// if it is, update the TimeRemaining element to show the real time remaining in minutes and seconds.
28-
timeRemaining.innerText = `Time Remaining: ${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
24+
totalSeconds = userInputValue;
25+
displyUpdate();
2926
}
3027
}
3128

29+
// Update the TimeRemaining element to show the real time remaining in minutes and seconds.
30+
function displyUpdate() {
31+
const minutes = Math.floor(totalSeconds / 60);
32+
const seconds = totalSeconds % 60;
33+
timeRemaining.innerText = `Time Remaining: ${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
34+
}
35+
3236
// define the function that will count down the time remaining every second and update the remaining time on the page
3337
function setAlarm() {
3438
// use setInterval to call a function every second that will decrease the time remaining by 1 second.
3539
const interval = setInterval(() => {
36-
if (seconds > 0) {
37-
seconds--;
38-
timeRemaining.innerText = `Time Remaining: ${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
39-
} else if (minutes > 0) {
40-
minutes--;
41-
seconds = 59;
42-
timeRemaining.innerText = `Time Remaining: ${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
40+
if (totalSeconds > 0) {
41+
totalSeconds--;
42+
displyUpdate();
4343
} else {
4444
clearInterval(interval);
4545
userInput.value = "";

0 commit comments

Comments
 (0)