Adding up combinations with select-multiple boxes

Discussion in 'Javascript' started by Ben Hallert, Jan 21, 2005.

  1. Ben Hallert

    Ben Hallert Guest

    Hi guys,

    I'm trying to figure out what bone headed mistake I made on something I
    put together. I've got a form (named 'context') that has a variable
    number of select-multiple inputs on it. Based on the number of
    variables passed through a GET string, I want to multiply the total
    number of selected items for each together to see how many possible
    combinations the selected items are generating.

    The following snippet of code succesfully gets the values from the GET
    statement to count the number of items being passed (I know, it assumes
    the variable will always be in the same place, I'll deal with that
    later), then it's supposed to loop through all elements in the page.
    If the item it finds is a select-multiple input (up to this point, it
    works. It finds each select-multiple and alerts in debugging), it's
    supposed to do a loop inside that element to count how many are
    checked, then multiply it against the running total (which is named
    predictioncount). I expect the number for predictioncount to be 0
    until the last field has an entry in it, but it never stops being
    anything BUT zero.

    I'm no Javascript expert, and my kludgy code shows it, but I think I'm
    close to this functioning and I'm hoping someone here will see and say
    "Oh, you got the standard whatsit confused with the thingy, just change
    character x to y and it should work".

    Thanks in advance for any assistance!

    function trackselections()
    {
    //We'll get the variable passed, then get the name of it. If the item
    is selected, we add one to the item. If not, we subtract it
    //This lets us map out what will be submitted and calculate complexity
    based on that.
    var num = document.context.length;
    var count=0;
    var predictioncount = 0;
    var testunit_count = 0;
    //First, count how many testcase_ids are passed.
    var getstring = location.search.substr(1);
    var testunit_ids = 0;
    count = getstring.search(/\&/);
    getstring = getstring.substr(13,count-13);
    testunit_ids = getstring.split(/%2C/g);
    testunit_count = testunit_ids.length;

    //Next, count all inputs of of the type select-multiple and loop
    through them,
    //collecting info on how many items are checked

    for (var i = 0; i < num; i++)
    {
    if (document.context.type == 'select-multiple')
    {
    count = 0;
    for (var o = 0; o< document.context.length; o++)
    {
    if (document.context.id[o].selected) count++;
    alert(count);
    }
    predictioncount = predictioncount * count;
    }
    alert(predictioncount);
    }

    }
     
    Ben Hallert, Jan 21, 2005
    #1
    1. Advertising

  2. Ben Hallert

    Lee Guest

    Ben Hallert said:
    >
    >It finds each select-multiple and alerts in debugging), it's
    >supposed to do a loop inside that element to count how many are
    >checked, then multiply it against the running total (which is named
    >predictioncount). I expect the number for predictioncount to be 0
    >until the last field has an entry in it, but it never stops being
    >anything BUT zero.


    If you start with zero, and keep multiplying that value by any
    other numbers, you will always end up with zero.

    If I understand you correctly, you want to count the number of
    selected items and THEN multiply that number by the number of arguments.
     
    Lee, Jan 21, 2005
    #2
    1. Advertising

  3. Ben Hallert

    McKirahan Guest

    "Ben Hallert" <> wrote in message
    news:...
    > Hi guys,
    >
    > I'm trying to figure out what bone headed mistake I made on something I
    > put together. I've got a form (named 'context') that has a variable
    > number of select-multiple inputs on it. Based on the number of
    > variables passed through a GET string, I want to multiply the total
    > number of selected items for each together to see how many possible
    > combinations the selected items are generating.
    >
    > The following snippet of code succesfully gets the values from the GET
    > statement to count the number of items being passed (I know, it assumes
    > the variable will always be in the same place, I'll deal with that
    > later), then it's supposed to loop through all elements in the page.
    > If the item it finds is a select-multiple input (up to this point, it
    > works. It finds each select-multiple and alerts in debugging), it's
    > supposed to do a loop inside that element to count how many are
    > checked, then multiply it against the running total (which is named
    > predictioncount). I expect the number for predictioncount to be 0
    > until the last field has an entry in it, but it never stops being
    > anything BUT zero.
    >
    > I'm no Javascript expert, and my kludgy code shows it, but I think I'm
    > close to this functioning and I'm hoping someone here will see and say
    > "Oh, you got the standard whatsit confused with the thingy, just change
    > character x to y and it should work".
    >
    > Thanks in advance for any assistance!
    >
    > function trackselections()
    > {
    > //We'll get the variable passed, then get the name of it. If the item
    > is selected, we add one to the item. If not, we subtract it
    > //This lets us map out what will be submitted and calculate complexity
    > based on that.
    > var num = document.context.length;
    > var count=0;
    > var predictioncount = 0;
    > var testunit_count = 0;
    > //First, count how many testcase_ids are passed.
    > var getstring = location.search.substr(1);
    > var testunit_ids = 0;
    > count = getstring.search(/\&/);
    > getstring = getstring.substr(13,count-13);
    > testunit_ids = getstring.split(/%2C/g);
    > testunit_count = testunit_ids.length;
    >
    > //Next, count all inputs of of the type select-multiple and loop
    > through them,
    > //collecting info on how many items are checked
    >
    > for (var i = 0; i < num; i++)
    > {
    > if (document.context.type == 'select-multiple')
    > {
    > count = 0;
    > for (var o = 0; o< document.context.length; o++)
    > {
    > if (document.context.id[o].selected) count++;
    > alert(count);
    > }
    > predictioncount = predictioncount * count;
    > }
    > alert(predictioncount);
    > }
    >
    > }
    >


    I think you want to replace
    if (document.context.id[o].selected) count++;
    with this
    if (document.context.options[o].selected) count++;

    I don't think you want this:
    predictioncount = predictioncount * count;
    don't you mean
    predictioncount = predictioncount +count;
    or better yet:
    predictioncount += count;
     
    McKirahan, Jan 21, 2005
    #3
  4. Ben Hallert

    RobB Guest

    Ben Hallert wrote:
    > Hi guys,
    >
    > I'm trying to figure out what bone headed mistake I made on something

    I
    > put together. I've got a form (named 'context') that has a variable
    > number of select-multiple inputs on it. Based on the number of
    > variables passed through a GET string, I want to multiply the total
    > number of selected items for each together to see how many possible
    > combinations the selected items are generating.
    >
    > The following snippet of code succesfully gets the values from the

    GET
    > statement to count the number of items being passed (I know, it

    assumes
    > the variable will always be in the same place, I'll deal with that
    > later), then it's supposed to loop through all elements in the page.
    > If the item it finds is a select-multiple input (up to this point, it
    > works. It finds each select-multiple and alerts in debugging), it's
    > supposed to do a loop inside that element to count how many are
    > checked, then multiply it against the running total (which is named
    > predictioncount). I expect the number for predictioncount to be 0
    > until the last field has an entry in it, but it never stops being
    > anything BUT zero.
    >
    > I'm no Javascript expert, and my kludgy code shows it, but I think

    I'm
    > close to this functioning and I'm hoping someone here will see and

    say
    > "Oh, you got the standard whatsit confused with the thingy, just

    change
    > character x to y and it should work".
    >
    > Thanks in advance for any assistance!
    >
    > function trackselections()
    > {
    > //We'll get the variable passed, then get the name of it. If the

    item
    > is selected, we add one to the item. If not, we subtract it
    > //This lets us map out what will be submitted and calculate

    complexity
    > based on that.
    > var num = document.context.length;
    > var count=0;
    > var predictioncount = 0;
    > var testunit_count = 0;
    > //First, count how many testcase_ids are passed.
    > var getstring = location.search.substr(1);
    > var testunit_ids = 0;
    > count = getstring.search(/\&/);
    > getstring = getstring.substr(13,count-13);
    > testunit_ids = getstring.split(/%2C/g);
    > testunit_count = testunit_ids.length;
    >
    > //Next, count all inputs of of the type select-multiple and loop
    > through them,
    > //collecting info on how many items are checked
    >
    > for (var i = 0; i < num; i++)
    > {
    > if (document.context.type == 'select-multiple')
    > {
    > count = 0;
    > for (var o = 0; o< document.context.length; o++)
    > {
    > if (document.context.id[o].selected) count++;
    > alert(count);
    > }
    > predictioncount = predictioncount * count;
    > }
    > alert(predictioncount);
    > }
    >
    > }


    I may be missing your point - but how can you read the submitted data
    from the querystring until you've submitted the form? Why even bother,
    as you can analyze it programmatically without performing any
    particular action.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript">
    //<![CDATA[

    function getCombos()
    {
    var s = 0, sel, sels = document.getElementsByTagName('select'),
    o, opt, opts,
    nsel, predictioncount = 1;
    while (sel = sels.item(s++))
    {
    if (/multiple/.test(sel.type))
    {
    opts = sel.getElementsByTagName('option');
    o = 0;
    nsel = 0;
    while (opt = opts.item(o++))
    if (opt.selected)
    ++nsel;
    }
    predictioncount *= nsel || 1;
    }
    return predictioncount;
    }

    //]]>
    </script>
    </head>
    <body>
    <form style="width:124px;">
    <select size="5" multiple="multiple">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select>
    <select size="5" multiple="multiple">
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    </select>
    <select size="5" multiple="multiple">
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    </select>
    <hr />
    <input type="button" value="get combos" onclick="alert(getCombos())" />
    </form>
    </body>
    </html>
     
    RobB, Jan 21, 2005
    #4
  5. Ben Hallert

    Ben Hallert Guest

    Good point on the starting from zero, I'll fix that immediately. I was
    trying to do it all on the fly instead of storing my values in an array
    because I felt I'd avoid some trouble, but that may not have been the
    case.
     
    Ben Hallert, Jan 21, 2005
    #5
  6. Ben Hallert

    Ben Hallert Guest

    The reason I was doing predictioncount = predictioncount * count is
    because each time it counts up the selected items, I wanted it to
    multiply the whole number by that. Eg, if I have 3 variables in the
    GET input, and 4 mutliple-select fields that each have two selected,
    then the total number of combinations (that I was trying to predict)
    would be 3*2*2*2*2=48 total units created. If Iadd them the way you
    showed, it would only be tallying the total number of selected items,
    not showing the multiplied total.

    Eureka! I applied your suggestion (changing id[o] to options[o]) and
    changed my initial value for predictioncount to be testunit_ids.length
    (to get the number of items passed in the GET variable string I was
    looking at) and it works now! Thank you, McKirahan, RobB, and Lee for
    your help, it got me over the little hump I was stuck at.
    Regards,

    Ben
     
    Ben Hallert, Jan 21, 2005
    #6
  7. Ben Hallert

    Ben Hallert Guest

    As a followup, here's the working code. It's might not be as elegant
    as RobB's, but it might be of use to some faceless denizen of the
    future who stumbles into a similar problem while learning JS:

    function trackselections()
    {
    //We'll get the variable passed, then get the name of it. If the item
    is selected, we add one to the item. If not, we subtract it
    //This lets us map out what will be submitted and calculate complexity
    based on that.
    var num = document.context.length;
    var count=0;
    var predictioncount;
    var testunit_count = 0;
    //First, count how many testcase_ids are passed.
    var getstring = location.search.substr(1);
    var testunit_ids = 0;
    count = getstring.search(/\&/);
    getstring = getstring.substr(13,count-13);
    testunit_ids = getstring.split(/%2C/g);
    predictioncount = testunit_ids.length;
    //Next, count all inputs of of the type select-multiple and loop
    through them,
    //collecting info on how many items are checked

    for (var i = 0; i < num; i++)
    {
    if (document.context.type == 'select-multiple')
    {
    count = 0;
    for (var o = 0; o< document.context.length; o++)
    {
    if (document.context.options[o].selected) count++;
    }
    predictioncount = predictioncount * count;
    }
    }

    //If the total number of test units being created is
    //over 1000 or so, alert with a warning
    if(predictioncount >=500 && predictioncount <=1000)
    {
    alert("If you submit this, you will create " + predictioncount + "
    testunits. Consider making assignments in smaller groups.");
    }
    if(predictioncount >=1001 && predictioncount <=3000)
    {
    alert("If you submit this, you will create " + predictioncount + "
    testunits. This may take a while (best case scenario) or crash your
    browser (worst case scenario). Consider making assignments in smaller
    groups.");
    }
    if(predictioncount >=3001)
    {
    alert("If you submit this, you will be asking the server to create "
    + predictioncount + " testunits. While the finely crafted swiss
    worksmanship of the G5 that serves Toro is ready and willing for the
    challenge, your browser most certainly isn't. You _will_ break your
    user session, and to add insult to injury, the mind bending quantity of
    work you've attempted to create will fall backwards into the shapeless
    ether from which it came. Give a hoot, don't pollute.");
    }

    }
     
    Ben Hallert, Jan 21, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Mueller
    Replies:
    5
    Views:
    12,409
    jamesxa
    Jun 16, 2009
  2. Srinivas Jonnalagadda

    Generating combinations from multiple sets

    Srinivas Jonnalagadda, Jan 16, 2006, in forum: Ruby
    Replies:
    1
    Views:
    143
    Srinivas Jonnalagadda
    Jan 16, 2006
  3. palmiere
    Replies:
    1
    Views:
    414
    Erwin Moller
    Feb 9, 2004
  4. Select boxes to select sets

    , Sep 14, 2005, in forum: Javascript
    Replies:
    5
    Views:
    103
    Dr John Stockton
    Sep 17, 2005
  5. Replies:
    6
    Views:
    119
    Luke Matuszewski
    Dec 21, 2005
Loading...

Share This Page