Adding up combinations with select-multiple boxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Hallert

    #1

    Adding up combinations with select-multiple boxes

    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.contex t.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.searc h(/\&/);
    getstring = getstring.subst r(13,count-13);
    testunit_ids = getstring.split (/%2C/g);
    testunit_count = testunit_ids.le ngth;

    //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.conte xt[i].type == 'select-multiple')
    {
    count = 0;
    for (var o = 0; o< document.contex t[i].length; o++)
    {
    if (document.conte xt[i].id[o].selected) count++;
    alert(count);
    }
    predictioncount = predictioncount * count;
    }
    alert(predictio ncount);
    }

    }

  • McKirahan

    #2
    Re: Adding up combinations with select-multiple boxes

    "Ben Hallert" <ben.hallert@gm ail.com> wrote in message
    news:1106333438 .642836.254610@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    > 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.contex t.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.searc h(/\&/);
    > getstring = getstring.subst r(13,count-13);
    > testunit_ids = getstring.split (/%2C/g);
    > testunit_count = testunit_ids.le ngth;
    >
    > //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.conte xt[i].type == 'select-multiple')
    > {
    > count = 0;
    > for (var o = 0; o< document.contex t[i].length; o++)
    > {
    > if (document.conte xt[i].id[o].selected) count++;
    > alert(count);
    > }
    > predictioncount = predictioncount * count;
    > }
    > alert(predictio ncount);
    > }
    >
    > }
    >[/color]

    I think you want to replace
    if (document.conte xt[i].id[o].selected) count++;
    with this
    if (document.conte xt[i].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;





    Comment

    • Lee

      #3
      Re: Adding up combinations with select-multiple boxes

      Ben Hallert said:[color=blue]
      >
      >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
      >predictioncoun t). 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.[/color]

      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.

      Comment

      • RobB

        #4
        Re: Adding up combinations with select-multiple boxes

        Ben Hallert wrote:[color=blue]
        > Hi guys,
        >
        > I'm trying to figure out what bone headed mistake I made on something[/color]
        I[color=blue]
        > 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[/color]
        GET[color=blue]
        > statement to count the number of items being passed (I know, it[/color]
        assumes[color=blue]
        > 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[/color]
        I'm[color=blue]
        > close to this functioning and I'm hoping someone here will see and[/color]
        say[color=blue]
        > "Oh, you got the standard whatsit confused with the thingy, just[/color]
        change[color=blue]
        > 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[/color]
        item[color=blue]
        > is selected, we add one to the item. If not, we subtract it
        > //This lets us map out what will be submitted and calculate[/color]
        complexity[color=blue]
        > based on that.
        > var num = document.contex t.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.searc h(/\&/);
        > getstring = getstring.subst r(13,count-13);
        > testunit_ids = getstring.split (/%2C/g);
        > testunit_count = testunit_ids.le ngth;
        >
        > //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.conte xt[i].type == 'select-multiple')
        > {
        > count = 0;
        > for (var o = 0; o< document.contex t[i].length; o++)
        > {
        > if (document.conte xt[i].id[o].selected) count++;
        > alert(count);
        > }
        > predictioncount = predictioncount * count;
        > }
        > alert(predictio ncount);
        > }
        >
        > }[/color]

        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 programmaticall y 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.getEle mentsByTagName( 'select'),
        o, opt, opts,
        nsel, predictioncount = 1;
        while (sel = sels.item(s++))
        {
        if (/multiple/.test(sel.type) )
        {
        opts = sel.getElements ByTagName('opti on');
        o = 0;
        nsel = 0;
        while (opt = opts.item(o++))
        if (opt.selected)
        ++nsel;
        }
        predictioncount *= nsel || 1;
        }
        return predictioncount ;
        }

        //]]>
        </script>
        </head>
        <body>
        <form style="width:12 4px;">
        <select size="5" multiple="multi ple">
        <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="multi ple">
        <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="multi ple">
        <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>

        Comment

        • Ben Hallert

          #5
          Re: Adding up combinations with select-multiple boxes

          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.

          Comment

          • Ben Hallert

            #6
            Re: Adding up combinations with select-multiple boxes

            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.le ngth
            (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

            Comment

            • Ben Hallert

              #7
              Re: Adding up combinations with select-multiple boxes

              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.contex t.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.searc h(/\&/);
              getstring = getstring.subst r(13,count-13);
              testunit_ids = getstring.split (/%2C/g);
              predictioncount = testunit_ids.le ngth;
              //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.conte xt[i].type == 'select-multiple')
              {
              count = 0;
              for (var o = 0; o< document.contex t[i].length; o++)
              {
              if (document.conte xt[i].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(predictionco unt >=500 && predictioncount <=1000)
              {
              alert("If you submit this, you will create " + predictioncount + "
              testunits. Consider making assignments in smaller groups.");
              }
              if(predictionco unt >=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(predictionco unt >=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.");
              }

              }

              Comment

              Working...