View Full Version : Help with simple form logic

09-17-2006, 12:52 PM
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.

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;

09-17-2006, 07:46 PM
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.


09-17-2006, 08:06 PM
The heart of this is the same issue I corrected in your other thread.

09-17-2006, 08:50 PM
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,

09-17-2006, 09:03 PM
I also pointed out the flaws in your original code, however. This would probably be of more use here.
function CalcDueNow(charges) {
var f = document.forms['charges'].elements,
d = f.duedatedif,
t = f.total;

if (d.value < 56)
f.duenow.value = t.value;
d.value = f.rentalcost.value * 0.3;
f.balancedue.value = t.value - d.value;

09-19-2006, 07:31 PM
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.