function returnQuoteText(bDisplayQuote) { var text = "
" + returnRandomQuote(); } return (text); } function lengthQuote() { var sum = 0; for (var i = 0; i < quote.length; i ++) { sum += quote[i].length; } return sum; } function returnQuote(num) { var sum = 0; var text = "Invalid quote number"; for (var i = 0; i < quote.length; i ++) { var index = num - sum; if (index >= 0 && index < quote[i].length) { text = quote[i][index] + " (Quote " + (i + 1) + "-" + (index + 1) + ")"; // if (i != 10 && i != 13) text = ""; } sum += quote[i].length; } return text; } function writeQuote1(num) { if (num >= 0 && num < lengthQuote()) { var quoteText = returnQuote(num); if (quoteText != "") document.write("Quote #" + (num + 1) + " of " + lengthQuote() + " total quotes:
" + quoteText); } else { document.write("Invalid quote number"); } } function writeQuote2(n1, n2) { if (n1 >= 0 && n1 < lengthQuote()) { var quoteText = returnQuote(n1); if (quoteText != "") document.write("Quote #" + (n1 + 1) + " of " + lengthQuote() + " total quotes (" + (n2 + 1) + "):
" + quoteText); } else { document.write("Invalid quote number"); } } function randomOccurrences(maxIter) { var quoteLength = lengthQuote(); var occurrences = new Array(quoteLength); for (var i = 0; i < quoteLength; i ++) { occurrences[i] = 0; } for (var i = 0; i < maxIter; i ++) { var num = parseInt(Math.random() * quoteLength); occurrences[num] ++; } var minVal = minElem(occurrences); var maxVal = maxElem(occurrences); var avgVal = average(occurrences); document.write("
On " + maxIter + " iteration" +
(maxIter == 1 ? "," : "s,"));
document.write("
quote[" + minVal + "] has the smallest number of occurrences, which is "
+ occurrences[minVal] + (occurrences[minVal] != 1 ? " times," : " time,"));
document.write("
quote[" + maxVal + "] has the largest number of occurrences, which is "
+ occurrences[maxVal] + (occurrences[maxVal] != 1 ? " times," : " time,"));
document.write("
each element has an average of " + avgVal
+ (avgVal != 1 ? " occurrences," : " occurrence,"));
for (var i = 0, lastIter = (i == quoteLength - 1);
i < quote.length;
i ++, lastIter = (i == quoteLength - 1))
{
document.write((!lastIter ? "" : " and")
+ "
quote[" + i + "] occurs " + occurrences[i]
+ (occurrences[i] != 1 ? " times" : " time")
+ (!lastIter ? "," : "."));
}
}
function allQuotes()
{
var number = 0;
var firstTime = 1;
var iStart = number;
var iEnd = lengthQuote();
for (var i = iStart, n = 0; i < iEnd; i ++, n ++)
{
if (firstTime == 0)
{
document.write("
"); } else { firstTime = 0; } writeQuote1(i); } } function returnRandomQuote() { var text = ""; var quoteText = ""; var number = parseInt(Math.random() * lengthQuote()); var firstTime = 1; var iStart = number; var iEnd = number + 1; for (var i = iStart, n = 0; i < iEnd; i ++, n ++) { if (firstTime == 0) { text += "
"; } else { firstTime = 0; } var quoteText = returnQuote(i); if (quoteText != "") text = "Quote #" + (i + 1) + " of " + lengthQuote() + " total quotes:
" + quoteText; } return text; } function randomQuote() { var number = parseInt(Math.random() * lengthQuote()); var firstTime = 1; var iStart = number; var iEnd = number + 1; for (var i = iStart, n = 0; i < iEnd; i ++, n ++) { if (firstTime == 0) { document.write("
"); } else { firstTime = 0; } writeQuote1(i); } } function showQuotes(switchVar) { switch (switchVar) { case 'a': allQuotes(); break; case 'r': randomQuote(); break; } }