...

View Full Version : Body Fat Calculator



axel22
11-08-2012, 03:52 AM
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.




<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>

Logic Ali
11-08-2012, 05:14 AM
Please pass this message to the person responsible for teaching you JavaScript.

Dear "Teacher",
You are invited to post on the forum http://www.codingforums.com/forumdisplay.php?f=2 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.

Old Pedant
11-08-2012, 05:43 AM
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:


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

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).

Old Pedant
11-08-2012, 05:46 AM
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."

axel22
11-10-2012, 09:48 PM
<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?

Philip M
11-11-2012, 04:30 PM
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!


<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.

felgall
11-11-2012, 06:41 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum