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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Why won't this work?

    Hello again,

    I have written the following code:


    <script language="Javascript">
    var win=0
    </script>
    <Form name="Form">
    <Input type="button" name="button" value="Click" onClick="Good()">
    </form>
    <script language="Javascript">
    function Good()
    {
    if (win=0)
    {
    alert('You win!')
    }
    else
    {
    alert("You lose!")
    }
    }
    </script>

    Based on the following, when I click the button it should say "You win!" as win=0 but it keeps saying "You lose!". Why is this happening?

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,915
    Thanks
    56
    Thanked 545 Times in 542 Posts
    Code:
    if (win==0)

  • Users who have thanked xelawho for this post:

    amster (12-22-2011)

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks a lot! Silly me.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Ok thanks! One other question now. I expanded it to this:


    <script language="Javascript">
    var win=0
    setTimeout("Bad()",4000)
    </script>
    <Form name="Form">
    <Input type="button" name="button" value="Click" onClick="Good()">
    </form>
    <script language="Javascript">
    function Good()
    {
    if (win==0)
    {
    alert('You win!')
    }
    else
    {
    alert("You lose!")
    }
    }
    ;
    function Bad()
    {win==1
    }
    </script>

    Based on the following, I wanted it to say "I win" after clicking the button but if I click the button after 3 seconds, it says "I lose!". What is wrong here as it just keeps telling me "I win!" when I click the button!

    EDIT: I've seemed to have solved my problem. When I made it = instead of ==, it worked. So when exactly would I use == or when would I use =?
    Last edited by amster; 12-22-2011 at 07:05 PM.

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,915
    Thanks
    56
    Thanked 545 Times in 542 Posts
    I don't think you have grasped the difference between = and ==

    you need to know this.

    And once you have changed the value of your variable "win" you need to call the function again to test it...

    Code:
    <script type = "text/javascript">
    var win=0
    setTimeout("Bad()",4000)
    </script>
    <Form name="Form">
    <Input type="button" name="button" value="Click" onClick="Good()">
    </form>
    <script language="Javascript">
    function Good()
    {
    if (win==0)
    {
    alert('You win!')
    }
    else
    {
    alert("You lose!")
    }
    }
    ;
    function Bad()
    {win=1
    Good();
    }
    </script>
    I don't mean to be rude, but can I suggest some basic tutorials like http://www.w3schools.com/js/ ? Learning the way you are doing it will take a very long time...

  • #7
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Oh not rude at all. I'm actually looking at those tutorials as I go haha. I just sometimes go on tangents and get distracted and start trying other stuff.


  •  

    Posting Permissions

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