SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Length Property

    Use the array length property to display an opening greeting to the user indicating how many original colonies (elements in the array) there were.

    I am really confused how to use this property. I was trying to follow an example, but I just can't get it, please help.

    "There were thirteen original American colonies. They were Connecticut, Delaware, Georgia, Maryland, Massachusetts, New Hampshire, New Jersey, New York, North Carolina, South Carolina, Pennsylvania, Rhode Island and Virginia."

    <script type="text/javascript">
    place = new Array(
    "",
    "There were thirteen original American colonies.",
    "Massachusetts, New Hampshire, New Jersey, New York,",
    "Rhode Island and Virginia."
    );

    function main(){
    for (versNum = 1; verseNum < place.length; verseNum++){
    alert(verse(verseNum));
    alert(chorus());
    }

    function chorus(){
    var greeting = "
    }


    (verseNum){
    var greeting +=
    var greeting +=
    var greeting += place[verseNum];
    return greeting;
    }

    var colonies = new Array(14);

    colonies[1] = "Connecticut";
    colonies[2] = "Delaware";
    colonies[3] = "Georgia";
    colonies[4] = "Maryland";
    colonies[5] = "Massachusetts";
    colonies[6] = "New Hampshire";
    colonies[7] = "New Jersey";
    colonies[8] = "New York";
    colonies[9] = "North Carolina";
    colonies[10] = "South Carolina";
    colonies[11] = "Pennsylvania";
    colonies[12] = "Rhode Island";
    colonies[13] = "Virginia";

    alert("Hi there! Did you know there were 13 original American colonies? They were: ");
    for(i = 1; i <=13; i++){
    alert(i + ") " + colonies[i]);
    }
    } // end program

  2. #2
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var colonies = ['conn', 'del', 'geor', 'mary', 'mass', 'new hamp', 'new jer', 'new york', 'north c', 'south c', 'penn', 'rhd', 'vir'];
    
    for (var i=0, colony; colony=colonies[i]; i++) {
      alert(colony);
    }

  3. #3
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'd want to start your array at 0, then number to 12. Then your loop would become:

    Code:
    for (versNum = 1; verseNum < place.length; verseNum++){
    though temporarily, you might just want to try place.length + 1, but consistently using 0 to start your array index is a good idea not for just javascript, but other languages as well.

  4. #4
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The example I posted doesn't even use length. And it's faster.

  5. #5
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure posting a not-too-frequently seen loop statement without an explanation is such a good idea. so given the code:

    Code:
    var colonies = ['conn', 'del', 'geor', 'mary', 'mass', 'new hamp', 'new jer', 'new york', 'north c', 'south c', 'penn', 'rhd', 'vir'];
    Instead of individually declaring your elements and values, this format lets you declare everything in one fatal swoop. This saves space in the long run, making for faster downloads (the difference is somewhat minute in this case, but still it's a good idea to to get accustomed to things like this, it helps in the long run).

    Code:
    for (var i=0, colony; colony=colonies[i]; i++) {
      alert(colony);
    }
    So here is a bit advanced playoff of the for loop. The first segment initializes loop variables. In this case, it's initializing the counter variable AND a loop scope variable to make reading the code more manageable.

    The next segment is the condition to check against for stopping the loop. In this case, it tries to index the array element referenced to by i. If i is an invalid reference, then it can't assign a value to colony, and the loop breaks off. If it IS a valid reference, it's assigned to colony to be used within the loop.

    The last segment is just the standard loop increment, increases i so the loop check segment can see if it needs to break out or not.

  6. #6
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Technically, the fastest of all is somewhat the least readable, and this is known as Duff's Device.

    That essentially goes something like this:

    Code:
    var items = ['one', 'two', 'three'];
    do {
      var item = items[i];
    } while(--i);
    But really, who does that?

  7. #7
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    window.colonial= function(){
    	this.toArray= function(){
    		return ['Connecticut','Delaware','Georgia','Maryland','Massachusetts',
    		'New Hampshire','New Jersey','New York','North Carolina','South Carolina',
    		'Pennsylvania','Rhode Island','Virginia'];
    	}
    	this.toString= function(){
    		var L,Va;
    		var colonies= this.toArray();
    		L= colonies.length;
    		Va= colonies.pop();
    		return 'The original '+L+' colonies were '+
    		colonies.join(', ')+' and '+Va+'.';
    	}
    }
    // sample code for colonial object
    var colonies= new colonial();// creates and returns a new object

    alert(colonies) // returns a string
    alert(colonies.toArray()) // returns an array


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
  •