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

Thread: Array Index

  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array Index

    If the items in an Array Index are numbered 0 - 7 ( dog[0] cat[1] bird[2] pony[3] etc.etc.) is there a way to do a document.write statement that will number them out beginning at 1? ( 1. dog 2. cat 3. bird 4. pony )

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    Simple enough. Just loop through the array and add one to the index when you print it.
    Code:
    var arr_test = new Array("dog","cat","bird","pony","fish","lizard","hamster");
    for(var i = 0; i < ar_test.length; i++)
      document.write((i+1) + ". " + arr_test[i] + " ");

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    A=Array("dog","cat","bird","pony","fox","fish","turtle")
    document.write('<ol>')
    for(i=0;i<A.length;i++)
    {
    document.write('<li>'+A[i]+'</li>')
    }
    document.write('</ol>')


  •  

    Posting Permissions

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