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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2004
    Location
    USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question student --guessing game script problems

    I am working on a homework assignment that asks as a guessing game. The user inputs numbers in a box and clicks the submit button to see if it is right. If it is correct, then an alert is displayed, if not an alert is also displayed. There is a 10 second timer that displays a confirmation box to continue or quit the game. If not responded by the 10 second timer the window closes.
    or if the use clicks ok then user is returned to the game.

    I would appreciate it if someone could tell me where I went wrong.
    Not looking for someone to do my work, just to explain what I am not doing correctly. I fixed the brackets and get no errors but still don't work.
    How do I get the alerts to show up?

    Thanks.
    Here is my script:

    <HTML>
    <HEAD>
    <TITLE>Guess Number</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    var number=7;
    function returnAnswer() {
    if (number == 7) {
    alert("You are correct!");
    Else (number != 7);
    alert("Try again!");
    }
    function confirmation() {
    return confirm("Do you want to stop the game?");
    if (confirmation == true){
    setTimeout("window.close, 10000");
    Else (confirmation == false);
    open("GuessNumber.html");
    }
    }
    // STOP HIDING FROM IMCOMPATIBLE BROWSERS -->
    </SCRIPT>
    </HEAD>
    <BODY>
    <H1>The Guessing Game</H1>
    <FORM NAME="game">
    <INPUT TYPE="text" NAME="number" SIZE="2">
    <INPUT TYPE="bUTTON" NAME="Guess" VALUE="Guess" onClick="returnAnswer">
    </FORM>
    </BODY>
    </HTML>
    confused: thanks for the help guys, but still got a problem
    Last edited by wolfy; 11-14-2004 at 02:41 PM. Reason: wrong code

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Posts
    429
    Thanks
    0
    Thanked 20 Times in 20 Posts
    your if's and else's need to be contained in braces
    function my(){
    if(number==7){
    //do this
    }
    else{
    //do this
    }
    }
    NO Limits!! www.dhcreationstation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    since your a student here is a friendly advise use indentation in ur code makes it easier to read:
    Code:
    function something(){
      *kode*
      if(){
        *kode*
      }
      else
      {
        *kode*
      }
    }

  • #4
    New to the CF scene
    Join Date
    Sep 2004
    Location
    USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am working on a homework assignment that asks as a guessing game. The user inputs numbers in a box and clicks the submit button to see if it is right. If it is correct, then an alert is displayed, if not an alert is also displayed. There is a 10 second timer that displays a confirmation box to continue or quit the game. If not responded by the 10 second timer the window closes.
    or if the use clicks ok then user is returned to the game.

    I would appreciate it if someone could tell me where I went wrong.
    Not looking for someone to do my work, just to explain what I am not doing correctly. I fixed the brackets and get no errors but still don't work.
    How do I get the alerts to show up?

    Thanks.
    Here is my script:

    <HTML>
    <HEAD>
    <TITLE>Guess Number</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    var number=7;
    function returnAnswer() {
    if (number == 7) {
    alert("You are correct!");
    Else (number != 7);
    alert("Try again!");
    }
    function confirmation() {
    return confirm("Do you want to stop the game?");
    if (confirmation == true){
    setTimeout("window.close, 10000");
    Else (confirmation == false);
    open("GuessNumber.html");
    }
    }
    // STOP HIDING FROM IMCOMPATIBLE BROWSERS -->
    </SCRIPT>
    </HEAD>
    <BODY>
    <H1>The Guessing Game</H1>
    <FORM NAME="game">
    <INPUT TYPE="text" NAME="number" SIZE="2">
    <INPUT TYPE="bUTTON" NAME="Guess" VALUE="Guess" onClick="returnAnswer">
    </FORM>
    </BODY>
    </HTML>
    confused: thanks for the help guys, but still got a problem!

  • #5
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Let's see...

    Code:
    var number=7;
    ...
    
    <INPUT TYPE="text" NAME="number" SIZE="2">
    These should be named differently

    Code:
    var number=7;
    function returnAnswer() 
    {
    	if (number == 7)
    	{
    		alert("You are correct!");
    	Else (number != 7);
    		alert("Try again!");
    	}
    The logic is flawed.
    Your braces are wrong.
    JavaScript is case sensitive.
    This does nothing, and screws up the conditional.

    Code:
    function confirmation()
    {
    	return confirm("Do you want to stop the game?");
    	if (confirmation == true)
    	{
    		setTimeout("window.close, 10000");
    	Else (confirmation == false);
    		open("GuessNumber.html");
    	}
    }
    That's a problem.
    Nothing calls this function
    close is missing parentheses "window.close()"
    your braces are wrong.
    JS is case sensitive.
    Your else statement is messed up again.
    Code:
    <INPUT TYPE="text" NAME="number" SIZE="2">
    <INPUT TYPE="bUTTON" NAME="Guess" VALUE="Guess" onClick="returnAnswer">

    Your function call is missing parentheses.
    You are not getting the value of the number input.
    You will have to convert that from string to number.
    Edit: correction: in this case, the string wouldn't need to be converted...
    Last edited by codegoboom; 11-14-2004 at 06:27 PM.
    *this message will self destruct in n-seconds*


  •  

    Posting Permissions

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