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 Coder
    Join Date
    Sep 2012
    Location
    Calgary, Alberta, Canada
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How to use a loop to repeat a sequence of numbers?

    I want to use javascript to create an HTML table that consists of 4 columns and a number of rows determined by a inputted value. Each column will have different code that will be associated with it. To do this, I will need to incorporate this all into a loop. I need a formula or other method to basically break the loop sequence numbers into instances of 1 - 4.

    i.e.:

    Loop Number / Instance Value

    1 / 1
    2 / 2
    3 / 3
    4 / 4
    5 / 1
    6 / 2
    7 / 2
    8 / 2
    9 / 3
    etc

    Can someone suggest a method for doing this please?

    Thanks.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I don't see the pattern. What happens after 9?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    I don't see the pattern or really understand the question either. Does this point the way?


    Code:
    <script type = "text/javascript">
    
    function showvals() {
    var num = Number(document.getElementById("rows").value) || 0;
    for (var i = 1; i<=num; i++) {
    for (var j = 1; j<=4; j++) {
    document.write(i*j + "&nbsp&nbsp&nbsp")
    }
    document.write ("<br><br>");
    }
    }
    
    </script>
    Of course, document.write() is not used in real applications. Just for the purpose of this demo.

    Quizmaster: Emmental and Double Gloucester are both types of what?
    Contestant: Banks.

    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.


  •  

    Posting Permissions

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