Skip to content

Commit 00423a7

Browse files
committed
refactor: apply beginner-friendly, self-documenting style with contextual comments
1 parent 484ea23 commit 00423a7

File tree

1 file changed

+36
-31
lines changed
  • Sprint-3/quote-generator

1 file changed

+36
-31
lines changed

Sprint-3/quote-generator/app.js

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

3-
// Constants for configuration
4-
// Sets the time interval for the auto-play feature in milliseconds.
5-
const AUTO_PLAY_INTERVAL_MS = 5000; // 5 seconds for testing/demo
3+
// Use a constant for the auto-play time to make it easy to change later.
4+
const AUTO_PLAY_INTERVAL_MS = 5000;
65

7-
/**
8-
* Variables to track the auto-play interval state.
9-
*/
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.
108
let autoPlayIntervalId = null;
119

12-
// DOM Elements
13-
const quoteElement = document.querySelector("#quote");
14-
const authorElement = document.querySelector("#author");
15-
const newQuoteButton = document.querySelector("#new-quote");
16-
const autoPlaySwitch = document.querySelector("#auto-play-switch");
17-
const autoPlayStatus = document.querySelector("#auto-play-status");
10+
// DOM Elements (fetched once to keep the code efficient)
11+
const quoteElement = document.getElementById("quote");
12+
const authorElement = document.getElementById("author");
13+
const newQuoteButton = document.getElementById("new-quote");
14+
const autoPlaySwitch = document.getElementById("auto-play-switch");
15+
const autoPlayStatus = document.getElementById("auto-play-status");
1816

1917
/**
20-
* Updates the displayed quote and author on the screen.
21-
* Uses the global pickFromArray function and quotes array.
18+
* Selects and displays a new random quote from the quote list.
19+
* This function changes the text on the screen.
2220
*/
2321
function displayNewQuote() {
22+
// Selects a random quote object from the global 'quotes' array.
2423
const randomQuote = pickFromArray(quotes);
24+
25+
// Updates the HTML elements with the new quote text and author name.
2526
quoteElement.innerText = randomQuote.quote;
2627
authorElement.innerText = randomQuote.author;
2728
}
2829

2930
/**
30-
* Starts the auto-play functionality.
31-
* Sets an interval to update the quote and shows the status indicator.
31+
* Starts the auto-play feature.
32+
* This sets up a timer to automatically change the quote every few seconds.
3233
*/
3334
function startAutoPlay() {
34-
// Clear any existing interval just in case
35+
// Clears any existing timer first to prevent multiple timers running at once.
3536
if (autoPlayIntervalId) {
3637
clearInterval(autoPlayIntervalId);
3738
}
3839

39-
// Show status
40+
// Shows the "auto-play: ON" status text to inform the user it is active.
4041
autoPlayStatus.classList.add("active");
4142

42-
// Set interval
43+
// Sets a repeating timer that calls 'displayNewQuote' every 5000 milliseconds (5 seconds).
4344
autoPlayIntervalId = setInterval(function () {
4445
displayNewQuote();
4546
}, AUTO_PLAY_INTERVAL_MS);
4647
}
4748

4849
/**
49-
* Stops the auto-play functionality.
50-
* Clears the interval and hides the status indicator.
50+
* Stops the auto-play feature.
51+
* This cancels the timer so the quotes stop changing automatically.
5152
*/
5253
function stopAutoPlay() {
54+
// Checks if a timer exists, and if so, stops it.
5355
if (autoPlayIntervalId) {
5456
clearInterval(autoPlayIntervalId);
5557
autoPlayIntervalId = null;
5658
}
5759

58-
// Hide status
60+
// Hides the "auto-play: ON" status text.
5961
autoPlayStatus.classList.remove("active");
6062
}
6163

6264
/**
63-
* Toggles auto-play based on the checkbox state.
64-
* @param event - The change event from the checkbox.
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.
67+
*
68+
* @param {Event} event - The event object from the click.
6569
*/
6670
function handleAutoPlayToggle(event) {
71+
// Checks if the toggle switch is currently 'checked' (on).
6772
if (event.target.checked) {
6873
startAutoPlay();
6974
} else {
7075
stopAutoPlay();
7176
}
7277
}
7378

74-
// Event Listeners
75-
76-
// Handle "New Quote" button click
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.
7781
newQuoteButton.addEventListener("click", function () {
7882
displayNewQuote();
7983

80-
// If auto-play is on, reset the timer so it doesn't change immediately after manual click
84+
// If auto-play is turned on, restarts the timer.
85+
// This prevents the quote from changing immediately after the user manually clicks.
8186
if (autoPlaySwitch.checked) {
8287
startAutoPlay();
8388
}
8489
});
8590

86-
// Handle Auto-play switch toggle
91+
// Adds an event listener to the auto-play toggle switch.
92+
// When changed, it triggers the handleAutoPlayToggle function.
8793
autoPlaySwitch.addEventListener("change", handleAutoPlayToggle);
8894

89-
// Initial Load
90-
// Display a random quote when the app starts
95+
// Displays a random quote immediately when the page loads so it isn't empty.
9196
displayNewQuote();

0 commit comments

Comments
 (0)