Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    New Coder
    Join Date
    Aug 2004
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #3
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •