Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'd like help with code

Status
Not open for further replies.

ckdoublenecks

Programmer
Dec 28, 2010
9
US
The below code works except that the calcs for "if the amptaid >= totOwed" don't work. I know it has to do with the
statement
"var totOwed = parseInt(rentdue.value) + parseInt(prevbal.value) + parseInt(secdep.value) + parseInt(damage.value) + parseInt(latechg.value) + parseInt(courtcost.value) +
parseInt(nsf.value) - parseInt(hudpay.value);"

but I don't know enough to make it work.

<script>
function $_(IDS) { return document.getElementById(IDS); }
function calculate_paid() {
var amtpaid = document.getElementById("amtpaid");
var rentdue = document.getElementById("rentdue");
var prevbal = document.getElementById("prevbal");
var hudpay = document.getElementById("hudpay");
var tentpay = document.getElementById("tentpay");
var datepaid = document.getElementById("datepaid");
var late = document.getElementById("late");
var damage = document.getElementById("damage");
var courtcost = document.getElementById("courtcost");
var nsf = document.getElementById("nsf");
var latechg = document.getElementById("latechg");
var secdep = document.getElementById("secdep");
var paidsum = document.getElementById("paidsum");
var dateNow = new Date();
var dayNow = dateNow.getDate();
var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
datepaid.value = datePaid;
paidsum.value = parseInt(paidsum.value) + parseInt(amtpaid.value);
tentpay.value = parseInt(tentpay.value) + parseInt(amtpaid.value) - parseInt(hudpay.value);
// *********************************************************
var totOwed = parseInt(rentdue.value) + parseInt(prevbal.value) + parseInt(secdep.value) + parseInt(damage.value) + parseInt(latechg.value) + parseInt(courtcost.value) +
parseInt(nsf.value) - parseInt(hudpay.value);
if(dayNow > 5) { late.value = "L"; totOwed = totOwed.value + 10; }
var excess = parseInt(amtpaid.value - rentdue.value);
if (amtpaid.value >= totOwed) { prevbal.value = totOwed - amtpaid.value ;
excess = 0 ; secdep.value = 0 ; damage.value = 0 ; latechg.value = 0 ;
courtcost.value = 0 ; nsf.value = 0; }
// *********************************************************
if (excess < prevbal.value && amtpaid.value > rentdue.value) { prevbal.value = prevbal.value - excess; excess = 0}
if (excess >= prevbal.value) { excess = excess - prevbal.value; prevbal.value = 0 ; }
if (excess < secdep.value && amtpaid.value > rentdue.value) { secdep.value = secdep.value - excess; excess = 0}
if (excess >= secdep.value) { excess = excess - secdep.value; secdep.value = 0 ; }
if (excess < damage.value && amtpaid.value > rentdue.value) { damage.value = damage.value - excess; excess = 0}
if (excess >= damage.value) { excess = excess - damage.value; damage.value = 0 ; }
if (excess < latechg.value && amtpaid.value > rentdue.value) { latechg.value = latechg.value - excess; excess = 0}
if (excess >= latechg.value) { excess = excess - latechg.value; latechg.value = 0 ; }
if (excess < courtcost.value && amtpaid.value > rentdue.value) { courtcost.value = courtcost.value - excess; excess = 0}
if (excess >= courtcost.value) { excess = excess - courtcost.value; courtcost.value = 0 ; }
if (excess < nsf.value && amtpaid.value > rentdue.value) { nsf.value = nsf.value - excess; excess = 0}
if (excess >= nsf.value) { excess = excess - nsf.value; nsf.value = 0 ; }
}
 
Just print out all the values concerned and you'll see where the error is. Use WScript.echo to print.
 
It looks like you have line of code split across 2 lines, which Javascript doesn't like. Put it all on one line or break the calculation into 2 separate operations.

Lee
 
Code spanning multiple lines shouldn't be an issue as long as you don't split in the middle of an object, method or property name. It can make the code more readable, and help in spotting errors.

You didn't say in what way the code "doesn't work", but I see several issues. First, be aware that parseInt() will return NaN (not a number) for anything it can't parse, and I don't see any error checking (perhaps that was omitted for brevity).

Second, you're only using parseInt() in part of the code, and in other parts doing arithmetic by directly accessing the elements' values. JavaScript is forgiving of course, as long as there's a number in each element. So if these are set programmically, you're probably OK, but if the user enters these fields you could have problems once the code is in the wild.

Last, in reading the field names, I'm guessing that some of them could actually be decimal quantities. Note that if a user enters "10.95", parseInt() will only return 10.

My suggestion would be to modify the code block at the top to do the parseInt() (or parseDouble() if needed) right off the bat into separate "value" variables, i.e.:

var amtpaid = document.getElementById("amtpaid");
var amtpaidVal = parseInt(amtpaid.value);
var ...

Follow that with validations (at least check each for NaN), and use the "xxxVal" variables for all arithmetic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top