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 9 of 9
  1. #1
    New Coder
    Join Date
    Oct 2004
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to create a list of ascending numbers?

    Greetings

    I often need to create lists of ascending numbers, but doing it manually takes forever to type all the digits.
    so I wonder if there is a javascript to do that for me?

    For example, I need to create a text file:
    - containing numbers from 0820001 to 1149991
    - in increments of 10
    - with each number in a separate line
    - in ascending order:

    0820001
    0820011
    0820021
    ...etc.

    I deeply appreciate any help, many thanks in advance.

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    Yes it is possible and I have just create a function in JavaScript to help you out.

    Please note that you cannot use JavaScript to write to external files. If that is what you want to do, you should consider using PHP, ASP, or something else. In any case, the following function should give you an idea of how you may do this in PHP or ASP so that you can write to files.

    PHP Code:
    function generateNumbersfromtoincrement )
    {
        var 
    numbers = new Array();
        
        
    // start generating numbers
        
    for( var from<= toi+= increment )
        {
            
    numbers.push);
        }
        
        
    // print numbers out.
        
    for( var 0numbers.lengthi++ )
        {
            
    window.document.writenumbers[i] + "<br>" );
        }

    All you have to do is to call upon this function like so

    PHP Code:
    generateNumbers010); 
    the output, should be something like the following
    0
    2
    4
    6
    8
    10

    Good luck and all the best
    Last edited by ess; 11-01-2006 at 03:31 PM.

  • #3
    New Coder
    Join Date
    Oct 2004
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks so much for your great help.
    I've made this php file, but it gives me an error:
    PHP Code:
    <?php

    function generateNumbersfromtoincrement)
    {
        var 
    numbers = new Array();
        
        
    // start generating numbers
        
    for( var from<= toi+= increment )
        {
            
    numbers.push);
        }
        
        
    // print numbers out.
        
    for( var 0numbers.lengthi++ )
        {
            
    window.document.writenumbers[i] + "<br>" );
        }


    generateNumbers010);
    ?>
    the error message:

    Parse error: syntax error, unexpected T_STRING, expecting ')' in /home/~/number-list-creator.php on line 3

  • #4
    New Coder bigtiger's Avatar
    Join Date
    Oct 2005
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sallam View Post
    thanks so much for your great help.
    I've made this php file, but it gives me an error:
    PHP Code:
    <?php

    function generateNumbersfromtoincrement)
    {
        var 
    numbers = new Array();
        
        
    // start generating numbers
        
    for( var from<= toi+= increment )
        {
            
    numbers.push);
        }
        
        
    // print numbers out.
        
    for( var 0numbers.lengthi++ )
        {
            
    window.document.writenumbers[i] + "<br>" );
        }


    generateNumbers010);
    ?>
    the error message:

    Parse error: syntax error, unexpected T_STRING, expecting ')' in /home/~/number-list-creator.php on line 3
    hi, sallam, be aware that javascript and php have syntax different, you may need change something to do that in php. Have a look at this :

    PHP Code:
    function generateNumbers$from$to$increment)
    {
        
    $numbers = array();
        
        
    // start generating numbers
        
    for ( $i $from$i <= $to$i+= $increment )
        {
            
    numbers[] = $i;
        }
        
        
    // print numbers out.
        
    for ( $i 0$i count($numbers); $i++ )
        {
            echo 
    $numbers[i] . '<br>';
        }

    Hope this help

    P.S. As a reference, php always prefix variables with $ symbols

  • #5
    New Coder
    Join Date
    Oct 2004
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    many thanks indeed.
    I'm now getting a new error. Here is what I tried:
    PHP Code:
    <?php

    function generateNumbers$from$to$increment)
    {
        
    $numbers = array();
        
        
    // start generating numbers
        
    for ( $i $from$i <= $to$i+= $increment )
        {
            
    numbers[] = $i;
        }
        
        
    // print numbers out.
        
    for ( $i 0$i count($numbers); $i++ )
        {
            echo 
    $numbers[i] . '<br>';
        }


    generateNumbers010);
    ?>
    and this gives me an error message:
    Parse error: syntax error, unexpected '[' in /~/number-list-creator.php on line 10
    I mus be doing something wrong...

  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    missing $

  • #7
    New Coder
    Join Date
    Oct 2004
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actualy I did try it as javascript in an html page, and it worked!
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <
    HTML><HEAD></HEAD>

    <
    BODY>
      <
    script type="text/javascript">
    function 
    generateNumbersfromtoincrement )
    {
        var 
    numbers = new Array();
        
        
    // start generating numbers
        
    for( var from<= toi+= increment )
        {
            
    numbers.push);
        }
        
        
    // print numbers out.
        
    for( var 0numbers.lengthi++ )
        {
            
    window.document.writenumbers[i] + "<br>" );
        }

      
    </script>
      <script type="text/javascript">
    generateNumbers( 0, 10, 2 );  
      </script>
    </BODY>
    </HTML> 
    Its working here, thanks to you:
    http://www.gawaher.com/pages/number-list-creator.htm

    Can you help me add a form to enter the: 'from', 'to' and 'increment' values? That way, I can directly type the numbers in the form, instead of editing the file everytime I want a new list...

  • #8
    New Coder bigtiger's Avatar
    Join Date
    Oct 2005
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes, take a look this :

    PHP Code:
    <html>
    <
    head>
    <
    script type="text/javascript">
    function 
    generateNumbersfromtoincrement )
    {
        var 
    numbers = new Array();
        
        
    // start generating numbers
        
    for( var from<= toi+= increment )
        {
            
    numbers.push);
        }
        
        
    // print numbers out.
        /*
        for( var i = 0; i < numbers.length; i++ )
        {
            window.document.write( numbers[i] + "<br>" );
        }
        */
        
    return numbers.join('<br />');


    function 
    create_list()
    {
        var 
    from parseInt(document.forms[0].lst_from.value);
        var 
    to parseInt(document.forms[0].lst_to.value);
        var 
    increment parseInt(document.forms[0].lst_incrmt.value);
        
        
    document.getElementById('list').innerHTML generateNumbersfromtoincrement );
    }
    </script>
    </head>
    <body>
    <form>
    <p><span>From:</span></p>
    <input type="text" name="lst_from" value="" /><br />
    <p><span>To:</span></p>
    <input type="text" name="lst_to" value="" /><br />
    <p><span>Increment:</span></p>
    <input type="text" name="lst_incrmt" value="" /><br />
    <input type="button" name="builder" value="Build List" onclick="create_list()" /><br />
    <hr />
    <span id="list"></span>
    </form>
    </body>
    </html> 
    Hope this help,

  • #9
    New Coder
    Join Date
    Oct 2004
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks so much, this is beautiful. It works great!
    Many thanks indeed.


  •  

    Posting Permissions

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