Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    bmi calculator with javascript

    I need help trying to figure out how to get my bmi calculator to work. I cant figure out what is wrong with my formula part and why it wont calculate the numbers you type in...pls help!

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>

    <script type="text/javascript">

    function calculateBMI()
    {
    var weight = numb(document.data.weight.value)
    var height = numb(document.data.height.value)
    var height2 = height2 * height2
    var BMI = weight * 703 / (height2 * height2)

    document.form.BodyMassIndex.value=custRound(BMI,1);
    }

    </script>
    <title></title>

    <style type="text/css">
    div.c2 {text-align: center}
    input.c1 {font-size: 8pt}
    </style>
    </head>
    <body>
    <div class="c2">
    <form name="form" id="form"><input type="text" name="weight" size="4"> Weight (in Kilos) <input type="text" name="height" size="4"> Height (in Centimeters)<br>
    <br>
    <input type="text" name="BodyMassIndex" id="BodyMassIndex" size="4"> BMI <input type="button" class="c1" value="Calculate" onclick="calculateBMI()" name="button"> <input type="reset" class="c1" value="Clear Form"></form>
    </div>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Question 1: Where did you get that formula from? BMI usually is (weight in kg)/(height in cm)^2 and you are (trying) to calculate (weight in kg) * 703 / (height in cm)^4

    Question 2: You are using a function numb() which is defined nowhere. Where did you get it from? Usually the native Javascript function for that purpose is called Number()

    Question 3: What is document.data? I only see document.form in your HTML

    Something like that will calculate the BMI (if weight and height are given correctly)
    Code:
    function calculateBMI()
    {
       var weight = Number(document.form.weight.value);
       var height = Number(document.form.height.value);
       var BMI = weight  / (height * height);
    
       document.form.BodyMassIndex.value=BMI.toFixed(1);
    }
    Last edited by devnull69; 11-16-2011 at 01:22 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •