Skip to content

Commit af53cd6

Browse files
committed
refactor: improve code clarity and comments
1 parent 89c0209 commit af53cd6

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

Sprint-3/quote-generator/app.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/* globals quotes, pickFromArray */
22

3-
// Use a constant for the auto-play time to make it easy to change later.
3+
// Defines the auto-play interval duration (60 seconds) as a constant for maintainability.
44
const AUTO_PLAY_INTERVAL_MS = 60000;
55

6-
// Defines variables to track the state of the app.
7-
// Uses 'let' because the timer ID will change when we start/stop auto-play.
6+
// Tracks the state of the auto-play timer.
7+
// Initializes as null to indicate no active timer.
88
let autoPlayIntervalId = null;
99

10-
// DOM Elements (fetched once to keep the code efficient)
10+
// Caches DOM references to avoid repeated lookups during runtime.
1111
const quoteElement = document.getElementById("quote");
1212
const authorElement = document.getElementById("author");
1313
const newQuoteButton = document.getElementById("new-quote");
1414
const autoPlaySwitch = document.getElementById("auto-play-switch");
1515
const autoPlayStatus = document.getElementById("auto-play-status");
1616

1717
/**
18-
* Selects and displays a new random quote from the quote list.
19-
* This function changes the text on the screen.
18+
* Retrieves a random quote from the data source and updates the DOM.
19+
* Updates both the quote text and the author name.
2020
*/
2121
function displayNewQuote() {
2222
// Selects a random quote object from the global 'quotes' array.
@@ -28,69 +28,69 @@ function displayNewQuote() {
2828
}
2929

3030
/**
31-
* Starts the auto-play feature.
32-
* This sets up a timer to automatically change the quote every few seconds.
31+
* Initiates the auto-play feature.
32+
* Establishes a recurring timer to refresh the displayed quote.
3333
*/
3434
function startAutoPlay() {
35-
// Clears any existing timer first to prevent multiple timers running at once.
35+
// Clears any existing timer to prevent overlapping intervals.
3636
if (autoPlayIntervalId) {
3737
clearInterval(autoPlayIntervalId);
3838
}
3939

40-
// Shows the "auto-play: ON" status text to inform the user it is active.
40+
// Activates the visual status indicator for auto-play.
4141
autoPlayStatus.classList.add("active");
4242

43-
// Sets a repeating timer that calls 'displayNewQuote' every 60000 milliseconds (60 seconds).
43+
// Schedules the 'displayNewQuote' function to execute every 60 seconds.
4444
autoPlayIntervalId = setInterval(function () {
4545
displayNewQuote();
4646
}, AUTO_PLAY_INTERVAL_MS);
4747
}
4848

4949
/**
50-
* Stops the auto-play feature.
51-
* This cancels the timer so the quotes stop changing automatically.
50+
* Terminates the auto-play feature.
51+
* Clears the active timer and resets the state.
5252
*/
5353
function stopAutoPlay() {
54-
// Checks if a timer exists, and if so, stops it.
54+
// Verifies if a timer exists before attempting to clear it.
5555
if (autoPlayIntervalId) {
5656
clearInterval(autoPlayIntervalId);
5757
autoPlayIntervalId = null;
5858
}
5959

60-
// Hides the "auto-play: ON" status text.
60+
// Deactivates the visual status indicator.
6161
autoPlayStatus.classList.remove("active");
6262
}
6363

6464
/**
65-
* Handles the change event when the user toggles the auto-play switch.
66-
* Decides whether to start or stop auto-play based on the switch position.
65+
* Manages the state change when the auto-play switch is toggled.
66+
* Routes execution to start or stop auto-play based on the switch state.
6767
*
68-
* @param {Event} event - The event object from the click.
68+
* @param {Event} event - The change event triggered by the checkbox.
6969
*/
7070
function handleAutoPlayToggle(event) {
71-
// Checks if the toggle switch is currently 'checked' (on).
71+
// Evaluates the checked state of the toggle switch.
7272
if (event.target.checked) {
7373
startAutoPlay();
7474
} else {
7575
stopAutoPlay();
7676
}
7777
}
7878

79-
// Adds an event listener to the "New quote" button.
80-
// When clicked, it shows a new quote and resets the auto-play timer if it's running.
79+
// Attaches a click event listener to the "New quote" button.
80+
// Triggers a manual update of the quote and resets the auto-play timer if active.
8181
newQuoteButton.addEventListener("click", function () {
8282
displayNewQuote();
8383

84-
// If auto-play is turned on, restarts the timer.
85-
// This prevents the quote from changing immediately after the user manually clicks.
84+
// Restarts the auto-play timer if the feature is currently enabled.
85+
// Ensures the full interval elapses before the next automatic update.
8686
if (autoPlaySwitch.checked) {
8787
startAutoPlay();
8888
}
8989
});
9090

91-
// Adds an event listener to the auto-play toggle switch.
92-
// When changed, it triggers the handleAutoPlayToggle function.
91+
// Attaches a change event listener to the auto-play toggle switch.
92+
// Invokes the handler function whenever the user interacts with the switch.
9393
autoPlaySwitch.addEventListener("change", handleAutoPlayToggle);
9494

95-
// Displays a random quote immediately when the page loads so it isn't empty.
95+
// Initializes the view by displaying a random quote upon page load.
9696
displayNewQuote();

Sprint-3/quote-generator/style.css

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ body {
3131
max-width: 700px;
3232
text-align: center;
3333
position: relative;
34-
/* Simple crisp shadow or none based on flat design of example, keeping slight shadow for visibility */
34+
/* Applies a simple crisp shadow for visibility, maintaining the flat design aesthetic */
3535
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
3636
}
3737

3838
h1 {
39-
display: none; /* The example image doesn't show a visible title inside the card */
39+
display: none; /* Hides the title as it is not visible in the reference design */
4040
}
4141

4242
#quote {
@@ -45,39 +45,39 @@ h1 {
4545
color: var(--text-color);
4646
margin-bottom: 1.5rem;
4747
position: relative;
48-
padding-left: 3rem; /* Space for the quote icon */
48+
padding-left: 3rem; /* Reserves space for the decorative quote icon */
4949
text-align: left;
5050
}
5151

5252
#quote::before {
53-
content: '“'; /* Large quote mark using curved quotes */
53+
content: '“'; /* Renders a large curved quote mark */
5454
font-size: 6rem;
5555
color: var(--primary-color);
5656
position: absolute;
5757
top: -1.5rem;
5858
left: -1rem;
5959
line-height: 1;
60-
font-family: sans-serif; /* Quote marks often look better in sans-serif or specific fonts */
61-
opacity: 1; /* Solid colour in example */
60+
font-family: sans-serif; /* Uses sans-serif for the quote mark for better visual appeal */
61+
opacity: 1; /* Maintains solid opacity matching the design */
6262
}
6363

