array undefined error

J

JC

$(document).ready(function(){

var customerArray =
[["Vodafone","#ff9900","Internal"],
["Bharti","#acc","External"],
["","",""],
["Wimax","#ccc","Internal"],
["AzZ","#454787","Vendor"],
["Automation","#ddd","City"],
["Vodafone","#ff9900","Internal"],
["Bharti","#acc","External"],
["Wimax","#ccc","Internal"],
["AzZ","#454787","Vendor"],
["Automation","#ddd","City"],
["Vodafone","#ff9900","Internal"],
["Bharti","#acc","External"],
["Wimax","#ccc","Internal"],
["AzZ","#454787","Vendor"],
["Automation","#ddd","City"],
["Vodafone","#ff9900","Internal"]];
document.write('<div style="width: 960px; height: auto;">')


for (var i=0; i<=customerArray.length; i++) {
document.write('<div class="div_box_ie" style="background-
color:'+ customerArray[1] +'; width: 80px; height: 50px; float:
left; border:1px solid #ddd; font: 12px Bold Trebuchet MS; padding:
5px;">'+ customerArray[0] + "<br/>" + customerArray[2] +'</
div>');
}

document.write('</div>');

});

when I run this snippet .

Iam getting the JS error like ' customerArray is undefined. Could
anyone help me on this ?

Thanks,
JC
 
J

J.R.

$(document).ready(function(){

Stop right here and think. Do you really need JQuery or whatever library
to run such a script?!?!?
for (var i=0; i<=customerArray.length; i++) {

when I run this snippet .
Iam getting the JS error like ' customerArray is undefined. Could
anyone help me on this ?


The 'i' variable cannot be less or equal (<=) the array.length. Use:
'i < customerArray.length. I'd rather optimise the loop storing the
array.length just once:

for (var i = 0, len = customerArray.length; i < len; i++) {
//
}
 
E

Evertjan.

J.R. wrote on 16 mrt 2011 in comp.lang.javascript:
$(document).ready(function(){

Stop right here and think. Do you really need JQuery or whatever library
to run such a script?!?!?
for (var i=0; i<=customerArray.length; i++) {

when I run this snippet .
Iam getting the JS error like ' customerArray is undefined. Could
anyone help me on this ?


The 'i' variable cannot be less or equal (<=) the array.length.


Yes it can.

Rethink the "or".
 
D

Denis McMahon

J.R. wrote on 16 mrt 2011 in comp.lang.javascript:
$(document).ready(function(){

Stop right here and think. Do you really need JQuery or whatever
library to run such a script?!?!?
for (var i=0; i<=customerArray.length; i++) {

when I run this snippet .
Iam getting the JS error like ' customerArray is undefined. Could
anyone help me on this ?


The 'i' variable cannot be less or equal (<=) the array.length.


Yes it can.

Rethink the "or".


What he means is that to cycle through an array, the array index should
run from 0 to array.length - 1, as arrays are zero indexed.

So for an array a[] of 5 elements, they are a[0], a[1], a[2], a[3], a[4];
to loop through all elements, you use an index of 0 .. 4, not 1 .. 5 or
0 .. 5.

Rgds

Denis McMahon
 
E

Evertjan.

Denis McMahon wrote on 16 mrt 2011 in comp.lang.javascript:
J.R. wrote on 16 mrt 2011 in comp.lang.javascript:
On 16/03/2011 10:08, JC wrote:
$(document).ready(function(){

Stop right here and think. Do you really need JQuery or whatever
library to run such a script?!?!?

for (var i=0; i<=customerArray.length; i++) {

<snip>
when I run this snippet .
Iam getting the JS error like ' customerArray is undefined.
Could anyone help me on this ?

The 'i' variable cannot be less or equal (<=) the array.length.


Yes it can.

Rethink the "or".


What he means is that to cycle through an array, the array index
should run from 0 to array.length - 1, as arrays are zero indexed.

So for an array a[] of 5 elements, they are a[0], a[1], a[2], a[3],
a[4]; to loop through all elements, you use an index of 0 .. 4, not 1
.. 5 or 0 .. 5.


People are known to mean other things than they say.
Wouldn't it be nice if they did mean what they said.

In technical NG's like this one, however, it is better to discuss what
they said than what someone else concludes that they ment but not said.

for (var i=0; i<=20; i++)
alert(i);

The fact that the value of 20 is never reached does NOT mean
that "the 'i' variable cannot be less or equal (<=) 20",
as it truely can for all the loop values between 0 and 19 inclusive.
 
D

Denis McMahon

Denis McMahon wrote on 16 mrt 2011 in comp.lang.javascript:
J.R. wrote on 16 mrt 2011 in comp.lang.javascript:

On 16/03/2011 10:08, JC wrote:
$(document).ready(function(){

Stop right here and think. Do you really need JQuery or whatever
library to run such a script?!?!?

for (var i=0; i<=customerArray.length; i++) {

<snip>
when I run this snippet .
Iam getting the JS error like ' customerArray is undefined. Could
anyone help me on this ?

The 'i' variable cannot be less or equal (<=) the array.length.

Yes it can.

Rethink the "or".


What he means is that to cycle through an array, the array index should
run from 0 to array.length - 1, as arrays are zero indexed.

So for an array a[] of 5 elements, they are a[0], a[1], a[2], a[3],
a[4]; to loop through all elements, you use an index of 0 .. 4, not 1
.. 5 or 0 .. 5.


People are known to mean other things than they say. Wouldn't it be nice
if they did mean what they said.

In technical NG's like this one, however, it is better to discuss what
they said than what someone else concludes that they ment but not said.

for (var i=0; i<=20; i++)
alert(i);

The fact that the value of 20 is never reached does NOT mean that "the
'i' variable cannot be less or equal (<=) 20", as it truely can for all
the loop values between 0 and 19 inclusive.


Er, in your example the value of 20 *IS* reached.

The OPs problem was that they were using:

for (var i=0; i<=customerArray.length; i++) {
// do something with customerArray
}

which on the last cycle through the loop, attempted to access a non
existent array element.

JR responded with

"The 'i' variable cannot be less or equal (<=) the array.length."

which in this context is an accurate statement of the problem, even if
different people with different understandings of English don't always
recognise it as such.

It seems to me that JR means that when iterating through an array with a
loop, with javascript, as is being done here, the index variable being
used to access the array contents should go from 0 to array.length - 1,
whereas the loop control statement as written by the OP allows the index
variable that is being used to go 1 higher, which then points at an
undefined array element, causing the OPs error.

You obviously chose to interpret a different meaning to JR's comment, and
have for whatever reason picked up specifically on the use of the term
"or" in the phrase "less than or equal to". I don't know why you've done
this, perhaps you're more interested in picking holes in JR's comment
than you are in helping the OP.

However, the case remains that the OP has written their code in a way
that attempts to read past the last member of the array, and the reason
for this is the use of a construct similar to "idx <= array.length"
instead of "idx < array.length" as the terminating constraint of their
for loop.

Rgds

Denis McMahon
 
J

J.R.

The OPs problem was that they were using:

for (var i=0; i<=customerArray.length; i++) {
// do something with customerArray
}

which on the last cycle through the loop, attempted to access a non
existent array element.

JR responded with

"The 'i' variable cannot be less or equal (<=) the array.length."

which in this context is an accurate statement of the problem, even if
different people with different understandings of English don't always
recognise it as such.

It seems to me that JR means that when iterating through an array with a
loop, with javascript, as is being done here, the index variable being
used to access the array contents should go from 0 to array.length - 1,
whereas the loop control statement as written by the OP allows the index
variable that is being used to go 1 higher, which then points at an
undefined array element, causing the OPs error.

You obviously chose to interpret a different meaning to JR's comment, and
have for whatever reason picked up specifically on the use of the term
"or" in the phrase "less than or equal to". I don't know why you've done
this, perhaps you're more interested in picking holes in JR's comment
than you are in helping the OP.

However, the case remains that the OP has written their code in a way
that attempts to read past the last member of the array, and the reason
for this is the use of a construct similar to "idx<= array.length"
instead of "idx< array.length" as the terminating constraint of their
for loop.


Denis, thank you very much indeed for this brilliant explanation on the
issue.

I thought that everyone could understand the meaning of the
*Less-than-or-equal Operator (<=)* as defined in ECMA 262 - 3rd ed., 11.8.3.
 
J

J.R.

J.R. wrote on 16 mrt 2011 in comp.lang.javascript:
for (var i=0; i<=customerArray.length; i++) {

when I run this snippet .
Iam getting the JS error like ' customerArray is undefined. Could
anyone help me on this ?


The 'i' variable cannot be less or equal (<=) the array.length.


Yes it can.

Rethink the "or".


*Less than _or_ equal" (<=) Operator as defined in ECMA 262 - 3rd ed.,
11.8.3.
 
E

Evertjan.

J.R. wrote on 17 mrt 2011 in comp.lang.javascript:
J.R. wrote on 16 mrt 2011 in comp.lang.javascript:
for (var i=0; i<=customerArray.length; i++) {

<snip>
when I run this snippet .
Iam getting the JS error like ' customerArray is undefined. Could
anyone help me on this ?

The 'i' variable cannot be less or equal (<=) the array.length.


Yes it can.

Rethink the "or".


*Less than _or_ equal" (<=) Operator as defined in ECMA 262 - 3rd ed.,
11.8.3.


Yes, and?
 
T

Thomas 'PointedEars' Lahn

J.R. said:
The OPs problem was that they were using:

for (var i=0; i<=customerArray.length; i++) {
// do something with customerArray
}

which on the last cycle through the loop, attempted to access a non
existent array element.

JR responded with

"The 'i' variable cannot be less or equal (<=) the array.length."

which in this context is an accurate statement of the problem, even if
different people with different understandings of English don't always
recognise it as such.

It seems to me that JR means that when iterating through an array with a
loop, with javascript, as is being done here, the index variable being
used to access the array contents should go from 0 to array.length - 1,
whereas the loop control statement as written by the OP allows the index
variable that is being used to go 1 higher, which then points at an
undefined array element, causing the OPs error.

You obviously chose to interpret a different meaning to JR's comment, and
have for whatever reason picked up specifically on the use of the term
"or" in the phrase "less than or equal to". I don't know why you've done
this, perhaps you're more interested in picking holes in JR's comment
than you are in helping the OP.


Your logic is flawed. You have already acknowledged the ambiguity of
natural language a paragraph before. Therefore, it is a fallacy to
insinuate malevolence when someone interpretes a word or phrase differently
than other people. In particular, the use of "cannot" here was bound to
lead to a misunderstanding.
However, the case remains that the OP has written their code in a way
that attempts to read past the last member of the array, and the reason
for this is the use of a construct similar to "idx<= array.length"
instead of "idx< array.length" as the terminating constraint of their
for loop.

[…]
I thought that everyone could understand the meaning of the
*Less-than-or-equal Operator (<=)* as defined in ECMA 262 - 3rd ed.,
11.8.3.

Probably everyone in this thread did. It was your choice of words that was
unfortunate and lead to a misunderstanding. Try to be more precise (like
"may not" or "must not" instead of "cannot") next time. For the value *can*
be "less or equal (<=) the array.length", it is just not wise to do so (in
this instance).


PointedEars
 
J

J.R.

J.R. wrote on 17 mrt 2011 in comp.lang.javascript:
J.R. wrote on 16 mrt 2011 in comp.lang.javascript:

for (var i=0; i<=customerArray.length; i++) {

<snip>
when I run this snippet .
Iam getting the JS error like ' customerArray is undefined. Could
anyone help me on this ?

The 'i' variable cannot be less or equal (<=) the array.length.

Yes it can.

Rethink the "or".


*Less than _or_ equal" (<=) Operator as defined in ECMA 262 - 3rd ed.,
11.8.3.


Yes, and?



And you are not serious, are you? Stick to the context of the OP’s
problem and you will understand my first response to the OP:

for (var i=0; i<=customerArray.length; i++) {
// <snip>
}

The OP wrote:
"When I run this snippet, I am getting the JS error like '
customerArray is undefined. Could anyone help me on this?"

Therefore, I ask you two questions:
a) Why did the OP's code return 'undefined'?
b) Are you helping the OP?
 
D

Denis McMahon

Denis McMahon wrote on 16 mrt 2011 in comp.lang.javascript:


It is NOT.

Using the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<title>Javascript Loop</title>
</head>
<body>
<pre>
=====
<script type="text/javascript">
var i;
for (i = 0; i <= 20; i++) document.write(i + "\n");
</script>
=====
</pre>
</body>
</html>

My browser generates:

=====
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

=====

So yes, the value of 20 *IS* most definitely reached using the form of
the loop control that you posted.

Maybe you meant to post "(var i=0; i<20; i++)", but what you actually
posted was "(var i=0; i<=20; i++)".

Using <= means it runs the loop when "i==20" just as it does when "i<20".

Rgds

Denis McMahon
 
C

Captain Paralytic

Using the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<title>Javascript Loop</title>
</head>
<body>
<pre>
=====
<script type="text/javascript">
var i;
for (i = 0; i <= 20; i++) document.write(i + "\n");
</script>
=====
</pre>
</body>
</html>

My browser generates:

=====
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

=====

So yes, the value of 20 *IS* most definitely reached using the form of
the loop control that you posted.

Maybe you meant to post "(var i=0; i<20; i++)", but what you actually
posted was "(var i=0; i<=20; i++)".

Using <= means it runs the loop when "i==20" just as it does when "i<20".
Surely not. "i<20" will not run the loop when i==20, because at that
stage i is not less than 20.

However whilst when using
for (i = 0; i < 20; i++) {...}
the loop will not run when i==20, the exit value of i WILL be 20.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
Denis McMahon wrote on 16 mrt 2011 in comp.lang.javascript:


It is NOT.

It is, when using for (var i=0; i<=20; i++) . In fact, the value 21 is
reached, but is not used within the loop.

Try actually executing
A = []
for (var i=0; i<=20; i++) A.push(i)
alert(i + "\n" + A.join(" "));

If 20 represents the length of the an array, the limit expression should
normally be i<20 not i<= 20.
 
D

Denis McMahon

Surely not. "i<20" will not run the loop when i==20, because at that
stage i is not less than 20.

OK, I see what confused you.

This:

can be expanded as: If you use "a<=b" as the test in a for loop, the code
inside the loop will be executed when a is equal to b (a==b), or [ii]
when a is less than b (a<b).

or reduced to: "( a <= b )" === "( ( a == b ) || ( a < b ) )"

Evertjan posted code using the form "a<=b" in the control condition test,
and asserted that the loop would not run with "a==b". I am stating that
he is wrong, and that if the test used is "a<=b", then it holds true for
either of "a<b" or "a==b"
However whilst when using
for (i = 0; i < 20; i++) {...}
the loop will not run when i==20, the exit value of i WILL be 20.

I didn't say otherwise.

Rgds

Denis McMahon
 
E

Evertjan.

Dr J R Stockton wrote on 17 mrt 2011 in comp.lang.javascript:
In comp.lang.javascript message
Denis McMahon wrote on 16 mrt 2011 in comp.lang.javascript:


It is NOT.

It is, when using for (var i=0; i<=20; i++) . In fact, the value 21
is reached, but is not used within the loop.

Try actually executing
A = []
for (var i=0; i<=20; i++) A.push(i)
alert(i + "\n" + A.join(" "));

If 20 represents the length of the an array, the limit expression
should normally be i<20 not i<= 20.

You are right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top