www.webdeveloper.com
Results 1 to 13 of 13

Thread: Javascript Lotto pick

  1. #1
    Join Date
    Nov 2007
    Posts
    10

    Javascript Lotto pick

    Not really sure where to go from here, I am stuck with this, I am trying to make this code so that you can enter 4 different numbers in the box and it will show the number of picks, like a lottery.
    Code:
    <html>
    <head>
      <title> Pick 4 Lotto </title>
      <script type="text/javascript"
              src="http://www.prenhall.com/reed/random.js">
      </script>
      <script type="text/javascript">
          function RollRepeatedly()
          
          {
            var numberA, numberB, numberC, numberD, roll1, roll2, pickNum, numPicks, numberCount;
    
            numPicks = 0;                          
            totalRolls = parseFloat(document.LottoForm.reps.value);
    
            repCount = 0;
            while (repCount < totalRolls) {           
                roll1 = RandomInt(1, 6);              
                roll2 = RandomInt(1, 6);
    
                if (roll1 == roll2) {                 
                    numPicks = numPicks + 1;    
                }
    
                numberCount = numberCount + 1;              
            }
    
            document.LottoForm.count.value = pickNum;
          }
      </script>
    
    </head>
    
    <body>
      <div style="text-align:center">
        <h2>Pick 4 Lotto</h2>
    
        <form name="LottoForm">
        This page demonstrates the futility of lotteries.
        Click on this button to perform Lotto drawings. <br />
        <input type="text" name="reps" size=1 value=0 />
        <input type="text" name="reps1" size=1 value=0 />
        <input type="text" name="reps2" size=1 value=0 />
        <input type="text" name="reps3" size=1 value=0 />
        <br /><br />
        <input type="button" value="Click to begin drawing" onClick="RollRepeatedly();" /> 
        <br /><br />
    
        Number of picks :   
        <input type="text" name="count" size=6 />
        </form>
      </div>
    </body>
    </html>

  2. #2
    Join Date
    Nov 2007
    Posts
    10
    Can anyone please help me?

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,444

    Arrow Consider this:

    I didn't read the book.
    You didn't supply the external JS code.
    It didn't work as coded.
    So I re-wrote it to work for me.

    See if you can see what I did to make it work (my way ).

    Post any questions.

    PHP Code:
    <html>
    <
    head>
      <
    titlePick 4 Lotto </title>
      <!-- 
    script type="text/javascript" src="http://www.prenhall.com/reed/random.js"></script -->
    <
    script type="text/javascript">
    function 
    RollRepeatedly() {
      var 
    roll1 0;
      var 
    roll2 0;
      var 
    PickNo 0;
      var 
    repCount 0;
      while (
    PickNo 4) {
        
    roll1 Math.floor(Math.random()*6+1);
        
    roll2 Math.floor(Math.random()*6+1);
        if (
    roll1 == roll2) { document.getElementById('Pick'+PickNo).value roll1;  PickNo++; } 
        
    repCount repCount 1;   
    //    alert(PickNo+' : '+roll1+' : '+roll2);           // for testing only
      
    }
      
    document.getElementById('reps').value repCount;
    }
    </script>
    </head>

    <body>
      <div style="text-align:center">
        <h2>Pick 4 Lotto</h2>
        <form name="LottoForm">
        This page demonstrates the futility of lotteries.<br />
        Click on this button to perform Lotto drawings. <p />
        <input type="text" id="Pick0" name="reps0" size="1" value="0" />
        <input type="text" id="Pick1" name="reps1" size="1" value="0" />
        <input type="text" id="Pick2" name="reps2" size="1" value="0" />
        <input type="text" id="Pick3" name="reps3" size="1" value="0" />
        <p />
        <input type="button" value="Click to begin drawing" onClick="RollRepeatedly();" /> 
        <p />

        Number of picks : 
        <input id="reps" name="reps" type="text" name="count" size="6" value="0" readonly />
        </form>
      </div>
    </body>
    </html> 

  4. #4
    Join Date
    Nov 2008
    Location
    New Mexico, Albuquerque
    Posts
    25

    Angry Code didn't work as it is suppose to.

    This is how I fixed it but I still need help! I am dying trying to get this darn Pick-4 to properly work.

    <!-- This page is designed to simulate the PICK-4 lottery, repeatedly until desired sequence is
    obtained. The user should be able to enter the desired sequence in a series of four text
    boxes, then click a button to see how many drawings the page had to perform before their
    sequence came up. This program will also make use of while loops for conditional execution,
    as well as implementing counter-driven loops. -->

    <head>
    <title> Pick 4 Lotto </title>

    <script type="text/javascript"
    src="http://www.dave-reed.com/book/random.js">
    </script>
    <script type="text/javascript">
    function RollRepeatedly()
    // Assumes: User will input desired sequence
    // Results: While loop will not execute until desired sequence is found.

    {

    /******** Declared Varibles ********/

    var roll1 = 0; roll2 = 0, PickNo = 0; repCount = 0;

    /******** End of Declaration ********/

    while (PickNo < 4) {
    roll1 = Math.floor(Math.random()*9+1);
    roll2 = Math.floor(Math.random()*9+1);

    if (roll1 == roll2) { document.getElementById('Pick'+PickNo).value = roll1; PickNo++; }
    repCount = repCount + 1;
    // alert(repCount+' : '+roll1+' : '+roll2); // for testing only
    }
    document.getElementById('reps').value = repCount;
    }
    </script>
    </head>

    <body>
    <div style="text-align:center">
    <h1>Pick 4 Lotto</h1>

    <form name="LottoForm">
    This page demonstrates the futility of lotteries.<br />
    Click on this button to perform Lotto drawings until <input type="text" id="pick0" name="reps0" size="1" value="0" />
    <input type="text" id="pick1" name="reps1" size="1" value="0" />
    <input type="text" id="pick2" name="reps2" size="1" value="0" />
    <input type="text" id="pick3" name="reps3" size="1" value="0" /> appears.
    <p />
    <input type="button" value="Click to begin drawing" onClick="RollRepeatedly();" />
    <p />

    Number of picks :
    <input id="reps" name="reps" type="text" size="6" value="0" />
    </form>
    </div>
    </body>
    </html>

  5. #5
    Join Date
    Nov 2008
    Location
    New Mexico, Albuquerque
    Posts
    25

    Exclamation opps

    I was trying to edit my name from the script I had done and forgot this:

    <html>

    at the top anyways

  6. #6
    Join Date
    Nov 2008
    Location
    New Mexico, Albuquerque
    Posts
    25

    Exclamation random.js

    Here is the random.js file you had mentioned not having.

    Code:
    // File: random.js
    // Author: Dave Reed
    //
    // This file contains several routines for generating random values.
    // Source code is available at http://dave-reed.com/book
    //////////////////////////////////////////////////////////////////////
    
    function RandomNum(low, high)
    // Given : low <= high
    // Returns : a random number in the range [low, high)
    {
        return Math.random()*(high-low) + low;
    }
    
    function RandomInt(low, high)
    // Given : low <= high
    // Returns : a random integer in the range [low, high]
    {
        return Math.floor(Math.random()*(high-low+1)) + low;
    }
    
    function RandomChar(str)
    // Given : str is a nonempty string
    // Returns: a random character from the string
    {
        return str.charAt(RandomInt(0, str.length-1));
    }
    
    function RandomOneOf(list)
    // Given : list is a nonempty list (array)
    // Returns: a random item from the list
    {
        return list[RandomInt(0, list.length-1)];
    }
    Last edited by ppotter; 11-20-2008 at 07:46 PM. Reason: forgot to put in the CODE

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,444

    Smile

    You've been working on this problem for almost a year!
    Gees, I thought you were happy with a solution.

    The script of post #3 was designed to show how many rolls of two dice it would randomly take to match values of the 2 dice.
    This was then repeated for 3 additional sequences.

    In other words:
    1. Roll two dice and save the values.
    2. Repeat #1 until both dice match.
    3. Repeat 1-2 for 3 more matches,
    keeping count of how many rolls are made to find matches.

    If you want to increase to a 10 sided dice, change the random # from 6 to 10.

    If you want to match an initial number entered, you could do this as well.

    But if you are trying to roll 4 dice simultaneously to match 4 input numbers
    be prepared to restart the program several times as it will probably time out
    before a match is randomly found.

    Probably would be better to use a probability formula to determine a statistical average for the number of rolls needed
    if that is what you wish to calculate.

  8. #8
    Join Date
    Nov 2008
    Location
    New Mexico, Albuquerque
    Posts
    25
    Thank you, so how exactly would I script it to let me match the initial sequence. Well have the while loop continue until the initial sequence is met? Then have the while loop execute once the sequence is found? This one particular script has me going nuts, I must have read the chapter 70 times and still having issues. Sorry to ask you to elaborate but I am more of a visual learner.


    Thank you once again for replying.

  9. #9
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,444

    Lightbulb Comment on this ...

    Consider this, modified from post #3 and sitll rolls one dice at a time until match for each entry is found.
    Report total number of rolls to get matching number.

    PHP Code:
    <html>
    <
    head>
      <
    titlePick 4 Lotto </title>
      <!-- 
    script type="text/javascript" src="http://www.prenhall.com/reed/random.js"></script NOT USED -->
    <
    script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?t=168450
    function RollRepeatedly() {
      var 
    flag false;
      var 
    YourPicks = []
          
    YourPicks.push(document.getElementById('uPick0').value);  if (YourPicks[YourPicks.length-1] == '') { flag true; }
          
    YourPicks.push(document.getElementById('uPick1').value);  if (YourPicks[YourPicks.length-1] == '') { flag true; }
          
    YourPicks.push(document.getElementById('uPick2').value);  if (YourPicks[YourPicks.length-1] == '') { flag true; }
          
    YourPicks.push(document.getElementById('uPick3').value);  if (YourPicks[YourPicks.length-1] == '') { flag true; }
      
    document.getElementById('Pick0').value '';
      
    document.getElementById('Pick1').value '';
      
    document.getElementById('Pick2').value '';
      
    document.getElementById('Pick3').value '';
      if (
    flag == true) { alert('Must enter some choices to match'); return; }
      var 
    roll1 0;
      var 
    PickNo 0;
      var 
    repCount 0;
      while (
    PickNo 4) {
        
    roll1 Math.floor(Math.random()*10);
        if (
    roll1 == YourPicks[PickNo]) { document.getElementById('Pick'+PickNo).value roll1;  PickNo++; }
                                   else { 
    document.getElementById('Pick'+PickNo).value roll1; }
        
    repCount repCount 1;   
      }
      
    document.getElementById('reps').value repCount;
    }
    </script>
    </head>

    <body>
      <div style="text-align:center">
        <h2>Pick 4 Lotto</h2>
        This page demonstrates the futility of lotteries.<br />
        Click on this button to perform Lotto drawings. <p />
        <form name="LottoForm">
    <center>
    <table border="0">
    <tr><td>
    Your Choice: 
    </td><td>
        <input type="text" id="uPick0" name="ureps0" size="1" value="0" />
        <input type="text" id="uPick1" name="ureps1" size="1" value="0" />
        <input type="text" id="uPick2" name="ureps2" size="1" value="0" />
        <input type="text" id="uPick3" name="ureps3" size="1" value="0" />
    </td></tr>
    <tr><td>
    Computer rolls:
    </td><td>
        <input type="text" id="Pick0" name="reps0" size="1" value="" readonly />
        <input type="text" id="Pick1" name="reps1" size="1" value="" readonly />
        <input type="text" id="Pick2" name="reps2" size="1" value="" readonly />
        <input type="text" id="Pick3" name="reps3" size="1" value="" readonly />
    </td></tr>
    </table>
    </center>
        <p />
        <input type="button" value="Click to begin drawing" onClick="RollRepeatedly();" />
        <p />

        Number of rolls to match choice :
        <input id="reps" name="reps" type="text" name="count" size="6" value="0" readonly />
        </form>
      </div>
    </body>
    </html> 
    Last edited by JMRKER; 11-21-2008 at 01:21 PM.

  10. #10
    Join Date
    Nov 2008
    Location
    New Mexico, Albuquerque
    Posts
    25

    Thumbs up Yay!

    OMG! Thank you so much... I finally see (understand too) what the book is actually trying to say.

  11. #11
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,444
    You're most welcome.
    Glad I was able to help ... even if it was a year later.
    Good Luck!

  12. #12
    Join Date
    Nov 2008
    Location
    New Mexico, Albuquerque
    Posts
    25

    Angry ESP-2 Test. (homework help)

    Okay I have another code for you to tackle. This one was from a previous chapter and I have been working on it for weeks.
    Create a web page that allows the user to conduct an ESP test. Statistics concerning the number and percentage of correct guesses should be maintained in text boxes. When the user clicks on one of these buttons, the corresponding guess should be passed as an input to the PickShape function. For the pictures, the rectangle containing the question mark represents the back of a card that is associated with a particular shape. After contemplating which shapes appears on the front of the card, the user clicks the button corresponding to the shape he or she believes is on the card (here, four possibilities are assumed). After each guess, the question mark should be replaced by a randomly selected shape, and an alert box should notify the user to whether the guess was correct. Statistics concerning the number and percentage of correct guesses should be maintained in text boxes. After the statistics have been updated and the alert box has been closed, the page should restore the question mark image, enabling the user to enter another guess.

    Here is my code which has some syntax errors, and I cannot get the shapes to appear or the counter to function.

    Code:
    <html>
     <!-- ESP-2 Test.html      Exercise 11.17 page 212-213                                     -->
     <!-- This page allows the user to conduct an ESP test.					    -->   
     <!-- ==================================================================================== -->
    
     <!--     This page serves as an ESP test, with a button for each possible guess.  	   
              For each shape named button, a card is associated with that particular shape.    
              The user is directed to guess which shape lies beneath the question mark          
              rectangular card.An alert box will notify the user whether their guess was 	   
              correct or not.Statistics concerning the number of guesses the user has taken    
              correctly will be displayed in the availible text boxes.                         -->                 
    
     <head>
       <title> ESP Test </title>
      
      
       <script type="text/javascript"
               src="http://dave-reed.com/book/random.js">
       
          /* This is the (above) source in which a general-purpose funtion is placed from a library, 
             and then loaded unto the page as needed. */
      
       </script>
    
       <script type="text/javascript">  
          
       function PickShape(guess) { 
          
          /* This function is intended to randomly select a shape using the RandomInt function, and 
    	 compare the shape with that of the one the user picks. *?
          
          /****** Varibles ******/
         
          var Shape; Correctcount = 0; TotalCount = 0;
          
          /****** End of Varibles ******/
         
         // Assumes: the user will try to pick or guess the corresponding shape
         // Results: picks a randomly selected shape and compares that to the shape the user picks.
    
            
       Shape = RandomInt(1,4);
       TotalCount = TotalCount + 1; 
    
       if (PickShape == 1)
    	{
         document.getElementById("Square").src ="http://www.dave-reed.com/book/Images/square.gif";
    	if(PickShape == guess)
    	{
    		Correctcount = Correctcount + 1;
    			
    	}
    	}
        else if (PickShape == 2)
    	{
    	document.getElementById("Circle").src ="http://www.dave-reed.com/book/Images/circle.gif";
    	if(PickShape == guess)
    	{
    		Correctcount = Correctcount + 1;
    	}
    	}
        else if (PickShape == 3)
            {
            document.getElementById("Star").src ="http://www.dave-reed.com/book/Images/star.gif";
            if(PickShape == guess)
            {
    		Correctcount = Correctcount + 1;
            }
    	}
        else if (PickShape == 4)
    	{
    	document.getElementById("Triangle").src ="http://www.dave-reed.com/book/Images/triangle.gif";
    	if(PickShape == guess)
    	{
    		Correctcount = Correctcount + 1;
    	}
    	}
            document.getElementById("Correctcountbox").value = Correctcount;
    	
            numGuess = Correctcount/TotalCount;
            
    </script> 
     </head>
    
     <body>
        
       <h1 style="text-align:center">ESP Test</h1>
        <h2 style="text-align:center">Measure your Extrasensory Perception</h2>
     
        <p style="text-align:center">
    
     
                The card below hides the image of a square, triangle, circle, or star.<br />         
    Concentrate on the hidden image, then click on the corresponding button to make your guess.
        </p>
        <br />
        <p style="text-align:center">          
      Statistics showing the number and percentage of correct guesses will appear in the boxes.
        </p>
        <hr />
        
        <div style="text-align:center">
    
     <!-- This tag will allow there to be a button that when clicked will 'call' upon the value (function) specified in the onclick section. -->
                               
         
         <input type="button" value="Square" 
                onclick="PickShape(1);" />
         <input type="button" value="Triangle"
                onclick="PickShape(4);" />
         <input type="button" value="Circle"
                onclick="PickShape(2);" />
         <input type="button" value="Star"
                onclick="PickShape(3);" />
         <br />
         <p>
            <img id="mystery" alt="question mark" src="Images/mystery.gif" />
    		
         </p>
                You have guessed correctly <input type="text" id="Correctcount" size="10" value="0" /> out of <input type="text" id="TotalCount" size="10" value="0" /> times.
     
         
         <br />  
       
     
                That means you were correct<input type="text" id="numGuess" size="10" value="0" />% of the time.
    
     <!-- This is the end of the program and webpage. -->
       
        
        
       </div>
     </body>
    </html>

  13. #13
    Join Date
    Apr 2010
    Posts
    1
    Well, two years late. But, I'm using the examples for the lotto thing, so I figure I might as well contribute. Here is the code I used for the ESP-2. Apparently, the book hasn't changed much in three years! :P

    Code:
    <!-- esp2.html ============= XXXXXXXXX ===============  -->
    <!--A page designed to test ESP by asking the user to guess an image and compare results with one randomly chosen by the program.-->
    <!--A count is also kept of correct and total guesses, along with a correct guess percentage-->
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <link href="style.css" rel="stylesheet" type="text/css" />
    
    <title>ESP Test v.2</title>
    
    <!-- Link to the function library owned by the text's author -->
    <script type="text/javascript" src="http://dave-reed.com/book/random.js">
    </script>
    
    <script type="text/javascript">
    
    function pickImage (guess)
    //Assumes: user attempts to guess what random image will be displayed
    //Results: a random image is displayed
    {
    	// variables are declared
    	var guess
    	var number
    	
    	// the "number" variable is assigned a value, referencing an external function in the author's library
    	number = RandomInt (1,4);
    	
    	// if/else statements to display the correct image
    	if (number == 1)
    	{
    		document.getElementById("shape").src = "square.png";
    	}
    	else if (number == 2)
    	{
    		document.getElementById("shape").src = "triangle.png";
    	}
    	else if (number == 3)
    	{
    		document.getElementById("shape").src = "circle.png";
    	}
    	else 
    	{
    		document.getElementById("shape").src = "star.png";
    	}
    	
    	// if/else statement for alerts and the percentCorrect text box
    	if (number == guess)
    	{
    		document.getElementById("correctGuesses").value = 
    			parseFloat(document.getElementById("correctGuesses").value)+1;
    		alert('Congratulations! You guessed correctly!');
    	}
    	else
    	{
    		alert('Sorry, your guess was incorrect.');
    	}
    	
    	
    	// statements for the counters
    	document.getElementById("totalGuesses").value = 
    		parseFloat(document.getElementById("totalGuesses").value)+1;
    		
    	document.getElementById("percentCorrect").value = 
    		Math.round((parseFloat(document.getElementById("correctGuesses").value) / parseFloat(document.getElementById("totalGuesses").value)) * 100);
    
    
    }
    
    // Function to clear fields on page reload
    function clear()
    	
    {
      document.getElementById('correctGuesses').value="0";
      document.getElementById('totalGuesses').value="0";
      document.getElementById('percentCorrect').value="0";
    }
    
    
    </script>
    
    </head>
    
    <body onload="clear()">
    
    <h1>ESP Test v.2</h1>
    
    <hr color="#306" />
    
    <p>
    Do you think you have ESP? Let's find out!
    </p>
    
    <!-- the default image that will be replaced when the user clicks a button -->
    <p>
    <img id="shape" alt="shape image" src="question.png" />
    </p>
    
    <table align="center">
    
    	<tr>
    		<th colspan="4">Click on a button to guess a shape:</th>
    	</tr>
    	<tr>
    		<td><input type="button" value="Square" onclick="pickImage('1');" /></td>
    		<td><input type="button" value="Triangle" onclick="pickImage('2');" /></td>
    		<td><input type="button" value="Circle" onclick="pickImage('3');" /></td>
    		<td><input type="button" value="Star" onclick="pickImage('4');" /></td>
    	</tr>
    	<tr>
    		<th colspan="4">Here are your statistics:</th>
    	</tr>
    	<tr>
    		<td colspan="2">Correct Guesses:</th>
    		<td colspan="2"><input type="text" value="0" id="correctGuesses" />&nbsp;&nbsp;</td>
    	</tr>
    	<tr>
    		<td colspan="2">Total Guesses:</th>
    		<td colspan="2"><input type="text" value="0" id="totalGuesses" />&nbsp;&nbsp;</td>
    	</tr>
    	<tr>
    		<td colspan="2">Percentage Correct:</th>
    		<td colspan="2">&nbsp;<input type="text" value="0" id="percentCorrect" />&#37;</td>
    	</tr>
    	
    
    
    </body>
    </html>

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