6464
#author {
6565
font-size: 1.1rem;
6666
color: var(--text-color);
6767
margin-bottom: 2.5rem;
6868
text-align: right;
69-
font-style: italic; /* Authors are often italicised in this style */
69+
font-style: italic; /* Applies italics to distinguish the author name */
7070
}
7171

72-
/* Add dash before author if not present in JS, but here we style assuming content */
72+
/* Appends a dash before the author name if not dynamically added by JS */
7373
#author::before {
7474
content: '- ';
7575
}
7676

7777
.controls {
7878
display: flex;
7979
flex-direction: column;
80-
align-items: flex-end; /* Button aligned to right in some designs, or center. Example shows right or center. Let's align right to match typical 'next' flows or keep center if safer. Example looks right-aligned for button. */
80+
align-items: flex-end; /* Aligns controls to the right to match typical progression flows */
8181
}
8282

8383
button {
@@ -96,12 +96,12 @@ button:hover {
9696
opacity: 0.9;
9797
}
9898

99-
/* Auto-play Switch - adjusting to fit the new theme */
99+
/* Auto-play Switch - adjusts layout to fit the theme */
100100
.auto-play-container {
101101
display: flex;
102102
align-items: center;
103103
gap: 1rem;
104-
margin-top: 1rem; /* Separate from main button */
104+
margin-top: 1rem; /* Separates the switch from the main action button */
105105
align-self: flex-end;
106106
}
107107

@@ -132,9 +132,9 @@ button:hover {
132132
left: 0;
133133
right: 0;
134134
bottom: 0;
135-
background-color: #e4e4e4; /* Light grey for off */
135+
background-color: #e4e4e4; /* Sets light grey background for the 'off' state */
136136
transition: .4s;
137-
border-radius: 0; /* Square/Rectangular look or keep rounded? Let's keep rounded for standard UI toggle */
137+
border-radius: 0; /* Maintains square/rectangular shape for a standard toggle look */
138138
}
139139

140140
.slider:before {

0 commit comments

Comments
 (0)