SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "for (a in b) {...}" generates errors!

    I have a function that counts the elements in an array or the properties and methods in an object, but it's intended for array element counting.
    Code:
    function count(array) {
      if (typeof array != "object" || !array) {
        alert("Bug: Object is of wrong type!")
        return 0;
      }
      var i = 0;
      for (index in array) {
        if (array[index]) i++;
        alert("Bug: element is undefined!")
      }
      return i;
    }
    First I want to see whether there is a simpler way of doing this. Partly because of that I have been experiencing problems after I have run this functions, error messages saying that "X is null or not an object", where X is either the object in question, when I have tried to access properties or methods of the object.

    For example:
    Code:
    function propChange(prop, e) {
      if (whichKey() == enterKey) {
        for (index in props) {
          if (!props[index].value 
                && (props[index].id != "txtp"
                //|| count(validSn)
                ))
              {
            props[index].focus();
            return;
          }
        }
        lastSn.focus();
      }
    }
    This function works as long as that line in the middle of the function is commented. But when I remove the double slashes so that the array validSn is counted, the script interpretter complains about props[index].focus being null or not an object. Yet validSn has nothing to do with props[index].focus, it's a completely different array! Why does the script interpretter behave like this? What's the actual problem? I really need to count that array but now I can't do it because I'm getting this error, which I don't understand why it's connected to the function call.

    Even other functions using for (a in b) {...} have caused me problems, which I have found out just recently and hence stopped to use those functions. Moreover I'm running IE7.

  2. #2
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you just trying to count the number of elements in the array?
    Code:
    var i = array.length;

  3. #3
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried that but I found it not working properly when the array contained string indexes.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,839
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Associative arrays don't have a length.

    The only reason I can think of for that extra line not working is that either the count function is not defined, the validSn variable is not defined, or there is some problem in the code inside of that function.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •