www.webdeveloper.com
Results 1 to 2 of 2

Thread: return a random amount of quotes

  1. #1
    Join Date
    Jul 2007
    Posts
    76

    return a random amount of quotes

    hi,

    just wondering if someone could assist me modify this script to display a random amount of quotes?

    i.e. sometimes it might return and display 2 quotes, then 1 quote, then maybe 3 quotes?

    rather than it is now, 1 quote per time.


    Code:
    <script type="text/javascript">
    <!--
    //I. Array of banner that will randomly appear
    
    var randomquote = new Array ( );
    randomquote[0] = '<em>"Nothing is more fairly distributed than common sense: no one thinks he needs more of it than he already has."</em><br/><br/><span class="alignright">-- Rene Descartes</span>';
    randomquote[1] = '<em>"Our virtues and our failings are inseparable, like force and matter. When they separate, man is no more."</em><br/><br/><span class="alignright">-- Nikola Tesla</span>';
    randomquote[2] = '<em>"Better to illuminate than merely to shine, to deliver to others contemplated truths than merely to contemplate."</em><br/><br/><span class="alignright">-- Thomas Aquinas</span>';
    
    //II. function to generate number from 0 to n
    
    function randomzero (n)
    {
      return ( Math.floor ( Math.random ( )*0.9999999999999999* (n + 1)) );
    }
    
    //III. assign any random number from 0 to 2 to x.
    
    x = randomzero(2);
    
    //IV. display the text 
    
    document.write(randomquote[x]);
    //-->
    
    </script>
    thanks!!!

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    I changed the x variable to an array and the rest of the script to reflect the change. Note that Math.random never return exactly 1.

    Code:
    <script type="text/javascript">
    //<![CDATA[ it's better to wrap scripts in CDATA than in a HTML comment
    
    //I. Array of banner that will randomly appear
    
    var randomquote = new Array();
    randomquote[0] = '<em>"Nothing is more fairly distributed than common sense: no one thinks he needs more of it than he already has."</em><br/><br/><span class="alignright">-- Rene Descartes</span>';
    randomquote[1] = '<em>"Our virtues and our failings are inseparable, like force and matter. When they separate, man is no more."</em><br/><br/><span class="alignright">-- Nikola Tesla</span>';
    randomquote[2] = '<em>"Better to illuminate than merely to shine, to deliver to others contemplated truths than merely to contemplate."</em><br/><br/><span class="alignright">-- Thomas Aquinas</span>';
    
    //II. function to generate number from 0 to n
    
    function randomzero(n)
    {
      // Math.random() returns a number that is >= 0 and < 1 (but never 1!)
      return Math.floor( Math.random() * (n+1) );
    }
    
    //III. get between 1 and 3 indices
    
    var amount = randomzero(2) + 1;
    
    var indices = [];
    while (amount > 0)
    {
      var i = randomzero( randomquote.length-1 );
      // check if the index has already been picked so we don't get duplicates
      if (indices.indexOf(i) == -1)
      {
        indices.push(i);
        amount--;
      }
    }
    
    //IV. display the quotes
    
    for (var i = 0; i < indices.length; i++)
    {
      document.write( randomquote[ indices[i] ] + '<br/><br/>' );
    }
    
    //]]> end of CDATA
    </script>
    Last edited by ReFreezed; 01-08-2013 at 10:18 AM.
    New to web development or in need of a good reference? Check out the Mozilla Developer Network or W3Schools.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles