SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru babyboy808's Avatar
    Join Date
    Nov 2004
    Location
    dublin
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display Random Quotes?

    Does anybody know how I can generate random associative array values?

    For example, I have 3 values inside my quotes array, and would like to output 1 of these every browser reload. I would like to have the first value (This is Quote number 1) display in it's own variable and the second (by Keith Donegan) too.

    Please help...

    Code:
    <script language="JavaScript">
            <!--
            
            var random_num;
            var quotes = new Array (
                  
                  new Array('This is Quote number 1' , 'by Keith Donegan') , 
                  new Array('This is Quote number 2' , 'by Aoife Moloney') , 
                  new Array('This is Quote number 3' , 'by Ted Father')
            
            )
            
            for(var i=0; i<1; i++)
            {
                random_num = (Math.round((Math.random()*8)+1))
            
                document.write(quotes[random_num]);
                
                document.write(quotes);
                
            }
            -->
            </script>

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this

    random_num = (Math.round((Math.random()*8)+1))

    should be

    random_num = Math.floor(Math.random()*quotes.length)


Bookmarks

Posting Permissions

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