Pounds & Ounces Calculator

Discussion in 'JavaScript' started by Salchester, Aug 14, 2007.

  1. #1
    Hi,

    Using JavaScript, how do I subtract a weight from a drop-down menu from another drop-down menu, to calculate the amount of weight loss?
    In total there should be 6 drop-down boxes, these are the following:

    1: Pounds (Before), 2. Ounces (Before), 3. Fraction (Before), 4. Pounds (After), 5. Ounces (After), & 6. Fraction (After)

    For example if:

    Drop-Down Box 1 = 5
    Drop-Down Box 2 = 3
    Drop-Down Box 3 = ¾

    Drop-Down Box 4 = 3
    Drop-Down Box 5 = 2
    Drop-Down Box 6 = ¼

    Answer = 2.1½ have been lost.
     
    Salchester, Aug 14, 2007 IP
  2. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    well.. might need some code up to actually see what you're doing but..

    i'm thinking you want to have this calculated each time a drop down menu is changed or something of the sort..

    so if each option's value is the same as it's text we can do something like this..

    
    <select id="lb1">
      <option value="5" onchange="calculate(pounds);">5
      <option ...
      ...
    </select>
    
    <select id="lb2">
      <option value="5" onchange="calculate(pounds);">5
      <option ...
      ...
    </select>
    
    ...
    
    <p>
    Pounds Lost: <span id="poundoutput"></span>
    ...
    </p>
    
    Code (markup):
    and than the javascript

    
    function calculate(weighttype)
    {
      if (weighttype == pounds)
      {
         document.getElementById("poundoutput").text = document.getElementById("lb1").value - document.getElementById("lb2").value
      }
    }
    
    Code (markup):
    i haven't tested any of this.. and their most likely is a better way to do it.. just thought i'd throw you a bone anyways.. good luck
     
    Jamie18, Aug 14, 2007 IP
  3. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #3
    I suspect seriously that this is an assignment question, in which case this isn't going to do you much good.
     
    Logic Ali, Aug 14, 2007 IP