forked from khushi-purwar/WebDev-ProjectKart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
157 lines (117 loc) · 4.66 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var quotes = [
{
quote: "No Grit, No Pearl.",
source: "Anonymous",
tags: ["Grit", "Determination", "Success"]
},
{
quote: "You miss 100% of the shots you don't take.",
source: "Wayne Gretzky",
tags: ["Just Do It", "Sports"]
},
{
quote: "Fall seven times, stand up eight.",
source: "Japanese Proverb",
tags: ["Determination", "Grit"]
},
{
quote: "I have not failed. I've just found 10,000 ways that won't work.",
source: "Thomas A. Edison",
tags: ["Perserverance", "Innovation"]
},
{
quote: "Done is better than perfect.",
source: "Sheryl Sandberg",
tags: ["Just Do It", "Tech"]
},
{
quote: "The only thing we have to fear is fear itself.",
source: "Franklin D. Roosevelt",
citation: "First Inaugural Address",
year: 1933,
tags: ["Hope", "Politics"]
}
];
// CSS Color Names
// Compiled by @bobspace.
//
// A javascript array containing all of the color names listed in the CSS Spec.
// The full list can be found here: http://www.w3schools.com/cssref/css_colornames.asp
// Use it as you please, 'cuz you can't, like, own a color, man.
// I deleted colors that didn't provide sufficient contrast or seemed particularly grating.
var CSS_COLOR_NAMES = ["Blue", "BlueViolet", "Brown", "BurlyWood", "CadetBlue", "Chocolate", "Coral", "CornflowerBlue", "Crimson", "DarkBlue", "DarkCyan", "DarkGoldenRod", "DarkGray", "DarkGrey", "DarkGreen", "DarkKhaki", "DarkMagenta", "DarkOliveGreen", "Darkorange", "DarkOrchid", "DarkRed", "DarkSalmon", "DarkSeaGreen", "DarkSlateBlue", "DarkSlateGray", "DarkSlateGrey", "DarkTurquoise", "DarkViolet", "DeepPink", "DeepSkyBlue", "FireBrick", "ForestGreen", "Fuchsia", "Green", "HotPink", "IndianRed", "Indigo", "Magenta", "Maroon", "MediumAquaMarine", "MediumBlue", "MediumOrchid", "MediumPurple", "MediumSeaGreen", "MediumSlateBlue", "MediumTurquoise", "MediumVioletRed", "MidnightBlue", "Olive", "OliveDrab", "Orange", "OrangeRed", "Orchid", "Pink", "Plum", "PowderBlue", "Purple", "RosyBrown", "RoyalBlue", "SaddleBrown", "Salmon", "SlateBlue", "SlateGray", "SlateGrey", "SpringGreen", "SteelBlue", "Teal", "Thistle", "Tomato", "Turquoise", "Violet"];
var quoteIndex = 0;
var colorIndex = 0;
var quotesUsed = [];
var intervalID;
// const keyword is not supported in IE
var NUMBER_OF_QUOTES = 6;
var CHANGE_INTERVAL = 10000;
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
// Returns a random integer between min (included) and max (included)
// Using Math.round() will give you a non-uniform distribution!
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function emptyQuotesArray() {
quotesUsed = [];
console.log("Emptied queue; Start fresh.");
}
function allQuotesUsed() {
return (quotesUsed.length === NUMBER_OF_QUOTES);
}
function getRandomQuote() {
do {
quoteIndex = getRandomIntInclusive(0, 5);
} while (quotesUsed.includes(quoteIndex));
quotesUsed.push(quoteIndex);
logQuoteToConsole(quoteIndex);
if (allQuotesUsed()) {
emptyQuotesArray();
}
return quotes[quoteIndex];
}
function getRandomColor() {
colorIndex = getRandomIntInclusive(0, CSS_COLOR_NAMES.length - 1);
return CSS_COLOR_NAMES[colorIndex];
}
function logQuoteToConsole(quoteIndex) {
console.log(quoteIndex, ': ', quotes[quoteIndex].quote.slice(0, 20));
}
function formatQuote(quote) {
var formattedQuote =
'<p class="quote">' + quote.quote + '</p>' +
'<p class="source">' + quote.source;
// http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property
if (typeof quote.citation !== "undefined") {
formattedQuote += '<span class="citation">' + quote.citation + '</span>';
}
if (typeof quote.year !== "undefined") {
formattedQuote += '<span class="year">' + quote.year + '</span>';
}
if (typeof quote.tags !== "undefined") {
formattedQuote += '<p class="tags">' + quote.tags.join(', ') + '</p>';
}
formattedQuote += '</p>';
return formattedQuote;
}
function changeBackground() {
document.getElementById("bgcolor").style.backgroundColor = getRandomColor();
}
function resetTimer() {
if (intervalID) {
window.clearInterval(intervalID);
intervalID = setInterval(printQuote, CHANGE_INTERVAL);
}
}
function printQuote() {
resetTimer();
document.getElementById('quote-box').innerHTML = formatQuote(getRandomQuote());
changeBackground();
}
printQuote();
// set the interval to change the quote to the defined interval
intervalID = window.setInterval(printQuote, CHANGE_INTERVAL);
document.getElementById('loadQuote').addEventListener("click", printQuote, false);