Skip to content

Commit 5f76b9a

Browse files
Merge pull request #1 from RichardJamesWard/Update
Update
2 parents f0332be + 76913d7 commit 5f76b9a

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

js/script.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
// inspirational quotes stored as mutliple strings within an array
2+
//Object Array to hold quotes, sources, citaitons and years
33
var quotes = [
4-
{
4+
{
55
quote: "Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.",
66
source: "Dr. Seuss"
77
},
@@ -161,44 +161,50 @@ var quotes = [
161161
quote: "We are what we repeatedly do. Excellence, then, is not an act, but a habit.",
162162
source: "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
168167
function 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
174174
function getRandomColor () {
175-
var randomColor;
176175
var red = Math.floor(Math.random() * 256 );
177176
var green = Math.floor(Math.random() * 256 );
178177
var blue = Math.floor(Math.random() * 256 );
179-
randomColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
178+
var randomColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
180179
return 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
185184
function printQuote () {
185+
var quotes = getRandomQuote ();
186186
var 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>`}
191190
else {quoteString += '</p>'};
192191
quoteContainer.innerHTML = quoteString;
193192

194193
//assigns random color value to document background color
195194
document.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
199203
document.getElementById("loadQuote").addEventListener("click", printQuote, false);
200204

201205

202206

203207

204-
208+
209+
210+

0 commit comments

Comments
 (0)