@@ -487,51 +487,74 @@ const quotes = [
487487
488488// call pickFromArray with the quotes array to check you get a random quote
489489
490- // Function to pick a random quote
491- function pickFromArray ( ) {
490+ // Picks a random quote object from the quotes array
491+ function pickFromArray ( quotes ) {
492+ const randomIndex = Math . floor ( Math . random ( ) * quotes . length ) ;
493+ return quotes [ randomIndex ] ;
494+ }
495+
496+ // Renders a quote object into the DOM
497+ function renderQuote ( quote ) {
492498 const quoteEl = document . getElementById ( "quote" ) ;
493499 const authorEl = document . getElementById ( "author" ) ;
494- const randomIndex = Math . floor ( Math . random ( ) * quotes . length ) ;
495500
496- quoteEl . textContent = `" ${ quotes [ randomIndex ] . quote } "` ;
497- authorEl . textContent = `-- ${ quotes [ randomIndex ] . author } --` ;
498- return randomIndex ;
501+ if ( ! quoteEl || ! authorEl || ! quote ) return ;
502+
503+ quoteEl . textContent = quote . quote ;
504+ authorEl . textContent = quote . author ;
499505}
500506
501- // Setup function for browser only
507+ // Sets up the application, event listeners, and initial state
502508function setupQuoteApp ( ) {
503509 const button = document . getElementById ( "new-quote" ) ;
504510 const autoplayCheckbox = document . getElementById ( "autoplay" ) ;
505511 const autoplayStatus = document . getElementById ( "autoplay-status" ) ;
506512
507- // Don't call pickFromArray here for Jest test
508- if ( typeof window !== "undefined" && window . document ) {
509- pickFromArray ( ) ;
513+ if ( ! button || ! autoplayCheckbox || ! autoplayStatus ) return ;
514+
515+ let autoplayInterval = null ;
510516
511- button . addEventListener ( "click" , ( ) => pickFromArray ( ) ) ;
517+ // Show first quote on page load
518+ renderQuote ( pickFromArray ( quotes ) ) ;
512519
513- let autoplayInterval = null ;
514- autoplayCheckbox . addEventListener ( "change" , ( ) => {
515- if ( autoplayCheckbox . checked ) {
516- autoplayInterval = setInterval ( pickFromArray , 5000 ) ;
517- autoplayStatus . textContent = "Auto-play: ON" ;
518- autoplayStatus . style . color = "#4CAF50" ;
519- } else {
520- clearInterval ( autoplayInterval ) ;
521- autoplayInterval = null ;
522- autoplayStatus . textContent = "Auto-play: OFF" ;
523- autoplayStatus . style . color = "brown" ;
524- }
525- } ) ;
520+ // Handles generating and rendering a new quote
521+ function handleNewQuote ( ) {
522+ renderQuote ( pickFromArray ( quotes ) ) ;
526523 }
524+
525+ // Button click generates a new quote
526+ button . addEventListener ( "click" , handleNewQuote ) ;
527+
528+ // Toggle autoplay feature
529+ autoplayCheckbox . addEventListener ( "change" , ( ) => {
530+ if ( autoplayCheckbox . checked ) {
531+ // Start auto-changing quotes every 5 seconds
532+ autoplayInterval = setInterval ( handleNewQuote , 5000 ) ;
533+ autoplayStatus . textContent = "ON" ;
534+
535+ // Apply ON styling via CSS class
536+ autoplayStatus . classList . add ( "autoplay-on" ) ;
537+ autoplayStatus . classList . remove ( "autoplay-off" ) ;
538+ } else {
539+ // Stop auto-changing quotes
540+ clearInterval ( autoplayInterval ) ;
541+ autoplayInterval = null ;
542+
543+ autoplayStatus . textContent = "OFF" ;
544+
545+ // Apply OFF styling via CSS class
546+ autoplayStatus . classList . add ( "autoplay-off" ) ;
547+ autoplayStatus . classList . remove ( "autoplay-on" ) ;
548+ }
549+ } ) ;
527550}
528551
529- // Only run setup in browser environment
552+ // Initialize app only when DOM is fully loaded ( browser environment)
530553if ( typeof window !== "undefined" ) {
531554 window . addEventListener ( "DOMContentLoaded" , setupQuoteApp ) ;
532555}
533556
534- // Export function for Jest tests
557+ // Export function for Jest testing environment
535558if ( typeof module !== "undefined" ) {
536559 module . exports = pickFromArray ;
537560}
0 commit comments