PDA

View Full Version : define



merlyn
01-06-2005, 06:08 PM
Basically this code seems to halt after calculation of the total fat and needs to have 'TotalFat' defined. Im still working on this however would like to hear opinions to a solution. Yes I am very new to Javascript, lol.


<script LANGUAGE="JAVASCRIPT">
<!--Hide from old browsers
function FatCal(myform) {
var FatGrams=document.Fat.Grams.value
var FatGrams=parseInt(FatGrams,10)

if (isNaN(FatGrams)) {
alert("Please enter the fat grams.")

document.Fat.Grams.value=" "
document.Fat.Grams.focus()

}
else {
alert("FatCal Elsestart")
var FatNumbCal=document.Fat.NumbCal.value
var FatNumbCal=parseInt(FatNumbCal,10)
alert("FatCal Elsestart2")
if (isNaN(FatNumbCal)) {
alert("Please enter the total calories")
document.Fat.NumbCal.value=""
document.Fat.NumbCal.focus()
alert("FatCal ElseEnd")
}
}
// calculate fat calories


var TotalFat = FatGrams
var TotalFatCal = FatGrams * 9
document.Fat.Total.value = TotalFatCal

// calculate percentage of fat
var TotalFatPerc = TotalFat/FatNumbCal
var TotalFatPerc = new String(TotalFatPerc)
document.Fat.FatPerc.value = makePerc(TotalFatPerc)
alert("FatCal end")
}

// Start makePerc function here
function makePerc(TotalFatPerc) {
alert("makePerc start")
var valuein = TotalFatPerc
strPercent = ""
tPercent = valuein
decimal = tPercent.indexOf(".")
Percent = parseInt(tPercent.substring(decimal+1,decimal+3),10)
return strPercent
alert("makePerc end")
}
//-->
</script>

mwinter
01-06-2005, 11:42 PM
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 &#91;code&#93;...&#91;/code&#93; or &#91;html&#93;...&#91;/html&#93; pseudo-tags.


<script LANGUAGE="JAVASCRIPT">The language attribute has been deprecated for over six years. Use the type attribute instead:


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


if (isNaN(FatGrams)) {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/FatNumbCalThis 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 (http://www.mlwinter.pwp.blueyonder.co.uk/dd/merlyn/fat.html) as promised.

Hope that helps,
Mike

merlyn
01-10-2005, 05:37 PM
Ah, a much better solution to my problem. Thank you very much for taking the time to awnser my post. I appriciate all the help. I am just beginning to get my feet wet with this stuff. Ive done really nothing but mIRC and HTML before so this is a bit new. Once again thank you :D