Evertjan. said:
Andrew Pollock wrote on 11 sep 2009 in comp.lang.javascript:
I made a calulator for sandwich leases (rent a property out and
sublet
it for profit). It works but is buggy depending on the order that the
form is filled in. So I added a button for the user to click to
doublecheck and update all the fields. My problem is that line 3
doesn't seem to work. ProfitifAlwaysRented.value doesn't update.
It should, but line 2 makes no sense.
(sandy is the name of the form)
if that is the case, use
document.forms['sandy']
to be cross browesr safe.
The below willl error is the user does not enter a string that can be
converted to a value.
With * or - the conversion is automatic.
An unary + could be less processor intensive ad a /1
The name checkAll is wrong, as the function does not check, but
compute.
function checkAll()
{document.sandy.RentDifference.value=document.sandy.ConsumerRent.v
alue/1-document.sandy.BaseRent.value/1;
document.sandy.ProfitifAlwaysRented.value=document.sandy.Profitif
AlwaysRented.value/1
document.sandy.ProfitifAlwaysRented.value=document.sandy.RentDiff
erence.value/1*document.sandy.PeriodinMo.value/1;
document.sandy.LostRevenue.value=document.sandy.ConsumerRent.valu
e/1*document.sandy.MonthsNotRented.value/1;
document.sandy.NetProfit.value=document.sandy.ProfitifAlwaysRente
d.value/1-document.sandy.LostRevenue.value/1;
}
If you did a better formatting job,
you would have seen what was wrong, methinks.
Try this:
[snipped]
I removed the functions I had being called by event handlers and pasted
yours in (also changed the NetProfit field to Profit to match your
function), but I couldn't get it to work. Here is the whole page code.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>Sandwich Lease Calculator</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;
charset=iso-8859-1">
<SCRIPT TYPE="text/javascript">
function computeAll() {
// define form
var temp = document.forms['sandy'];
// do reading from form
var ConsumerRent = temp.ConsumerRent.value;
var BaseRent = temp.BaseRent.value;
var PeriodinMo = temp.PeriodinMo.value;
var MonthsNotRented = temp.MonthsNotRented.value;
// do computation
var RentDifference = ConsumerRent - BaseRent;
var ProfitifAlwaysRented = RentDifference * PeriodinMo;
var LostRevenue = ConsumerRent * MonthsNotRented;
var Profit = ProfitifAlwaysRented - LostRevenue;
// do writing to form
temp.RentDifference.value = RentDifference;
temp.ProfitifAlwaysRented.value = ProfitifAlwaysRented;
temp.LostRevenue.value = LostRevenue;
temp.Profit.value = Profit;
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#C0C0C0" TEXT="#000000">
<FORM METHOD="post" ACTION="" NAME="sandy">
<H1 ALIGN="CENTER"><FONT COLOR="#0000FF">Sandwich Lease Calculator
</FONT></H1>
<TABLE BORDER="6" WIDTH="94%">
<TR BGCOLOR="#66CC33">
<TD WIDTH="30%">
<P>Period in months</P>
<P>
<INPUT TYPE="text" NAME="PeriodinMo" VALUE="">
</P>
</TD>
<TD WIDTH="70%">
<P>Consumer Rent</P>
<P>
<INPUT TYPE="text" NAME="ConsumerRent" VALUE="" >
</P>
</TD>
</TR>
<TR>
<TD WIDTH="30%"> </TD>
<TD WIDTH="70%"> </TD>
</TR>
<TR>
<TD HEIGHT="24" WIDTH="30%" BGCOLOR="#66CC33">
<P>Base Rent </P>
<P>
<INPUT TYPE="text" NAME="BaseRent" VALUE="">
</P>
</TD>
<TD HEIGHT="24" WIDTH="70%"> </TD>
</TR>
<TR>
<TD HEIGHT="24" WIDTH="30%"> </TD>
<TD HEIGHT="24" WIDTH="70%"> </TD>
</TR>
<TR BGCOLOR="#CC9966">
<TD WIDTH="30%">
<P>Rent Difference </P>
<P>
<INPUT TYPE="text" NAME="RentDifference "VALUE="">
</P>
</TD>
<TD WIDTH="70%">
<P>Profit if Always Rented </P>
<P>
<INPUT TYPE="text" NAME="ProfitifAlwaysRented" VALUE="">
</P>
</TD>
</TR>
<TR>
<TD WIDTH="30%"> </TD>
<TD WIDTH="70%"> </TD>
</TR>
<TR>
<TD WIDTH="30%" BGCOLOR="#66CC33">
<P>Months Not Rented </P>
<P>
<INPUT TYPE="text" NAME="MonthsNotRented" VALUE="">
</P>
</TD>
<TD WIDTH="70%" BGCOLOR="#CC9966">
<P>Lost Revenue </P>
<P>
<INPUT TYPE="text" NAME="LostRevenue" VALUE="">
</P>
</TD>
</TR>
<TR>
<TD WIDTH="30%"> </TD>
<TD WIDTH="70%"> </TD>
</TR>
<TR>
<TD WIDTH="30%" BGCOLOR="#CC9966">
<P>Net Profit </P>
<P>
<INPUT TYPE="text" NAME="Profit" VALUE="">
</P>
</TD>
<TD WIDTH="70%" ALIGN="CENTER" VALIGN="MIDDLE">
<INPUT TYPE="BUTTON" NAME="Button" VALUE="Compute"
onClick="computeAll()">
</TR>
</TABLE>
<P> </P>
</FORM>
</BODY>
</HTML>