...

View Full Version : Nesting If/Else statement



asharm27
03-15-2005, 03:43 PM
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

afru
03-15-2005, 04:32 PM
Hi Arun,

Are you from AP? :)

Ok here is the 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>

asharm27
03-15-2005, 04:55 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum