473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Can someone please explain this code?


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[i]=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.

Jan 30 '06 #1
6 1529
am****@iwc.net 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[i]=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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 30 '06 #2
am****@iwc.net wrote:
var groups=document.$fm.category.options.length;
var group=new Array(groups);
for (i=0; i<groups; i++);
group[i]=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[i]=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[i]) 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
Jan 30 '06 #3
am****@iwc.net wrote:
var groups=document.$fm.category.options.length;
var group=new Array(groups);
for (i=0; i<groups; i++);
group[i]=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[i] = 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[i]) 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
Jan 30 '06 #4
am****@iwc.net wrote:
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[i]=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.
Jan 30 '06 #5

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

Jan 31 '06 #6
Zif
am****@iwc.net wrote:
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[i] = [];
}
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.
--
Zif
Feb 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: BluDog | last post by:
Hi I am trying to test dynamically created controls, to do this i have added a placeholder to a WbForm and added the following code behind: Private Property Count() As Integer Get If...
5
by: klj_mcsd | last post by:
Let's say you set txtlastname1.visible = true Then DirectCast(Page.FindControl("txtLastName1"), TextBox).Visible = False Why when you check txtlastname1.visible it is equal to True?
1
by: Simon Windsor | last post by:
Hi I have just recevived this error could not write to hash-join temporary file: No space left on device Can someone please explain how I can stop this occuring. Whereis the hash-join...
5
by: garyusenet | last post by:
I understand that Point is an object used for storing co-ordinates, such as mouse location. I can't figure out what the e's do in the above example can someone explain please. Thanks, Gary.
2
by: hassruby | last post by:
Can someone pls help me with some validation that im having a few technical problems with in my program. First of all, I will explain to you a little about what my program is suppose to do. It...
8
by: Bart | last post by:
Could someone explain me what is wrong with this code ? I gives me a compile error: Error 1 Use of unassigned local variable 'fileStreamObject' C:\Documents and Settings\Bart\Local...
4
by: fatboySudsy | last post by:
Hi, I have constructed a client program that has given me some error codes that i just cannot see. I was wondering if a different set of eyes with much more experience than me could help me out. ...
1
by: td0g03 | last post by:
Hello, I am new to C and I am new to English. I not sure what palindromes mean. I don't know exactly what my teacher wants me to do. If someone could explain it to me in a different way that would be...
3
by: Aarti | last post by:
Hi, Can some one please explain why the output of this program is 15 #include <iostream> using namespace std; class A {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.