|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
With the code provided, you must be getting errors.
Where is RandomInt() function defined? |
|
#3
|
|||
|
|||
|
it's defined here
<script type="text/javascript" src="http://dave-reed.com/book/random.js"> </script> |
|
#4
|
|||
|
|||
|
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>
|
|
#5
|
|||
|
|||
|
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')); |
|
#6
|
|||
|
|||
|
For test purposes only.
After each roll, displays count and roll results in an alert box. |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|