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.
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.
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 06:46 PM.
Reason: forgot to put in the CODE
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.
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.
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> <title> Pick 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>
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>
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" /> </td>
</tr>
<tr>
<td colspan="2">Total Guesses:</th>
<td colspan="2"><input type="text" value="0" id="totalGuesses" /> </td>
</tr>
<tr>
<td colspan="2">Percentage Correct:</th>
<td colspan="2"> <input type="text" value="0" id="percentCorrect" />%</td>
</tr>
</body>
</html>
Bookmarks