473,803 Members | 3,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Guess A Number

17 New Member
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="Javas cript">
function get_random()
{
var ranNum1= Math.floor(Math .random()*4+1);
return ranNum1;
}
function match_random()
{
var the_answer=get_ random()
if(document.for m1.textbox1.val ue=the_answer)
{
alert("Correct! ")
}
else if
(document.form1 .textbox1.value !=the_answer)
{
alert("Sorry,th e number was "+the_answe r)
}
}
</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 2610
mrhoo
428 Contributor
if(document.for m1.textbox1.val ue=the_answer)
if(document.for m1.textbox1.val ue==the_answer)
Jun 9 '07 #2
r035198x
13,262 MVP
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="Javas cript">
function get_random()
{
var ranNum1= Math.floor(Math .random()*4+1);
return ranNum1;
}
function match_random()
{
var the_answer=get_ random()
if(document.for m1.textbox1.val ue=the_answer)
{
alert("Correct! ")
}
else if
(document.form1 .textbox1.value !=the_answer)
{
alert("Sorry,th e number was "+the_answe r)
}
}
</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
Suudsu2200
17 New Member
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="Javas cript">
function get_random()
{
var ranNum1= Math.floor(Math .random()*4+1);
return ranNum1;
}
function match_random()
{
var the_answer=get_ random()
if(document.for m1.textbox1.val ue==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_answe r)
}
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
Suudsu2200
17 New Member
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="Javas cript">
function get_random()
{
var ranNum1= Math.floor(Math .random()*4+1);
return ranNum1;
}
function match_random()
{
var the_answer=get_ random()
if(document.for m1.textbox1.val ue==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_answe r)
}
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 Recognized Expert Top Contributor
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="Javas cript">
function get_random()
{
var ranNum1= Math.floor(Math .random()*4+1);
return ranNum1;
}
function match_random()
{
var the_answer=get_ random()
if(document.for m1.textbox1.val ue==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_answe r)
}
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 Recognized Expert Moderator MVP
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 Recognized Expert Moderator MVP
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
12030
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 1 and 100, but I want to switch things around. I want to be able to put in my own number and have the computer guess it. I already know how to make it guess (by using randrange) but I'm having a hard time making it smarter. (Like guessing higher...
2
1543
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 generate a pseudo random number in the range 0 to 99. The program will then enter an infinite loop and will: 1 - prompt the user to enter a guess between 0 and 99. 2 - read the guess 3 - if the guess and the random number are equal a - the program...
3
6374
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: guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' running = False # this causes the while loop to stop elif guess < number:
2
4840
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 correct. The goal of the program is from the computer to pick a number between 1 and 100 and the user to try to guess the number...if the user choses a number that is too high, the computer will tell the user that their guess is too large and if the number...
0
9565
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10550
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.