Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Guessing game help

Status
Not open for further replies.

masteryamaha

Programmer
Feb 13, 2005
15
US
I have been made up this simple guessing game, i got bored, but it does not work, any help.

<head>
<title>Practice html </title>
<script type="text/javascript" language="javascript>
<!--
funtion Guess_game()
var rnum=Math.random()*20
var guess="GetElementById('guess')"

If (rnum == guess)
{document.write("Good Guess")}
else
{document.write("Try Again")}
-->
</script>
</head>
<body>
<form onload="Guess_game()" >
<input type="text" id="guess" />
<input type="button" name="Submit" value="submit" />
</form>
</body>
 

1. You should not use document.write after the page has loaded. Doing so will cause all content (including JavaScript code) to be overwritten. Try using alert instead.

2. I do not understand why you are enclosing "GetElementById('guess')" in quotes - it's a method call, not a string. Regardless, you need to access the value property of the element, not the element itself. Use this instead:

Code:
var guess = document.getElementById('guess').value;

Hope this helps,
Dan


The answers you get are only as good as the information you give!

 

Incidentally, your random number guessing game will be VERY hard to win, as the random number you are generating will be a floating point number, not an integer. Try using this instead:

Code:
var rnum = Math.floor(Math.random() * 20);[code]

Dan


The answers you get are only as good as the information you give!
 


I spotted a few other things (in addition to those that BillyRayPreachersSon identified) - the corrections are marked in RED for you:

You must close speech marks if you open them:
Code:
<script type="text/javascript" language="javascript[COLOR=red]"[/color]>

Javascript requires you spell certain reserved words correctly:
Code:
fun[COLOR=red]c[/color]tion Guess_game()

Javascript is case sensitive and requires you use lower case for if:
Code:
[COLOR=red]if[/color] (rnum == guess)

When using comments to protect "older" browsers and to promote XHTML validity, make sure you use the correct type of comment:
Code:
[COLOR=red]//[/color]-->

A warning - giving your submit button the name submit is going to make it tricky if you ever want to use javascript to submit the form (I suggest you change it before this ever becomes a problem):
Code:
<input type="button" name="Submit[COLOR=red]Btn[/color]" value="submit" />

Finally, it's good practice to put a semi-colon at the end of lines. This is not a requirement - but like the submit button name above, it will lead to problems as your code becomes more abundant. Just a suggestion, mind.

Hope these extra pointers help!

Jeff
 
Using all the suggestions and couple of my own,this should work.
Code:
<head>
    <title>Practice html </title>
    <script type="text/javascript" language="javascript">
   // <!--
    function Guess_game(){
    var rnum = Math.floor(Math.random() * 20);

   var guess = document.getElementById('guess').value;
    if(rnum == guess)
      {alert("Good Guess")}
     else
[COLOR=red]alert(rnum +" was the Number \nTry Again");//don't need braces here
[/color]

  }
 //   -->
    </script>
</head>
 <body >
 <form [COLOR=red]onsubmit[/color]="Guess_game()" >
 <input type="text" id="guess" />
  <input type="[COLOR=red]submit[/color]" name="SubmitBtn" value="submit" />
 </form>
 </body>

Glen 
[URL unfurl="true"]http://lantzvillecomputers.com[/URL]
 


Glen,

Was all that really necessary? You have now provided a solution full of inconsistencies, with bad coding techniques and adds nothing to the learning experience this thread offers (in my opinion it detracts from it in fact).

You, of all people, should know about the use of [ CODE ] tags, as well!

Jeff
 
Code:
<html>
<head>
<title>Practice html </title>
<script type="text/javascript" language="javascript">
<!--
    function Guess_game()
{
     var rnum = Math.floor(Math.random() * 20);
     var num = document.getElementById('guess').value;

     if(rnum == num){
          	    alert("Good Guess");
		            }
     else{
          alert(rnum +" is the Number \nTry Again");
         }
}
 //   -->//hide from older browsers
</script>
</head>
<body>
<noscript>Needs Javascript to play.</noscript>
<form onsubmit="Guess_game()" >
     <input type="text" id="guess" />
     <input type="submit" name="SubmitBtn" value="submit" />
</form>
</body>
</html>

Glen
 


In the problem posted, yes, the random number would be regenerated each time - making it a very hard game for any user.

Ideally you would create the random number, store it in a global variable and then test against the global variable each "guess".

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top