www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-07-2009, 04:40 PM
    tri4thlete tri4thlete is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 3
    Help with While Loop code (Javascript)

    Hello, for class I am to create a webpage that is a Lottery. Basically an individual enters 4 numbers (0-9) in 4 different text boxes and a display textbox shows how many times it took to get that exact sequence of numbers....

    Here is the code I have created, and it only averages 5 sequences which seems really low... thanks for the help
    Code:
    <html>
    <head>
       <title>The Lotto!</title>
       <script type="text/javascript"
       src="http://dave-reed.com/book/random.js">
       </script>
       
       <script type="text/javascript">
       function Lotto()
       // Assumes: individual enters number in each box below
       // Results: Show how many drawings the page had to perform before sequence came up
       
       { 
       	   var roll1, roll2, roll3, roll4, act1, act2, act3, act4;
       	   
       	   doublecount = 0;
       	   
       	   roll1 = RandomInt(0, 9);
       	   roll2 = RandomInt(0, 9);
       	   roll3 = RandomInt(0, 9);
       	   roll4 = RandomInt(0, 9);
       	   
       	   act1 = document.getElementById("boxone").value;
       	   act2 = document.getElementById("boxtwo").value;
       	   act3 = document.getElementById("boxthree").value;
       	   act4 = document.getElementById("boxfour").value;
       	   	   
       	   while (roll1 != act1 && roll2 != act2 && roll3 != act3 && roll4 != act{
       	   	   roll1 = RandomInt(0, 9);
       	   roll2 = RandomInt(0, 9);
       	   roll3 = RandomInt(0, 9);
       	   roll4 = RandomInt(0, 9);
       	   act1 = document.getElementById("boxone").value;
       	   act2 = document.getElementById("boxtwo").value;
       	   act3 = document.getElementById("boxthree").value;
       	   act4 = document.getElementById("boxfour").value;
       	   doublecount = doublecount + 1;
       	   
       	   }
       
       	   document.getElementById("textbox").value = doublecount;
       	   
       }
       	   
       	   </script>
       	 
       
       </head>
       
       <body>
       <div style="text-align:center">
       <h1> Pick-4 Lotto </h1>
       <br />
       
       <p>This page demonstrates the futility of lotteries. </p>
       
       <p>
       Click on the button to perform LOTTO drawings until: <input type="text" id="boxone" size="6" value="0" /> <input type="text" id="boxtwo" size="6" value="0" /> <input type="text" id="boxthree" size="6" value="0" /> <input type="text" id="boxfour" size="6" value="0" />
       
       </p>
       <p>
       <input type="button" value="Click to begin drawing" onclick="Lotto();" />
       </p>
       <p>
       
       Number of picks: <input type="text" id="textbox" size="10" value="" />
       </p>
       </div>
          
       </body>
       
       </html>

    Last edited by Kor; 11-08-2009 at 03:14 AM. Reason: wrapping the code [code][/code] and removing the empty new lines
    Reply With Quote
      #2  
    Old 11-08-2009, 09:24 AM
    JMRKER JMRKER is offline
    Registered User
     
    Join Date: Dec 2005
    Location: FL
    Posts: 4,561
    With the code provided, you must be getting errors.
    Where is RandomInt() function defined?
    Reply With Quote
      #3  
    Old 11-08-2009, 09:43 AM
    tri4thlete tri4thlete is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 3
    it's defined here

    <script type="text/javascript"
    src="http://dave-reed.com/book/random.js">
    </script>
    Reply With Quote
      #4  
    Old 11-08-2009, 12:57 PM
    JMRKER JMRKER is offline
    Registered User
     
    Join Date: Dec 2005
    Location: FL
    Posts: 4,561
    Be prepared to wait a while for a match ...
    Code:
    <html>
    <head>
       <title>The Lotto!</title>
    <script type="text/javascript"  src="http://dave-reed.com/book/random.js"></script>
      
       <script type="text/javascript">
       // From: http://www.webdeveloper.com/forum/showthread.php?p=1046727#post1046727
       
       function Lotto()
       // Assumes: individual enters number in each box below
       // Results: Show how many drawings the page had to perform before sequence came up
       
       { 
       	   var roll1, roll2, roll3, roll4, act1, act2, act3, act4;
       	   doublecount = 0;
       	   
       	   roll1 = RandomInt(0, 9);
       	   roll2 = RandomInt(0, 9);
       	   roll3 = RandomInt(0, 9);
       	   roll4 = RandomInt(0, 9);
       	   
       	   act1 = document.getElementById("boxone").value;
       	   act2 = document.getElementById("boxtwo").value;
       	   act3 = document.getElementById("boxthree").value;
       	   act4 = document.getElementById("boxfour").value;
       	   	   
       	   while (!((roll1 == act1) && (roll2 == act2) && (roll3 == act3) && (roll4 == act4))) {
    //	   	   alert([doublecount,roll1,roll2,roll3,roll4].join('\n'));
       	   roll1 = RandomInt(0, 9);
       	   roll2 = RandomInt(0, 9);
       	   roll3 = RandomInt(0, 9);
       	   roll4 = RandomInt(0, 9);
       	   act1 = document.getElementById("boxone").value;
       	   act2 = document.getElementById("boxtwo").value;
       	   act3 = document.getElementById("boxthree").value;
       	   act4 = document.getElementById("boxfour").value;
       	   doublecount = doublecount + 1;
       	   
       	   }
    //	   	   alert([doublecount,roll1,roll2,roll3,roll4].join('\n'));
       
       	   document.getElementById("textbox").value = doublecount;
       	   
       }
       	   
       	   </script>
       	 
       
       </head>
       
       <body>
       <div style="text-align:center">
       <h1> Pick-4 Lotto </h1>
       <br />
       
       <p>This page demonstrates the futility of lotteries. </p>
       <p>
       Click on the button to perform LOTTO drawings until: <input type="text" id="boxone" size="6" value="0" /> <input type="text" id="boxtwo" size="6" value="0" /> <input type="text" id="boxthree" size="6" value="0" /> <input type="text" id="boxfour" size="6" value="0" />
       
       </p>
       <p>
       <input type="button" value="Click to begin drawing" onclick="Lotto();" />
       </p>
       <p>
       
       Number of picks: <input type="text" id="textbox" size="10" value="" />
       </p>
       </div>
       </body>
       </html>
    Reply With Quote
      #5  
    Old 11-08-2009, 03:05 PM
    tri4thlete tri4thlete is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 3
    Thank you very much for your help! Sorry i'm fairly new at this, but what does this mean in your code...

    alert([doublecount,roll1,roll2,roll3,roll4].join('\n'));
    Reply With Quote
      #6  
    Old 11-08-2009, 03:13 PM
    JMRKER JMRKER is offline
    Registered User
     
    Join Date: Dec 2005
    Location: FL
    Posts: 4,561
    For test purposes only.

    After each roll, displays count and roll results in an alert box.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 12:57 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.