SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Location
    Bridgeport, Connecticut, United States
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Letter Length of an Array

    Alright guys so I have an array. umm.. example.

    var state = new Array();
    state[0] = "Alabama";
    state[1] = "ALASKA";
    state[2] = "Arizona";
    state[3] = "Arkansas";
    state[4] = "California";

    Im trying to find out the length of this array in letters. Im thinking something along the lines of "state.length" but that pulls up the number of arrays. I want the letter length. How would I go buy doing that? The assignment is to find the avg of the letter length. so in this case.

    state.length/5 <---even though this is deff wrong. But get the point?

    Any links or pointers would be very useful.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by PCLogick View Post
    Any links or pointers would be very useful.
    Join the array contents together, so that you can then take the length of that.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Mar 2012
    Location
    Bridgeport, Connecticut, United States
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did actually. Now i have:

    state[i]

    which is what created the loop. "i++" and so forth. But when i document.write state[i].length it gives me the length of each individual state rather than the sum.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Code javascript:
    function averageArrayLen(array) {
        return array.join('').length / array.length;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •