@@ -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