![]() |
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> |
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. |
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 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.42In 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). |
A little too gentle, LogicAli:
Quote:
|
Code:
<html> |
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>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 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.
|
| All times are GMT +1. The time now is 08:55 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.