I am writing code for a quote randomizer. I am currently having problems because I do not want the quotes to repeat and I do not know where my mistake is.
The quotes are generating but some of them are repeating. There is also an option for someone to add a new quote to the list.
Here is the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Matthew Daly's JavaScript Page</title>
<script type="text/javascript">
<!--
//-->
</script>
<link rel="stylesheet" type="text/css" href="csjsmain.css" />
</head>
<body>
<div class="header">
<h1>Matthew Daly's Lab 2 Subbmission!</h1>
</div>
<div class="body">
<span id="quoteText"> </span><br/><br/>
<button id="getQuote">Get a Quote</button>
<button id="newQuote" onclick="addQuote()">Add Your Own</button>
<script type="text/javascript">
var arrQuotes = new Array();
arrQuotes[0] = "Programming is an art form that fights back.";
arrQuotes[1] = "One time I thought I was a variable. But then I changed my mind. ";
arrQuotes[2] = "Of course I want some Cheesy Poofs.";
arrQuotes[3] = "No one suspects the butterfly!!!!";
arrQuotes[4] = "what does it mean when a young, lithe, and sassy girl tells you that she wants to 'have your baby'? i have no babies..";
arrQuotes[5] = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
arrQuotes[6] = "Chicken meat tastes better cooked.";
arrQuotes[7] = "Is that really supposed to go there?";
arrQuotes[8] = "Any sufficiently technologically advanced music is indistinguishable from line noise.";
var intQuoteIndex = -1;
// assign the event handler function
document.getElementById("getQuote").onclick = nextQuote;
function random()
{
var qDex = Math.floor(Math.random() * arrQuotes.length);
return qDex;
}
// function to generate a new random quote.
function nextQuote()
{
// keep track of the current quote index.
// use a while loop to generate a new index
// that is different from the current one
// assign the new quote[index] text to the
// quote display element
// Assign the new quote content to the innerHTML
var intTemp = random();
while(intTemp != intQuoteIndex)
{
document.getElementById("quoteText").innerHTML = arrQuotes[intTemp];
intQuoteIndex = intTemp;
}
}
function addQuote()
{
// Adding an additional element to an array:
var strQuote = window.prompt("Please eneter your quote:","Quote");
arrQuotes[arrQuotes.length] = strQuote;
}
</script>
</div>
<div class="footer">
<hr/>
© Matthew Daly
</div>
</body>
</html>
The quotes are generating but some of them are repeating. There is also an option for someone to add a new quote to the list.
Here is the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Matthew Daly's JavaScript Page</title>
<script type="text/javascript">
<!--
//-->
</script>
<link rel="stylesheet" type="text/css" href="csjsmain.css" />
</head>
<body>
<div class="header">
<h1>Matthew Daly's Lab 2 Subbmission!</h1>
</div>
<div class="body">
<span id="quoteText"> </span><br/><br/>
<button id="getQuote">Get a Quote</button>
<button id="newQuote" onclick="addQuote()">Add Your Own</button>
<script type="text/javascript">
var arrQuotes = new Array();
arrQuotes[0] = "Programming is an art form that fights back.";
arrQuotes[1] = "One time I thought I was a variable. But then I changed my mind. ";
arrQuotes[2] = "Of course I want some Cheesy Poofs.";
arrQuotes[3] = "No one suspects the butterfly!!!!";
arrQuotes[4] = "what does it mean when a young, lithe, and sassy girl tells you that she wants to 'have your baby'? i have no babies..";
arrQuotes[5] = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
arrQuotes[6] = "Chicken meat tastes better cooked.";
arrQuotes[7] = "Is that really supposed to go there?";
arrQuotes[8] = "Any sufficiently technologically advanced music is indistinguishable from line noise.";
var intQuoteIndex = -1;
// assign the event handler function
document.getElementById("getQuote").onclick = nextQuote;
function random()
{
var qDex = Math.floor(Math.random() * arrQuotes.length);
return qDex;
}
// function to generate a new random quote.
function nextQuote()
{
// keep track of the current quote index.
// use a while loop to generate a new index
// that is different from the current one
// assign the new quote[index] text to the
// quote display element
// Assign the new quote content to the innerHTML
var intTemp = random();
while(intTemp != intQuoteIndex)
{
document.getElementById("quoteText").innerHTML = arrQuotes[intTemp];
intQuoteIndex = intTemp;
}
}
function addQuote()
{
// Adding an additional element to an array:
var strQuote = window.prompt("Please eneter your quote:","Quote");
arrQuotes[arrQuotes.length] = strQuote;
}
</script>
</div>
<div class="footer">
<hr/>
© Matthew Daly
</div>
</body>
</html>