Skip to content

Commit 4967200

Browse files
committed
Add displayNewQuote function to show random quotes and update HTML
1 parent a7b1d2a commit 4967200

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

‎Sprint-3/quote-generator/quotes.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,25 @@ const quotes = [
491491
];
492492

493493
// call pickFromArray with the quotes array to check you get a random quote
494+
495+
// Function to select a random quote and update the HTML elements
496+
function displayNewQuote() {
497+
const quoteP = document.querySelector("#quote");
498+
const authorP = document.querySelector("#author");
499+
500+
// Get a random quote object from the quotes array
501+
const randomQuote = pickFromArray(quotes);
502+
503+
// Update the DOM element contents
504+
quoteP.textContent = randomQuote.quote;
505+
authorP.textContent = randomQuote.author;
506+
}
507+
508+
// 1. Display a random quote when the page initially loads
509+
displayNewQuote();
510+
511+
// 2. Add a click event listener to the "New quote" button
512+
const newQuoteBtn = document.querySelector("#new-quote");
513+
if (newQuoteBtn) {
514+
newQuoteBtn.addEventListener("click", displayNewQuote);
515+
}

0 commit comments

Comments
 (0)