SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot superbrava's Avatar
    Join Date
    Aug 2001
    Location
    Indianapolis, IN
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how would one count the elements in an array to use in a while loop?

    my question is this.. i know in php, you can do something like this..

    PHP Code:
    $var count($array); 
    but i dont know how to get javascript to count how many elements there are in the array so i can print them, without having to reset the number.

    any ideas?
    Steve v3.2.2 running on LocalSuck port 80

  2. #2
    SitePoint Zealot superbrava's Avatar
    Join Date
    Aug 2001
    Location
    Indianapolis, IN
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    here is my code

    Code:
    	function createNewProjects() {
    
    		// declare array values for projects
    			var list_projects = new Array();
    
    			list_projects[1] = "headline 1";
    			list_projects[2] = "headline 2";
    			list_projects[3] = "headline 3";
    			list_projects[4] = "headline 4";
    			list_projects[5] = "headline 5";
    
    			var arrLength = 5;
    
    			document.write("<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\" width=\"100%\" class=\"theme\"><tr><td class=\"header\">:: Current Projects</td></tr>");
    
    			var i = 1;
    			while (i <= arrLength) {
    				if (i%2) {
    					var bgcolor = "#FFFFFF";
    					document.write("<tr><td bgcolor=" + bgcolor + " class=\"small\"><b>" + i + "</b> " + list_projects[i] + "</td></tr>");
    				} else {
    					var bgcolor = "#F6F6F6";
    					document.write("<tr><td bgcolor=" + bgcolor + " class=\"small\"><b>" + i + "</b> " + list_projects[i] + "</td></tr>");
    				}					
    			i++;
    			}
    
    			document.write("</table>");
    
    	}
    i want to take this part

    Code:
    			var arrLength = 5;
    and make it count the elements in the array automatically, instead of me having to edit the number. I dont know the syntax for this. any ideas?
    Steve v3.2.2 running on LocalSuck port 80

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, what about list_projects.length ?
    Note though that the way you have it now the value would be 6 because the first element of the array is list_projects[0]

  4. #4
    SitePoint Zealot superbrava's Avatar
    Join Date
    Aug 2001
    Location
    Indianapolis, IN
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that worked..
    but i have to do this list_projects.length-1
    Steve v3.2.2 running on LocalSuck port 80

  5. #5
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    use .length

    try this

    fruit=new Array(apple, orange, banana, sensi)
    alert(fruit.length)


Bookmarks

Posting Permissions

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