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 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    array.length not returning correct number of results

    Hello, another noob question for you: I have a function that requires the ability to count the number of entries in an array.

    I'm using the following to call my function inside an input tag:

    Code:
    onblur="javascript:check(this.name, this.value, 1, 10);"
    which for example is calling check('field1', 'foobar', 1, 10)

    Here is the javascript:

    Code:
    function check(name, value, min, max){
    
    	var errors=new Array();
    
    	if(value.length < min || value.length > max){ //checking against min/max length of value
    	 
      errors[name] = "Text field must not be blank.";
      errors["blabla"] = "array value 2"; //added for testing purposes
    
    alert(name+" : "+value); //returns "field1 : foobar" 
    
    }
    
    alert(errors.length); // returns "0"
    
    }
    And when errors.length is alerted, it outputs 0. I can only figure that there is an issue when using custom named keys in an array, since this works when I use only integers, however, I have the need to use a custom key index as shown. Any ideas?
    Last edited by predilezione; 11-10-2010 at 04:25 PM. Reason: incorrect subject heading

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    errors[name] = "Text field must not be blank.";

    The index of an array must be an integer number.

    Is this what you want:-

    Code:
    function check(name, value, min, max) {
    var errors=new Array();
    var count = 0;
    if(value.length < min || value.length > max){ //checking against min/max length of value
    errors[count] = "Text field " + name + " must not be blank.";
    count++;

    You should avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'test' or 'text' or 'checked' or 'go' or 'replace' or 'button' or 'radio' or 'parseInt'.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 11-10-2010 at 04:37 PM.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    errors[name] = "Text field must not be blank.";

    The index of an array must be an integer number.
    Technically correct, but also true that you *CAN* treat the [ ] operator as a key into a key/value collection.

    So, yes, you *can* do
    Code:
    errors["foo"] = "Text field must not be blank.";
    And then later you can do
    Code:
    alert( errors["foo"] );
    But adding a key/value pair to the collection does not reflect in the length property, as length applies to the array, not to the collection.

    Is it confusing? Damned straight.

    Maybe this will help confuse thing further. Or not:
    Code:
    <script type="text/javascript">
    var foo = [];
    foo["bar"] = "foobar";
    alert( "foo[bar] is " + foo["bar"] + ", length is " + foo.length );
    
    foo[3] = "three";
    alert( "foo[3] is " + foo[3] + ", length is " + foo.length );
    alert( "foo[bar] is " + foo["bar"] + ", length is " + foo.length );
    
    foo["apple"] = "banana";
    alert( "foo[apple] is " + foo["apple"] + ", length is " + foo.length );
    alert( "foo[3] is " + foo[3] + ", length is " + foo.length );
    alert( "foo[bar] is " + foo["bar"] + ", length is " + foo.length );
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    And as a *VERY* minor point, you can avoid keeping track of the "count" by coding *EITHER*:
    Code:
    function check(name, value, min, max) {
        var errors=new Array();
        if(value.length < min || value.length > max){ //checking against min/max length of value
            errors[errors.length] = "Text field " + name + " must not be blank.";
       }
    *OR*
    Code:
    function check(name, value, min, max) {
        var errors=new Array();
        if(value.length < min || value.length > max){ //checking against min/max length of value
            errors.push( "Text field " + name + " must not be blank." );
       }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by Old Pedant View Post
    Technically correct, but also true that you *CAN* treat the [ ] operator as a key into a key/value collection.
    Yes, perfectly correct, but it was another noob question. I feel that your comments are too advanced/confusing for this user.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Quote Originally Posted by Philip M View Post
    Yes, perfectly correct, but it was another noob question. I feel that your comments are too advanced/confusing for this user.
    Normally, I'd agree, but he ended his post saying
    however, I have the need to use a custom key index as shown. Any ideas?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    @Old Pedant and Philip M, you guys must be the gurus around here. I very much appreciate your help and welcome the ideas of your 'advanced' code, otherwise I won't learn, and I don't want to be such a novice forever.

    And as a note, it's not the theories of javascript which confuse me, as coming from php they make perfect sense, but rather the idiosyncrasies and syntax between the two which confounds me, which I'm thankful sometime takes the time to help me iron out.

    I'll try your snippets out and let you know how it works. Thanks again for your replies.
    Last edited by predilezione; 11-11-2010 at 10:40 AM.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    the idiosyncrasies ... confounds me
    Huh. JS has enough of those! Look at the long discussion/argument that Philip and I had last couple of days re Array.sort( )!! He thinks the JS way is natural, I think it's counterintuitive..
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Instead of messing around and looping with .length I went ahead and took this from W3C:

    Code:
      for (x in errors)
      {
    
      document.write(errors[x] + "<br />");
    
      }
    Which works just fine. Thanks for the suggestions.

  • #10
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    the for … in loop is meant to loop through objects, although you can get "surprising" resuts with it.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #11
    Regular Coder
    Join Date
    Sep 2010
    Location
    Virginia
    Posts
    112
    Thanks
    11
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by predilezione View Post
    @Old Pedant and Philip M, you guys must be the gurus around here. I very much appreciate your help and welcome the ideas of your 'advanced' code, otherwise I won't learn, and I don't want to be such a novice forever.

    And as a note, it's not the theories of javascript which confuse me, as coming from php they make perfect sense, but rather the idiosyncrasies and syntax between the two which confounds me, which I'm thankful sometime takes the time to help me iron out.

    I'll try your snippets out and let you know how it works. Thanks again for your replies.
    Javascript is just annoying and it takes a while to get used to...coming from any language. It's best to just download the emcascript language spec so you can understand how the language works completely.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    LOL! Tell you what, if you can tell how *ANY* language will work in real life by just reading its specification, you're a much better man than I am, Gunga Din!

    The way I learned the C language, back in 1979, was this: Our boss (in a small consulting shop...5 programmers, including him) came in one day and plunked a copy of the original K&R C manual (spec, basically) on our desks and said "we're going to write a compiler for this language." None of us had ever used C before.

    Trust me, we read that doc from cover to cover several times, and *STILL* we had to change various ways the compiler worked, in order to make the language do what people expected. And still, years later, I find out things about C that I didn't know, even though we created a compiler for it. (And a pretty good one, for the time!)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by Dormilich View Post
    the for … in loop is meant to loop through objects, although you can get "surprising" resuts with it.
    According to w3schools "The for...in statement loops through the elements of an array or through the properties of an object."

    Which is what I have always understood.

  • #14
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by Philip M View Post
    According to w3schools "The for...in statement loops through the elements of an array or through the properties of an object."
    the elements of an array are properties of an object (an Array object instance). besides that, the ECMAScript does not specify any special rules for arrays. it states (section 12.6.4, in short): for … in loops through enumerable object properties.


    PS. w3schools does make learning JavaScript easy, but that does not mean its descriptions always follow the specs (normally, you either have to choose correctness or perspicuity).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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