11
2- // inspirational quotes stored as mutliple strings within an array
2+ //Object Array to hold quotes, sources, citaitons and years
33var quotes = [
4- {
4+ {
55quote : "Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind." ,
66source : "Dr. Seuss"
77} ,
@@ -161,44 +161,50 @@ var quotes = [
161161quote : "We are what we repeatedly do. Excellence, then, is not an act, but a habit." ,
162162source : "Aristotle"
163163}
164-
165164] ;
166165
167- //Function to select a random quote object from the quotes array & return the randomly selected quote value
166+ //Function to randomly select a quote value and return a random quote object from the quotes array
168167function getRandomQuote ( ) {
169- var randomQuote = Math . floor ( Math . random ( ) * ( quotes . length ) ) ;
170- return randomQuote
171- }
168+ var randomNumber = Math . floor ( Math . random ( ) * ( quotes . length ) ) ;
169+ var randomQuote = quotes [ randomNumber ] ;
170+ return randomQuote ;
171+ }
172172
173173//Function to select random rgb color value
174174function getRandomColor ( ) {
175- var randomColor ;
176175var red = Math . floor ( Math . random ( ) * 256 ) ;
177176var green = Math . floor ( Math . random ( ) * 256 ) ;
178177var blue = Math . floor ( Math . random ( ) * 256 ) ;
179- randomColor = 'rgb(' + red + ',' + green + ',' + blue + ')' ;
178+ var randomColor = 'rgb(' + red + ',' + green + ',' + blue + ')' ;
180179return randomColor ;
181180}
182181
183182//Function to call the getRandomQuote function and stores the returned quote object in a variable
184183//Constructs a string containing the different properties of the quote object
185184function printQuote ( ) {
185+ var quotes = getRandomQuote ( ) ;
186186var quoteContainer = document . getElementById ( "quote-box" ) ;
187- var selectQuote = getRandomQuote ( ) ;
188- var quoteString = '<p class="quote">' + quotes [ selectQuote ] . quote + '</p><p class="source">' + quotes [ selectQuote ] . source ;
189- if ( quotes [ selectQuote ] . citation ) { quoteString += '<span class="citation">' + quotes [ selectQuote ] . citation + '</span>' ; }
190- if ( quotes [ selectQuote ] . year ) { quoteString += '<span class="year">' + quotes [ selectQuote ] . year + '</span></p>' ; }
187+ var quoteString = `<p class="quote">${ quotes . quote } </p><p class="source">${ quotes . source } ` ;
188+ if ( quotes . citation ) { quoteString += `<span class="citation">${ quotes . citation } </span>` }
189+ if ( quotes . year ) { quoteString += `<span class="year">${ quotes . year } </span></p>` }
191190else { quoteString += '</p>' } ;
192191quoteContainer . innerHTML = quoteString ;
193192
194193//assigns random color value to document background color
195194document . body . style . backgroundColor = getRandomColor ( ) ;
196- }
195+ }
196+
197+ //Quote automatically refreshes every 15 seconds
198+ window . setInterval ( function ( ) {
199+ printQuote ( ) ;
200+ } , 15000 ) ;
197201
198202//Event listener on LoadQuote button to generate new quote
199203document . getElementById ( "loadQuote" ) . addEventListener ( "click" , printQuote , false ) ;
200204
201205
202206
203207
204-
208+
209+
210+
0 commit comments