Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple random array

    I'm just trying to get this code to randomly select a restaurant to eat at when the button is clicked. I'm having trouble doing the random part. here is the code I got:

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Random Restaraunt Selector</title>
    <script type="text/javascript">
    //PTE = Place to eat
    <!--
    function getpte(ptenum) {
    return Math.floor(Math.random()* ptenum);
    }
    //-->
    </script>
    </head>
    <body>
    <h2>Random Restaraunt Selector</h2>
    <script type="text/javascript">

    <!--
    var pte = new Array("Jimmy John's","Momo's","Subway", "Pita Pit", "Fresh Stirfry", "Cici's", "Quiznos", "Hungry Howies", "Decent Pizza", "Stevi B's", "Wendy's", "Super Perros");

    for (x in pte)
    {
    document.write(pte[x] + "<br />");
    }
    //-->

    getpte = ptenum(12); // 12 values, 0 - 11
    document.write("The Winner is... ");
    document.write(ptenum);

    </script>
    <form>
    <p></p>
    <p><input type="button" value="Click me!" onclick="getpte(this.form.pte.value)" /></p>
    </form>
    </body>
    </html>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb Try this ...

    Note differences from your script ...
    PHP Code:
    <html>
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <
    title>Random Restaraunt Selector</title>
    <
    script type="text/javascript">
    //PTE = Place to eat

     
    function getpte(ptenum) { return Math.floor(Math.random()* ptenum); }

     var 
    pte = new Array("Jimmy John's","Momo's","Subway""Pita Pit""Fresh Stirfry""Cici's""Quiznos",
                         
    "Hungry Howies""Decent Pizza""Stevi B's""Wendy's""Super Perros");
     function 
    ShowList() { for (x in pte) { document.write(pte[x] + "<br />"); } }
     
     function 
    PickWinner() { 
       var 
    ptenum getpte(pte.length);  // 12 values, 0 - 11
       
    var str "The Winner is ... " pte[ptenum];
       
    document.getElementById('winner').innerHTML str;
     }
    </script>
    </head>
    <body>
    <h2>Random Restaraunt Selector</h2>
    <script type="text/javascript"> ShowList(); </script>
    <p></p>
    <p><input type="button" value="Click me!" onclick="PickWinner();" /></p>
    <p>
    <div id="winner" style="font-size:50px;color:red"></div>
    </body>
    </html> 

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks man, I appreciate it.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts
    You're most welcome.
    If you understand the differences then I'm glad I was able to help.
    Good Luck!


  •  

    Posting Permissions

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