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>

First there is problem in the line

var answerField = document.getElementById( "answer" );

You are trying to access an element with id="answer". But there is no such element on the html page.

Second I would suggest you to rethink your algorithm used in the method guessANum()
You might enter into a infinite loop because of while and also on click of 'Calculate' button the guess number will change each time defeating the purpose that the user must guess the number

I'll try that out ... Another question I had was for resetting the text field upon user guess. Is there a way to get the button to perform two operations or would I need to make a function for that?

you can perform two or more functions on button click like

<input type = "button" value = "Calculate" onclick = "func1();func2();" />

and perform different actions in these functions

OR

write javascript itself in onclick as,

<input type = "button" value = "Calculate" onclick = 'Game();document.getElementById( "entry1" ).value="";' />

Just be careful with single and double quotes used

But the way I would recommend is to have the line

document.getElementById( "entry1" ).value="";

at the end of the Game() function after the call to guessANum()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.