Hey all,
I'm having to write a program that generates a random number between 1-1000 and then allows the user to make guesses in a text box, answering to high or to low, until the correct answer is entered. I had the program working fine with using strictly prompts and alerts but when I tried to create a form I started having trouble. Please take a look and give me any advice you may have. Thanks!!
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>9.30</title>
<script type="text/javascript">
window.alert("Please Enter a Number Between 1 and 1000 in the field!");
function Game()
{
var inputField1 = document.getElementById( "entry1" );
var guess = parseFloat( inputField1.value );
var answerField = document.getElementById( "answer" );
answerField.value = guessANum( guess );
}
function guessANum( guess ){
var randomNumber = Math.floor(1 + Math.random() * 1000);
while (randomNumber != guess){
if (randomNumber > guess)
{
guess = window.alert("Too low, try again.");
}
else if (randomNumber < guess)
{
guess = window.alert("Too high, try again.");
}
}
window.alert("Congratulations. You guessed the number!")
//var again = window.prompt("Enter 'yes' to play again");
//if (again == "yes")
//guessANum();
}
</script>
</head>
<body>
<form action = "">
<div>
<label>Your Guess:
<input id = "entry1" type = "text" /></label>
<br />
<input type = "button" value = "Calculate"
onclick = "Game()" />
</div>
</form>
</body>
</html>