Skip to content

Commit c1c30f3

Browse files
Implement displayRandomQuote function and initialize quote display on page load
1 parent 6c81be1 commit c1c30f3

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Sprint-3/quote-generator/quotes.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
// Function to fetch a random quote and update the DOM
2+
function displayRandomQuote() {
3+
// 1. Get a random quote object using the provided helper function
4+
const randomQuote = pickFromArray(quotes);
5+
6+
// 2. Select the HTML elements where the quote and author will go
7+
const quoteElement = document.querySelector("#quote");
8+
const authorElement = document.querySelector("#author");
9+
10+
// 3. Update the text of those elements
11+
if (quoteElement && authorElement) {
12+
quoteElement.innerText = randomQuote.quote;
13+
authorElement.innerText = randomQuote.author;
14+
}
15+
}
16+
17+
// Ensure the DOM is fully loaded before attaching event listeners
18+
window.onload = function () {
19+
// Display an initial quote right when the page loads
20+
displayRandomQuote();
21+
22+
// Find the button and attach a click event listener
23+
const newQuoteBtn = document.querySelector("#new-quote");
24+
if (newQuoteBtn) {
25+
newQuoteBtn.addEventListener("click", displayRandomQuote);
26+
}
27+
};
28+
129
// DO NOT EDIT BELOW HERE
230

331
// pickFromArray is a function which will return one item, at

0 commit comments

Comments
 (0)