By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

Guess A Number

P: 17
Hi I am making a basic "guess what number I'm thinking of" script and I cant figure out what I'm doing wrong. When I enter my number it changes it to the right number and says correct.
(p.s. kinda new so could be dumb mistake)

heres my script:

<html>
<head>
<script language="Javascript">
function get_random()
{
var ranNum1= Math.floor(Math.random()*4+1);
return ranNum1;
}
function match_random()
{
var the_answer=get_random()
if(document.form1.textbox1.value=the_answer)
{
alert("Correct!")
}
else if
(document.form1.textbox1.value!=the_answer)
{
alert("Sorry,the number was "+the_answer)
}
}
</script>
</head>
<body>
Try to guess what number I'm thinking by typing it in the text box and then clicking the button. (It's between 1 and 5)
<form name="form1">
<input type="text" name="textbox1">
<input type="button" value="Click me!" onclick="match_random()"
</body>
</html>
Jun 9 '07 #1
Share this Question
Share on Google+
7 Replies


100+
P: 428
if(document.form1.textbox1.value=the_answer)
if(document.form1.textbox1.value==the_answer)
Jun 9 '07 #2

10K+
P: 13,264
Hi I am making a basic "guess what number I'm thinking of" script and I cant figure out what I'm doing wrong. When I enter my number it changes it to the right number and says correct.
(p.s. kinda new so could be dumb mistake)

heres my script:

<html>
<head>
<script language="Javascript">
function get_random()
{
var ranNum1= Math.floor(Math.random()*4+1);
return ranNum1;
}
function match_random()
{
var the_answer=get_random()
if(document.form1.textbox1.value=the_answer)
{
alert("Correct!")
}
else if
(document.form1.textbox1.value!=the_answer)
{
alert("Sorry,the number was "+the_answer)
}
}
</script>
</head>
<body>
Try to guess what number I'm thinking by typing it in the text box and then clicking the button. (It's between 1 and 5)
<form name="form1">
<input type="text" name="textbox1">
<input type="button" value="Click me!" onclick="match_random()"
</body>
</html>
Use == to test for equality not = .
Jun 9 '07 #3

P: 17
Use == to test for equality not = .
Thanks guys, A mistype, but could you help me with one more thing?

I've edited it a little more so it looks like this

[HTML]<html>
<head>
<script language="Javascript">
function get_random()
{
var ranNum1= Math.floor(Math.random()*4+1);
return ranNum1;
}
function match_random()
{
var the_answer=get_random()
if(document.form1.textbox1.value==the_answer)
{
alert("Correct!")
}
else if
(document.form1.textbox1.value>0&& document.form1.textbox1.value<6&& document.form1.textbox1.value!=the_answer())
{
alert("Sorry, the number was "+the_answer)
}
else
{
alert("Please enter a number between 1 and 5")
}
}
</script>
</head>
<body>
Try to guess what number I'm thinking by typing it in the text box and then clicking the button. (It's between 1 and 5)
<form name="form1">
<input type="text" name="textbox1">
<input type="button" value="Click me!" onclick="match_random()"
</body>
</html>
[/HTML]
As you may I have noticed I'm trying to make it so that when you type a number not between 1-5 it tells you to type a number in that range. It works good except for if you type a number between 1-5 and its incorrect then it doesnt say anything.

thanks
Jun 9 '07 #4

P: 17
Ok so I am making a simple guess a number game (ya know, guess a number I'm thinking of between 1 and 5) And I have one problem, If the number is between 1 and 5 and correct, or isnt between 1 and 5 it says the right message, but if it is between 1 and 5 and is wrong it says nothing. Could I have help? Here is my script:
<html>
<head>
<script language="Javascript">
function get_random()
{
var ranNum1= Math.floor(Math.random()*4+1);
return ranNum1;
}
function match_random()
{
var the_answer=get_random()
if(document.form1.textbox1.value==the_answer)
{
alert("Correct!")
}
else if
(document.form1.textbox1.value>0 && document.form1.textbox1.value<6 && document.form1.textbox1.value!=the_answer())
{
alert("Sorry, the number was "+the_answer)
}
else
{
alert("Please enter a number between 1 and 5")
}
}
</script>
</head>
<body>
Try to guess what number I'm thinking by typing it in the text box and then clicking the button. (It's between 1 and 5)
<form name="form1">
<input type="text" name="textbox1">
<input type="button" value="Click me!" onclick="match_random()"
</body>
</html>
Jun 9 '07 #5

epots9
Expert 100+
P: 1,351
Ok so I am making a simple guess a number game (ya know, guess a number I'm thinking of between 1 and 5) And I have one problem, If the number is between 1 and 5 and correct, or isnt between 1 and 5 it says the right message, but if it is between 1 and 5 and is wrong it says nothing. Could I have help? Here is my script:
<html>
<head>
<script language="Javascript">
function get_random()
{
var ranNum1= Math.floor(Math.random()*4+1);
return ranNum1;
}
function match_random()
{
var the_answer=get_random()
if(document.form1.textbox1.value==the_answer)
{
alert("Correct!")
}
else if
(document.form1.textbox1.value>0 && document.form1.textbox1.value<6 && document.form1.textbox1.value!=the_answer())
{
alert("Sorry, the number was "+the_answer)
}
else
{
alert("Please enter a number between 1 and 5")
}
}
</script>
</head>
<body>
Try to guess what number I'm thinking by typing it in the text box and then clicking the button. (It's between 1 and 5)
<form name="form1">
<input type="text" name="textbox1">
<input type="button" value="Click me!" onclick="match_random()"
</body>
</html>
this should do the trick:
Expand|Select|Wrap|Line Numbers
  1. else if 
  2. ((document.form1.textbox1.value<0 || document.form1.textbox1.value>5) && document.form1.textbox1.value!=the_answer())
  3. {
  4. alert("Sorry, the number was "+the_answer)
  5. }
  6.  
or u can just make it so if its not equal, no matter how low or high, display sorry message.
Jun 9 '07 #6

acoder
Expert Mod 15k+
P: 16,027
I've added code tags for you.

On line 17, you have added some braces to make the_answer into a function.
Jun 11 '07 #7

acoder
Expert Mod 15k+
P: 16,027
Merged both "Guess a Number" threads.
Jun 11 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.