
Originally Posted by
merlyn
Basically this code seems to halt after calculation of the total fat and needs to have 'TotalFat' defined.
Perhaps I'm going blind, but I don't see anything that would cause that problem. So, I'll make a few comments and post an alternative.
By the way, in order to maintain formatting, wrap your code in [code]...[/code] or [html]...[/html] pseudo-tags.
<script LANGUAGE="JAVASCRIPT">
The language attribute has been deprecated for over six years. Use the type attribute instead:
HTML Code:
<script type="text/javascript">
<!--Hide from old browsers
"Hiding" scripts is equally deprecated. The "old browsers" to which that comment refers aren't used any more (they haven't been for a long time).
function FatCal(myform) {
If the myform argument truely contains a reference to your form, you should replace document.Fat with myform.
The isNaN function isn't very appropriate for validation purposes. It allows any number form - integer, exponential, floating-point, and hexadecimal - which is usually excessive. Regular expressions provide a much richer alternative.
document.Fat.Grams.value=" "
document.Fat.Grams.focus()
If validation fails, it would probably be a good idea to stop performing the operation.
var TotalFatPerc = TotalFat/FatNumbCal
This doesn't make much sense to me. Wouldn't the correct value be obtained by dividing the number of calories from the fat by the total number of calories, or am I missing something?
function makePerc(TotalFatPerc) {
The function you've defined here doesn't produce a percentage. It returns the first two digits after a decimal point. What if the value is one hundred percent of the total? It would be much easier to multiply by a hundred and round.
The final comment I have to make is that it's considered good form to end every statement with a semicolon.
Now, the alternative as promised.
Hope that helps,
Mike
Bookmarks