Results 1 to 3 of 3

Thread: define

  1. #1
    Join Date
    Jan 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default define

    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>

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote 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 &#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:

    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.

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

  3. #3
    Join Date
    Jan 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Thanks :-)

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •