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 2005
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Extremely Simple Question

    What I want to know, is how to make a for loop go up by twos.

    FE, here is my code as it is, it is going up by ones.

    Code:
    <script type="text/javascript">
    var h=0
    for(h=0;h<=50;h++)
    {
    document.write("The magic number is " + h)
    document.write(".")
    document.write("<br>")
    }
    </script>
    I know it is controlled by the "h++" bit, unless I am missing something.

    Help would be appreciated.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    h++ adds 1 to h. To add 2 each time around change it to h+=2
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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