Web Analytics Made Easy -
StatCounter for loop/select element help - CodingForum

Announcement

Collapse
No announcement yet.

for loop/select element help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • for loop/select element help

    hey all, long time no post.....

    i have this script to work out the cost of loads of selected computer components, there are lots of drop down lists which each have a value which is the cost of that component.

    firstly the script checks that the two main required fields are completed. this works fine.

    then a for loop runs through the 15 elements on the form and is supposed to add together all the values of each selected component in the drop down lists. however i keep getting errors like "undefined42 is undefined." (42 happens to be the value of the first selected component, showing that it seems to work at least the first time through.

    i'm using the eval function to evaluate the strings as a sum, is this right? - this is where i think the problem might be

    ill leave you the code......

    Code:
    <script language="JavaScript">
    <!--
    
    function quote() {
    
    var cost
    
    if (document.forms['components'].elements["case"].selectedIndex == 0) {
      alert('Please select a case.')
      return false
    }
    
    else if (document.forms['components'].elements["moth"].selectedIndex == 0) {
      alert('Please select a motherboard.')
      return false
    }
    
    for (i = 0; i < 16; i++) {
    cost = eval(cost + document.forms['components'].elements[i].value)
    }
    
    alert(cost)
    }
    
    //-->
    </script>
    and a select box like:

    Code:
    <select name="case">
    <option value="0"></option>
    <option value="42">ENLIGHT / Desktop 230 Watt</option>
    <option value="135">ENLIGHT / Server Tower 400 Watt</option>
    <option value="29">STEK / Desktop 230 Watt</option>
    <option value="55">STEK / Large Tower 230 Watt</option>
    <option value="29">STEK / Midi Tower 230 Watt</option>
    </select>
    thanks in advance

    dd/mm/yy

  • #2
    Yeah I don't know about that eval...

    Try declaring cost as:
    Code:
    var cost = 0;
    Then the for loop could look like:
    Code:
    for (i = 0; i < 16; i++) {
     cost += document.forms['components'].elements[i].value;
    }
    I'm pretty sure that should work. If it is still considering them as strings, try this:
    Code:
    for (i = 0; i < 16; i++) {
     cost += (document.forms['components'].elements[i].value)*1;
    }
    Hope that helps,
    Sadiq.

    Comment


    • #3
      ah thanks for that, the first example you showed was stil working as a string, but multiplying by 1 did the trick!

      cheers, ill av to remember that

      dd/mm/yy

      Comment


      • #4
        Other ways of converting string to numbers:

        cost += parseInt(document.forms['components'].elements[i].value, 10);

        cost += Number(document.forms['components'].elements[i].value);
        Glenn
        vBulletin Mods That Rock!

        Comment


        • #5
          if you need decimals, parse floating

          parseFloat(....)
          KOR
          Offshore programming
          -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

          Comment


          • #6
            thanks for those aswell they look the more correct way to do it so i have switched to the Number() method

            thanks

            Comment


            • #7
              Originally posted by dd/mm/yy
              ah thanks for that, the first example you showed was stil working as a string, but multiplying by 1 did the trick!

              cheers, ill av to remember that

              dd/mm/yy
              FYI, this worked because multiplying by one (or any number) caused JavaScript to do an implicit type conversion to the other part of the equation so that it could do the math.

              Using the parseInt() is better however, because it tells everyone reading your code (including you 6 months from now!) that "hey, I know my operands are not numbers yet."

              Further, in other languges, like JAVA, implicit type conversion is not as forgiving as Javascript and can be tricky; you save yourself a lot of grief and troubleshoot time(!!!) by always doing explicit type conversion (ie. parseInt() in JavaScript),

              Comment


              • #8
                oh man, what did I start here....


                Sadiq.

                Comment


                • #9
                  You done good, Sad69

                  Comment


                  • #10
                    Is the numer() method as good as the parseInt() method?
                    What are the differences. I never heard of the number() method...
                    nor did I hear about the parsefloat() method
                    Shawn

                    Comment


                    • #11
                      hm...

                      KOR
                      Offshore programming
                      -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

                      Comment


                      • #12
                        Nice link, thanks.
                        But I still don't quite understand the difference, there isn't an example for number(). Could you provide one please?
                        If I had an example I'm sure I could understand. Thanks
                        Shawn

                        Comment


                        • #13
                          Number(object) converts an objects argument, while ParseInt(string) converts a string. It is not quite the same thing. For instance, Number() can convert a boolean argument

                          bla = new Boolean('true')
                          alert(Number(bla));

                          the output is 1
                          KOR
                          Offshore programming
                          -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

                          Comment


                          • #14
                            oh I see. Thanks for the explanation
                            Shawn

                            Comment

                            Working...
                            X