Can someone please explain this code?

Discussion in 'Javascript' started by amerar, Jan 30, 2006.

  1. amerar

    amerar Guest

    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.
     
    amerar, Jan 30, 2006
    #1
    1. Advertisements

  2. amerar

    Randy Webb Guest

    said the following on 1/30/2006 3:23 PM:


    Actually, its getting the length of the options collection of a select
    element on the page.
    Yes, and the groups variable used for the length is unnecessary.
    Making each element of the group array a new array so that group is an
    Array of Arrays.
     
    Randy Webb, Jan 30, 2006
    #2
    1. Advertisements



  3. 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.
    No, the `group' variable declared and assign a reference to a newly created
    Array object.
    Wrong, see below.
    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();
    }
    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
     
    Thomas 'PointedEars' Lahn, Jan 30, 2006
    #3


  4. 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.
    No, the `group' variable is declared and assigned a reference to a newly
    created
    Array object.
    Wrong, see below.
    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();
    }
    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
     
    Thomas 'PointedEars' Lahn, Jan 30, 2006
    #4


  5. The length property of the - options - collection of a SELECT element
    (there are no 'combo boxes' in HTML).
    It is not a maximum length. Javascript Arrays have a maximum length, but
    it is the same for all arrays (and is quite big).
    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.
    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.
    It is an attempt to make an array of arrays written by someone
    unfamiliar with javascript, and inevitably unsuccessful as a result.

    Richard.
     
    Richard Cornford, Jan 30, 2006
    #5
  6. amerar

    amerar Guest

    Well, I did not write it. But since I am going to maintain it, do you
    have a better suggestion?
     
    amerar, Jan 31, 2006
    #6
  7. amerar

    Zif Guest

    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.
     
    Zif, Feb 1, 2006
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.