Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-15-2005, 02:43 PM   PM User | #1
asharm27
New to the CF scene

 
Join Date: Mar 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
asharm27 is an unknown quantity at this point
Nesting If/Else statement

Hi
I am currently working on a webpage that has a javascript element to analyse the input in a text box.

I have a series of numbers in a table and the user needs to complete the series by entering their guess for the next number.

There is then a submit button that they press to see if they are correct.

If: The guess is too high i want it to display a window.alert saying that "the guess is too high, try again"

If: The guess is too low i want it to display a window.alert saying "the guess is too high, try again"

Else: A window alert saying "Correct!"

Problem is I only want it to allow 5 guesses i was thinking of using the do/while function.

Is this possible?

here is my attempt:

var loop = 0;

function numberGuess(TheNumber,TheGuess)
{
if(loop = 10)
{
window.alert("The number is" + TheNumber)
}
else
{
if (TheGuess > TheNumber)
{
window.alert("Too Big");
}
else if(TheNumber > TheGuess)
{
window.alert("Too Small");
}
else
{
window.alert("CORRECT");
}
}
loop += 1;

Any suggestions will be greatly appreciated!

thanks

Arun
asharm27 is offline   Reply With Quote
Old 03-15-2005, 03:32 PM   PM User | #2
afru
New Coder

 
Join Date: Aug 2004
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
afru is an unknown quantity at this point
Hi Arun,

Are you from AP?

Ok here is the code...
Code:
<HTML>
<Head>
<Script Language=JavaScript>
var maxattempts=5;
var attempts=0;
function check(ans){
	if(attempts>=maxattempts)
	{
		alert("Sorry you have lost it. Too many attempts.");
		return;
	}
	var guess=document.getElementById("guess");
	var val=parseInt(guess.value);

	if(val<ans)
	alert("Too Small Dear...");
	else if(val>ans)
	alert("Too Big Dear...");
	else
	{
		alert("You are Great... Got it Right.");
		attempts=0;
	}
	attempts++;
	guess.select();
	guess.focus();
}

</Script>
</Head>
<Body>
<TABLE border=1 cellpadding=10 cellspacing=10>
<TR>
<TD>2</TD>
<TD>4</TD>
<TD>6</TD>
<TD>8</TD>
<TD><input type="text" name='guess'></TD>
<TD><input type="submit" value="Guess" onclick="check(10)"></TD>
</TR>
</TABLE>

</Body>
</HTML>
afru is offline   Reply With Quote
Old 03-15-2005, 03:55 PM   PM User | #3
asharm27
New to the CF scene

 
Join Date: Mar 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
asharm27 is an unknown quantity at this point
Great thats amazing!!

Thanks for the quick response!

Unfortunately I'm not from UP but I was there last month on a visit!! I'm from England, family from Delhi...

thanks again

arun
asharm27 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:42 PM.


Advertisement
Log in to turn off these ads.