...

View Full Version : array.length not returning correct number of results



predilezione
11-10-2010, 03:24 PM
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:


onblur="javascript:check(this.name, this.value, 1, 10);"

which for example is calling check('field1', 'foobar', 1, 10)

Here is the javascript:


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?

Philip M
11-10-2010, 03:31 PM
errors[name] = "Text field must not be blank.";

The index of an array must be an integer number.

Is this what you want:-


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.

Old Pedant
11-10-2010, 07:56 PM
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

errors["foo"] = "Text field must not be blank.";

And then later you can do

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:

<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>

Old Pedant
11-10-2010, 07:59 PM
And as a *VERY* minor point, you can avoid keeping track of the "count" by coding *EITHER*:


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*


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." );
}

Philip M
11-10-2010, 08:01 PM
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.

Old Pedant
11-10-2010, 08:10 PM
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?

predilezione
11-11-2010, 09:38 AM
@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.

Old Pedant
11-11-2010, 07:00 PM
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..

predilezione
11-14-2010, 07:48 PM
Instead of messing around and looping with .length I went ahead and took this from W3C:


for (x in errors)
{

document.write(errors[x] + "<br />");

}

Which works just fine. Thanks for the suggestions.

Dormilich
11-14-2010, 08:53 PM
the for … in loop is meant to loop through objects, although you can get "surprising" resuts with it.

Afro_Programmer
11-14-2010, 09:07 PM
@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.

Old Pedant
11-14-2010, 09:55 PM
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!)

Philip M
11-15-2010, 08:38 AM
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.

Dormilich
11-15-2010, 09:20 AM
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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum