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

    Unhappy Subtracting won't work

    Code:
    <html>
    <script type="text/javascript">
    var x=200;
    function update()
    {
    document.getElementById('health').innerHTML=x;
    }
    function attack()
    {
    x=x-10;
    }
    if (x<10)
    {
    document.write("You Won")
    }
    </script>
    <body onload="update()">
    <div id="health">
    </div>
    <input type="button" value="Attack" onclick="attack()" />
    </body>
    </html>
    I don't see that x loses 10 what's the problem.

  • #2
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    When you click on the Attack button, attack is called. x is subtracted by 10, but the innerHTML has not been updated, nor have you checked if x is less tan 10.


  •  

    Posting Permissions

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