473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Guess A Number

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
7 2588
mrhoo
428 256MB
if(document.form1.textbox1.value=the_answer)
if(document.form1.textbox1.value==the_answer)
Jun 9 '07 #2
r035198x
13,262 8TB
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
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
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
1,351 Expert 1GB
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
16,027 Expert Mod 8TB
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
16,027 Expert Mod 8TB
Merged both "Guess a Number" threads.
Jun 11 '07 #8

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

Similar topics

8
by: EAS | last post by:
Hey, I'm new to python (and programming in general) so I'll prolly be around here a lot... Anyways, I've found out how to make a "guess my number game" where the player guesses a number between...
2
by: d0ugg | last post by:
Hi, I'm starting to learn C++. And I have this assignment and I can't compile it for some reason.. Here is the problem: -> Write a number guessing name program named guess. The program will...
3
by: willkab6 | last post by:
I am very new to both programming and Pyhton and while trying to do some practice using A byte of python an Error pops up on the IDLE shell. I am using windows XP. PLease see below. while running:...
2
by: realNewbie | last post by:
I am trying to duplicate the Guess My Number Game from Python Programming for the Absolute Beginner by Michael Dawson but for some reason I still cannot figure out my output is not completely...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.