Results 1 to 6 of 6

Thread: Help with simple form logic

  1. #1
    Join Date
    Sep 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with simple form logic

    I have the following form logic that always defaults to the "else" statement even when "duedatedif.value" < 56. Can anyone please tell me what's wrong with the way I've written this.
    Code:
    function CalcDueNow(charges)
    
    {charges = document.forms['charges'].elements['duenow'].value
    
    	if (document.forms.charges.duedatedif.value < 56)
    	document.forms.charges.duenow.value = document.forms.charges.total.value;
    	
    	else (document.forms.charges.duedatedif.value > 55)
    	document.forms.charges.duenow.value = document.forms.charges.rentalcost.value * .3;
    	document.forms.charges.balancedue.value = document.forms.charges.total.value - document.forms.charges.duenow.value;
    }

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

    Default

    Perhaps you could explain exactly what's supposed to occur as it's not entirely clear. For instance,

    charges = document.forms['charges'].elements['duenow'].value
    you never use that value. You should also declare all variables, especially if they should be local.

    else (document.forms.charges.duedatedif.value > 55)
    This doesn't do what you think it will: if the first condition isn't satisfied (the one following the if keyword), the alternate action is to evaluate that expression, but that's all. After this line, the if..else statement has ended and the two statements that follow it will be executed unconditionally. It's not clear if they should only be executed if the expression above evaluates to true, or only the first is dependent upon it.

    The curious thing, though, is the two comparisons you attempt to make. The first checks if the form control value is less than 56, and the second checks if it is greater than 55. Those values overlap.

    A final consideration is efficiency: there's no need to keep looking up property values, but we'll worry about that later.

    Mike

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The heart of this is the same issue I corrected in your other thread.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Sep 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Twey for the post. Yes, you did correct me previously on a similar matter and provided the "discount boundaries" which I appreciated greatly. I tried to use that same code modified in this situation but I kept running into problems. I'll have to study your code a little more. I did, however, find a work around that works just fine. It's may not be as clean as your code but it works.
    Thanks again,
    Mark

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I also pointed out the flaws in your original code, however. This would probably be of more use here.
    Code:
    function CalcDueNow(charges) {
      var f = document.forms['charges'].elements,
        d = f.duedatedif,
        t = f.total;
    
      if (d.value < 56)
        f.duenow.value = t.value;
      else
        d.value = f.rentalcost.value * 0.3;
      f.balancedue.value = t.value - d.value;
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Sep 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Twey for the latest code. Besides solving possible problems I might have with the work around I have it will help me understand your previous code because I'll be able to compare the 2 pieces of code. I'll incorporate it in place of my code and see how it works.
    Thanks again for the continued guidance.
    Mark

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
  •