Can someone please explain this code?

A

amerar

Hi All,

I'm not good at Javascript, so I am trying to understand this small bit
of code:

var groups=document.$fm.category.options.length;
var group=new Array(groups);
for (i=0; i<groups; i++);
group=new Array();


It looks like line #1 is getting the length of a combo box on the page.
Line #2 then creates an array called 'group' that has max elements from
the value in #1.

What could line #3 & #4 be doing?

The array has already been created with the size........I'm unclear.

Thanks.
 
R

Randy Webb

(e-mail address removed) said the following on 1/30/2006 3:23 PM:
Hi All,

I'm not good at Javascript, so I am trying to understand this small bit
of code:

var groups=document.$fm.category.options.length;
var group=new Array(groups);
for (i=0; i<groups; i++);
group=new Array();


It looks like line #1 is getting the length of a combo box on the page.


Actually, its getting the length of the options collection of a select
element on the page.
Line #2 then creates an array called 'group' that has max elements from
the value in #1.

Yes, and the groups variable used for the length is unnecessary.
What could line #3 & #4 be doing?

Making each element of the group array a new array so that group is an
Array of Arrays.
 
T

Thomas 'PointedEars' Lahn

var groups=document.$fm.category.options.length;
var group=new Array(groups);
for (i=0; i<groups; i++);
group=new Array();

It looks like line #1 is getting the length of a combo box on the page.


Only roughly speaking. What really happens is that the `groups' variable is
declared and assigned the number of options of a certain `select' element
named `$fm' (or has a name that results from evaluation of the server-side
variable `fm') in the current (X)HTML document.
Line #2 then creates an array called 'group'

No, the `group' variable declared and assign a reference to a newly created
Array object.
that has max elements from the value in #1.

Wrong, see below.
What could line #3 & #4 be doing?

Almost nothing. I presume it is a typo or due to a misconception of the
author that the semicolon after the `for' statement is included; it stands
for an empty statement there, so that line 4 is not within the for-loop.
It should be

for (i=0; i < groups; i++)
{
group=new Array();
}
The array has already been created with the size [...]

Which is unnecessary, since JS arrays are of dynamic size by default; which
is error-prone, since it depends on the implementation if the array has one
numeric element with the value of `groups' or `groups' elements with the
value of `undefined'.

However, line 4 is not useless by itself if used in proper context (see
above). It then makes each element of the `group' array (group) an
array itself by assigning it a reference to a newly created Array object.
Maybe it was the author's intention to create a two-dimensional array.


PointedEars
 
T

Thomas 'PointedEars' Lahn

var groups=document.$fm.category.options.length;
var group=new Array(groups);
for (i=0; i<groups; i++);
group=new Array();

It looks like line #1 is getting the length of a combo box on the page.


Only roughly speaking. What really happens is that the `groups' variable is
declared and assigned the number of options of a certain `select' element
named `$fm' (or has a name that results from evaluation of the server-side
variable `fm') in the current (X)HTML document.
Line #2 then creates an array called 'group'

No, the `group' variable is declared and assigned a reference to a newly
created
Array object.
that has max elements from the value in #1.

Wrong, see below.
What could line #3 & #4 be doing?

Almost nothing. I presume it is a typo or due to a misconception of the
author that the semicolon after the `for' statement is included; it stands
for an empty statement there, so that line 4 is not within the for-loop.
It should be

for (var i = groups; i--;)
{
group = new Array();
}
The array has already been created with the size [...]

Which is unnecessary, since JS arrays are of dynamic size by default; which
is error-prone, since it depends on the implementation if the array has one
numeric element with the value of `groups' or `groups' elements with the
value of `undefined'.

However, line 4 is not useless by itself if used in proper context (see
above). It then makes each element of the `group' array (group) an
array itself by assigning it a reference to a newly created Array object.
Maybe it was the author's intention to create a two-dimensional array.


PointedEars
 
R

Richard Cornford

I'm not good at Javascript, so I am trying to understand
this small bit of code:

var groups=document.$fm.category.options.length;
var group=new Array(groups);
for (i=0; i<groups; i++);
group=new Array();


It looks like line #1 is getting the length of a combo
box on the page.


The length property of the - options - collection of a SELECT element
(there are no 'combo boxes' in HTML).
Line #2 then creates an array called 'group' that has
max elements from the value in #1.

It is not a maximum length. Javascript Arrays have a maximum length, but
it is the same for all arrays (and is quite big).
What could line #3

Spinning its wheels. A javascript - for - statement controls the
execution of a single statement. That single statement may be a block
statement, which may then contain numerous other statements, or it could
be any other valid statement in the language. The statement controlled
by this - for - loop is an empty statement, as define by the semicolon
following the closing parenthesis. This is almost certainly an error.
& #4 be doing?

Because the loop has uselessly executed by the time line 4 is executed
the - i - variable is equal to - groups -, and so a new element is added
to the - group - array at index - i - and a reference to a new Array
assigned as the value of that element.
The array has already been created with the size........I'm
unclear.

It is an attempt to make an array of arrays written by someone
unfamiliar with javascript, and inevitably unsuccessful as a result.

Richard.
 
A

amerar

Well, I did not write it. But since I am going to maintain it, do you
have a better suggestion?
 
Z

Zif

Well, I did not write it. But since I am going to maintain it, do you
have a better suggestion?

You haven't described what it should do, so we can only guess at that.
As written, it won't do what it appears it is supposed to do.

It seems to be an attempt to create an array of as many empty arrays as
the value returned by document.$fm.category.options.length.

The actual outcome will be an array called group that has a length of
groups+1. It will have a single element at index groups that is an
array - the others are all undefined. Because the single element is at
index groups, the length of the array will now be groups+1 (the length
of an array is always greater than the highest index).

Whether that serves any useful purpose is impossible to tell from the
snippet provided. It is possible that some later code is based on the
fact that the length is now groups+1, so 'fixing' that may cause other
errors.

Thomas has given you one suggestion, here's another:

var group = [];
var i = document.$fm.category.options.length;
while(i--){
group = [];
}


A more robust effort might be:

var group = [];
var o;
var i = ( (o = document.$fm)
&& (o = o.category )
&& (o = o.options )
&& (o = o.length )
);
while(i){
group[--i] = [];
}


But it seems rather pointless. Presumably after this an attempt will be
made to assign values to the empty arrays. The arrays could be created
then, making the above redundant.

If whatever script the posted snippet is part of actually works, that is
likely what is happening anyway.

But be warned - fixing apparently broken parts of 'working' code will
very likely cause errors elsewhere.
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top