Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-08-2012, 03:52 AM   PM User | #1
axel22
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
axel22 is an unknown quantity at this point
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>
axel22 is offline   Reply With Quote
Old 11-08-2012, 05:14 AM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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.
Logic Ali is offline   Reply With Quote
Old 11-08-2012, 05:43 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
axel22 (11-08-2012)
Old 11-08-2012, 05:46 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
A little too gentle, LogicAli:
Quote:
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.
Old Pedant is offline   Reply With Quote
Old 11-10-2012, 09:48 PM   PM User | #5
axel22
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
axel22 is an unknown quantity at this point
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?
axel22 is offline   Reply With Quote
Old 11-11-2012, 04:30 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 11-11-2012 at 04:55 PM..
Philip M is offline   Reply With Quote
Old 11-11-2012, 06:41 PM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,462
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:59 AM.


Advertisement
Log in to turn off these ads.