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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Body Fat Calculator

    I am trying to create a body fat calculator but I am not sure if my equations are correct. I feel like im missing a variable? I am not too sure about my equation for javascript. Please explain why you did what you did.


    Code:
    <html>
    <head>
    <title> BodyFat Formula </title>
    <script type="text/javascript">
    function BodyFat(weight, waist){
    	var weight, waist, bodyfat, total bodyfat
    if ((weight >=0) , (waist >=0) , (total bodyfat >=0))
    
    weight = Math.round(weightBox x 1.082) + 94.42
    waist = Math.round(waistBox x 4.15)
    bodyfat = (weight - waist)
    total bodyfat =  Math.round(bodyfat x 100) / total bodyfat)
    
    weight = parseInt(document.getElementById("weightBox").value;
    waist = parseInt(document.getElementById("waistBox").value;
    bodyfat = = parseInt(document.getElementById("bodyfatBox").value;
    
    
    
    // Assumes: weightBox contains the person's weight in pounds
    // Assumes: waistBox contains the person's waist measurement in inches
    // converts the weight and the waist measurement into numeric values
    // calculates the BodyFat using the following Body Fat Formula(For Men): 
    // Factor1: (Total body weight x 1.082) + 94.42 
    // Factor2: Waist measurement x 4.15 
    // Lean Body Mass: Factor1 - Factor2 
    // Body Fat Weight: Total body weight - Lean Body Mass 
    // Body Fat Percentage: Rounded( (Body Fat Weight x 100) / total bodyweight ) 
    
    }
    </script>
    </head>
    <body>
    <h2>BodyFat Formula</h2>
    <p>
    Weight: <input type="text" id="weightBox" size=4 value=150> pounds <br>
    Waist measurement: <input type="text" id="waistBox" size=4 value=32> inches<br>
    BodyFat: <input type="text" id="bodyfatBox" size=4 value=0> percent
    </p>
    <input type="button" value="Calculate body fat" onclick="BodyFat();">
    <hr>
    <div id="outputDiv"></div>
    </body>
    </html>

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Please pass this message to the person responsible for teaching you JavaScript.

    Dear "Teacher",
    You are invited to post on the forum http://codingforums.com/javascript-programming/ to afford you the opportunity to explain your rationale for failing to teach your JavaScript students to use the error console as a matter of course.
    So to do would prevent them wasting countless hours of frustration, culminating in their having publicly to humiliate themselves by seeking unnecessary assistance in forums, with regard to trivial syntax errors and the like readily indicated by the console.
    If the phrase 'error console' is alien to you, then you may wish to research the term while considering your position.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,648 Times in 4,610 Posts
    Axel: Here's a hint for you.

    Computer languages in general, and JavaScript in particular, execute code IN THE ORDER that it appears in your coding text. They don't "look ahead" to find values that haven't been obtained yet, for example. [Yes, yes...I know function names are a huge exception...but that doesn't apply in this case.]

    So look at your code, just the first few lines:
    Code:
        var weight, waist, bodyfat, total bodyfat 
        if ( (weight >=0) , (waist >=0) , (total bodyfat >=0) )
    At the point were you are trying to do weight >= 0 what do you think the value of weight is?

    HINT: You never defined a value. So it has no value. (Or, rather, it has a value of null, but that is useless to you.)

    Same is, of course, true for waist.

    The case of total bodyFat is more egregious. You have a space in the middle of what I *assume* is supposed to be totalbodyFat. KABLOOEY. JavaScript doesn't permit spaces in names.

    Mind you, these are just the *beginning* of your errors.

    Just for one more example (and there are others!) you do
    Code:
        weight = Math.round(weightBox x 1.082) + 94.42
    but you have never defined a variable named weightBox so the value of *THAT* variable is null and you can't then multiply it by anything.

    In short, what you have there is pretty much total hash.

    Start all over again.

    Remember what I said: JavaScript executes the code *IN THE SAME ORDER* that you wrote it. It can't use values at point A when they aren't defined until point B in the code (and B follows A).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    axel22 (11-08-2012)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,648 Times in 4,610 Posts
    A little too gentle, LogicAli:
    If the phrase 'error console' is alien to you, then you may wish to research the term while considering your position.
    Maybe that should end in "...while considering how fast you can flip burgers when the powers that be finally realize you are incompetent in your current position."
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Code:
    <html>
    <head>
    <title> BodyFat Formula </title>
    <script type="text/javascript">
    function BodyFat(weight, waist, bodyfat){
    
    
    var weight = parseInt(document.getElementById("weightBox").value, 10);
    var waist = parseInt(document.getElementById("waistBox").value, 10);
    var BodyFat = parseInt(document.getElementById("bodyfatBox").value, 10);
    	
    	weightBox = Math.round(weight * 1.082) + (94.42);
    	waistBox = Math.round(waist * 4.15);
    
    var str1="Your Body Fat Percentage is ";
    document.getElementById('outputDiv').innerHTML= str1.concat(weight);
    
    }
    </script>
    </head>
    <h2>BodyFat Formula</h2>
    <p>
        Weight: <input type="number" id="weightBox" size="4" value="150" min="0"> pounds
     <br>
        Waist measurement: <input type="number" id="waistBox" size="4" value="32" min="0"> inches
    <br>
        BodyFat: <input type="number" id="bodyfatBox" size="4" value="0" min="0" max="0"> percent
    <br>
    </p>
    <input type="button" value="Calculate body fat" onclick="BodyFat();">
    <hr>
    <div id="outputDiv"></div>
    </html>
    How is that? Im just confused now it just shows the weight as an answer not the equation?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    There a lot of problems, I am afraid. Not least that you are using the same name for an HTML element and a Javascript variable, and, worse, you are using the same name BodyFat both as the name of a function and the name of a variable.
    The concat() method is used to join two or more arrays.
    You have left out the <body> and </body> tags.
    And nowhere do you actually calculate the Body Fat Index.
    But I give you credit for correctly using the radix 10 with parseInt() - although Number() would have been just as good and would give you the opportunity to trap NaN entries, thus:-

    var weight = Number(document.getElementById("weightBox").value) || 0 ; // return 0 if not a number

    I have put it right for you following the guidance in your first post. Please study and learn from it!

    Code:
    <html>
    <head>
    <title> BodyFat Formula </title>
    
    <script type="text/javascript">
    
    function BodyFat() {
    var weight = parseInt(document.getElementById("weightBox").value, 10);
    var waist = parseInt(document.getElementById("waistBox").value, 10);
    var Factor1 = weight * 1.082 + 94.42;   
    var Factor2 = waist * 4.15;
    var LBMass = Factor1 - Factor2;
    var BFWeight = weight - LBMass;
    var BFPercent = (BFWeight * 100/ weight).toFixed(2);  // 2 decimal places
    document.getElementById("bodyfatBox").value = BFPercent;
    }
    </script>
    
    </head>
    <body>
    
    <h2>BodyFat Formula</h2>
    Weight: <input type="text" id="weightBox" size="4" value="150"> pounds
    <br>
    Waist measurement: <input type="text" id="waistBox" size="4" value="32" > inches
    <br>
    BodyFat: <input type="text" id="bodyfatBox" size=4 value=0> percent
    <br>
    <input type="button" value="Calculate body fat" onclick="BodyFat();">
    <hr>
    </body>
    </html>
    It is not really in your best interests that others do your all or most homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase.

    I am happy to give you a push forward, but remember that there is a limit to the number of times that you can take your pitcher to this well!

    I also endorse the comments of Logic Ali regarding the use of the error console.
    Last edited by Philip M; 11-11-2012 at 05:55 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    All browsers now also have a full JavaScript debugger built in (except Firefox which has several extensions available to add a debugger). So you can even go beyond the error console and step through your code one command at a time and see exactly what gets updated and what the values it uses are.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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