Skip to content

Commit f6b634c

Browse files
committed
quotes.js completed
1 parent c28e8d3 commit f6b634c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Sprint-3/quote-generator/quotes.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,45 @@ const quotes = [
494494
];
495495

496496
// call pickFromArray with the quotes array to check you get a random quote
497+
// Select the elements from the html
498+
const quoteDisplay = document.getElementById("quote");
499+
const authorDisplay = document.getElementById("author");
500+
const newQuoteButton = document.getElementById("new-quote");
501+
502+
// Create a function to update the content on the screen
503+
function updateQuote() {
504+
// Use the provided pickFromArray function
505+
const randomQuoteObject = pickFromArray(quotes);
506+
// Access the 'quote' property from the object and,
507+
// inject it into the HTML element
508+
quoteDisplay.innerText = randomQuoteObject.quote;
509+
// Access the 'author' property and use a template literal,
510+
// to add a dash for styling
511+
authorDisplay.innerText = `- ${randomQuoteObject.author}`;
512+
}
513+
514+
// Add event listener to the button
515+
newQuoteButton.addEventListener("click", updateQuote);
516+
// Show a random quote immediately when the page loads
517+
updateQuote();
518+
519+
// Get the new elements from the DOM
520+
const autoplayToggle = document.getElementById("autoplay-toggle");
521+
const autoplayStatus = document.getElementById("autoplay-status");
522+
523+
// Track timer
524+
let timerId = null;
525+
526+
// Event listener to the checkbox
527+
autoplayToggle.addEventListener("change", () => {
528+
if (autoplayToggle.checked) {
529+
// Switch is ON
530+
autoplayStatus.innerText = "auto-play: ON";
531+
timerId = setInterval(updateQuote, 5000);
532+
} else {
533+
// Switch is OFF
534+
autoplayStatus.innerText = "auto-play: OFF";
535+
// Stop the timer
536+
clearInterval(timerId);
537+
}
538+
});

0 commit comments

Comments
 (0)