Javascript - Randomizer

Discussion in 'Web Design and Development' started by Sideonecincy, Oct 4, 2007.

  1. Sideonecincy macrumors 6502

    Sep 29, 2003
    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"

    <title>Matthew Daly's JavaScript Page</title>
    <script type="text/javascript">
    <link rel="stylesheet" type="text/css" href="csjsmain.css" />

    <div class="header">
    <h1>Matthew Daly's Lab 2 Subbmission!</h1>
    <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;

    <div class="footer">
    © Matthew Daly

  2. angelwatt Moderator emeritus


    Aug 16, 2005
    Well you're using random, so of course things can repeat. That's why it's random. One solution would be to display quotes as they appear in the order they are in the array, then shuffle the quote array after it reaches the end and start from the beginning again. This still leaves a possibility that the last quote would become the first quote after shuffling, but you could also do a check for this. This should get you started. I don't have time to code it up currently.

    Also, for the code,
    while(intTemp != intQuoteIndex)
    document.getElementById("quoteText").innerHTML = arrQuotes[intTemp];
    intQuoteIndex = intTemp;
    The while loop will only ever run once through because you set those two variables equal to one another.

Share This Page