third statement in function not producing results

A

Andrew Pollock

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.
(sandy is the name of the form)

function checkAll()
{document.sandy.RentDifference.value=document.sandy.ConsumerRent.value/1-document.sandy.BaseRent.value/1;
document.sandy.ProfitifAlwaysRented.value=document.sandy.ProfitifAlwaysRented.value/1
document.sandy.ProfitifAlwaysRented.value=document.sandy.RentDifference.value/1*document.sandy.PeriodinMo.value/1;
document.sandy.LostRevenue.value=document.sandy.ConsumerRent.value/1*document.sandy.MonthsNotRented.value/1;
document.sandy.NetProfit.value=document.sandy.ProfitifAlwaysRented.value/1-document.sandy.LostRevenue.value/1;
}
 
C

Captain Paralytic

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.
(sandy is the name of the form)

function checkAll()
     {document.sandy.RentDifference.value=document.sandy.ConsumerRent.value/1-do cument.sandy.BaseRent.value/1;
      document.sandy.ProfitifAlwaysRented.value=document.sandy.ProfitifAlwaysRent ed.value/1
      document.sandy.ProfitifAlwaysRented.value=document.sandy.RentDifference.val ue/1*document.sandy.PeriodinMo.value/1;
      document.sandy.LostRevenue.value=document.sandy.ConsumerRent.value/1*docume nt.sandy.MonthsNotRented.value/1;
      document.sandy.NetProfit.value=document.sandy.ProfitifAlwaysRented.value/1- document.sandy.LostRevenue.value/1;
       }

you have extra spaces between Rent and ed and between val and ue in
lines 2 and 3.
 
C

Captain Paralytic

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.
(sandy is the name of the form)

function checkAll()
     {document.sandy.RentDifference.value=document.sandy.ConsumerRent.value/1-do cument.sandy.BaseRent.value/1;
      document.sandy.ProfitifAlwaysRented.value=document.sandy.ProfitifAlwaysRent ed.value/1
      document.sandy.ProfitifAlwaysRented.value=document.sandy.RentDifference.val ue/1*document.sandy.PeriodinMo.value/1;
      document.sandy.LostRevenue.value=document.sandy.ConsumerRent.value/1*docume nt.sandy.MonthsNotRented.value/1;
      document.sandy.NetProfit.value=document.sandy.ProfitifAlwaysRented.value/1- document.sandy.LostRevenue.value/1;
       }

and between do and cument on line 1.

Aside from that, since you haven't shown us the form, that's all I can
tell you.
 
E

Evertjan.

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:

==================================
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;

};
==================================

btw: Writing to a form textinput the user is not allowed to change
is not a sound idea.

Better write into <div>s or tablecells.
 
S

SAM

Le 9/11/09 7:59 PM, Andrew Pollock a écrit :
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.
(sandy is the name of the form)

function checkAll()

Next line, no need to do /1
Because the operator is '-'
(that automatically converts the terms in numbers)
{document.sandy.RentDifference.value=document.sandy.ConsumerRent.value/1-document.sandy.BaseRent.value/1;

if(document.sandy.ConsumerRent.value != '' &&
document.sandy.BaseRent.value != '')
document.sandy.RentDifference.value=document.sandy.ConsumerRent.value-document.sandy.BaseRent.value;


Next line doing nothing, it can be deleted.
document.sandy.ProfitifAlwaysRented.value=document.sandy.ProfitifAlwaysRented.value/1

Next line, no need to do /1
because you have a * operator
(that automatically converts the terms in numbers)
However maybe if one of the terms is '' the result will be the
conversion in string of the other one.
document.sandy.ProfitifAlwaysRented.value=document.sandy.RentDifference.value/1*document.sandy.PeriodinMo.value/1;

if(document.sandy.RentDifference.value == '')
document.sandy.RentDifference.value = 0;
if(document.sandy.PeriodinMo.value == '')
document.sandy.PeriodinMo.value = 0;
document.sandy.LostRevenue.value=document.sandy.RentDifference.value*document.sandy.PeriodinMo.value;

Next line, same as previous.
document.sandy.LostRevenue.value=document.sandy.ConsumerRent.value/1*document.sandy.MonthsNotRented.value/1;

if(document.sandy.ConsumerRent.value == '')
document.sandy.ConsumerRent.value = 0;
if(document.sandy.MonthsNotRented.value == '')
document.sandy.MonthsNotRented.value = 0;
document.sandy.LostRevenue.value=document.sandy.ConsumerRent.value*document.sandy.MonthsNotRented.value;


Next line same as line 2.
document.sandy.NetProfit.value=document.sandy.ProfitifAlwaysRented.value/1-document.sandy.LostRevenue.value/1;
}

function valToNbr(itemName) {
return document.sandy[itemName].value==''?
0 : document.sandy[itemName].value;
}

function checkAll(){
document.sandy.RentDifference.value=valToNbr('ConsumerRent')-valToNbr('BaseRent');
document.sandy.ProfitifAlwaysRented.value=valToNbr('RentDifference')*valToNbr('PeriodinMo');
document.sandy.LostRevenue.value=valToNbr('ConsumerRent')*valToNbr('MonthsNotRented');
document.sandy.NetProfit.value=valToNbr('ProfitifAlwaysRented')-valToNbr('LostRevenue');
}
 
A

Andrew Pollock

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.
(sandy is the name of the form)

function checkAll()
{document.sandy.RentDifference.value=document.sandy.ConsumerRent.value/1-do
cument.sandy.BaseRent.value/1;
document.sandy.ProfitifAlwaysRented.value=document.sandy.ProfitifAlwaysRent
ed.value/1
document.sandy.ProfitifAlwaysRented.value=document.sandy.RentDifference.val
ue/1*document.sandy.PeriodinMo.value/1;
document.sandy.LostRevenue.value=document.sandy.ConsumerRent.value/1*docume
nt.sandy.MonthsNotRented.value/1;
document.sandy.NetProfit.value=document.sandy.ProfitifAlwaysRented.value/1-
document.sandy.LostRevenue.value/1;
}

and between do and cument on line 1.
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.
(sandy is the name of the form)

function checkAll()
{document.sandy.RentDifference.value=document.sandy.ConsumerRent.value/1-document.sandy.BaseRent.value/1;
document.sandy.ProfitifAlwaysRented.value=document.sandy.ProfitifAlwaysRented.value/1
document.sandy.ProfitifAlwaysRented.value=document.sandy.RentDifference.value/1*document.sandy.PeriodinMo.value/1;
document.sandy.LostRevenue.value=document.sandy.ConsumerRent.value/1*document.sandy.MonthsNotRented.value/1;
document.sandy.NetProfit.value=document.sandy.ProfitifAlwaysRented.value/1-document.sandy.LostRevenue.value/1;
}



Aside from that, since you haven't shown us the form, that's all I can
tell you.

This is strange. none of those spaces show in my original post nor do
they show in the quoted post that others posted. (I'm using Outlook
Express if that makes any difference.
 
S

SAM

Le 9/12/09 3:32 PM, Andrew Pollock a écrit :
My problem is that line 3 doesn't seem to work.
ProfitifAlwaysRented.value doesn't update.

Why do you give successively 2 values to ProfitifAlwaysRented ?
(line 2 then line 3)
What's the value of PeriodinMo ?
(sandy is the name of the form)

function checkAll()
{document.sandy.RentDifference.value=document.sandy.ConsumerRent.value/1-document.sandy.BaseRent.value/1;
document.sandy.ProfitifAlwaysRented.value=document.sandy.ProfitifAlwaysRented.value/1
document.sandy.ProfitifAlwaysRented.value=document.sandy.RentDifference.value/1*document.sandy.PeriodinMo.value/1;
document.sandy.LostRevenue.value=document.sandy.ConsumerRent.value/1*document.sandy.MonthsNotRented.value/1;
document.sandy.NetProfit.value=document.sandy.ProfitifAlwaysRented.value/1-document.sandy.LostRevenue.value/1;
}

Aside from that, since you haven't shown us the form, that's all I can
tell you.

Isn't it to you to show the form?
Didn't you post the original question ?

delete all the '/1' from your code.
(and the line 2)
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
, Fri, 11 Sep 2009 17:59:48, Andrew Pollock <[email protected]> posted:
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.
(sandy is the name of the form)

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;
}

Firstly, you should learn to write unambiguous English. Which is line
3? Do you count function checkAll() or the following line as Line
1? Far better to mark the line by adding trailing comment.

Secondly, you should both read and heed the FAQ about line-length in
News. Break those assignments after "=", indent by the minimum
recommended amount, and your code will be more readable, to you and to
us. Lines of 80 or more characters will not necessarily be seen as you
think you sent them; allowing for subsequent quoting, limit your lines
to about 72 characters as per FAQ.

Thirdly, you can put var DS = document.sandy , and shorten about 14
subsequent references.

Fourthly, do not use /1 to convert a string to a number. Subtraction,
multiplication, division do that automatically; and for addition and
assignment, you can put a unary + before the string.

Fifthly, don't just say "does not work; say what you wanted, and what
actually happened. And say what error messages your browser(s) gave,
including "none" (if you do not see messages from an intentional syntax
error, then rest assured that the intelligence you have displayed would
be better suited to a janitorial career).

If and when your knowledge has advanced sufficiently : input strings
should be pattern-checked before conversion to numbers, by using a
simple RegExp.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
btw: Writing to a form textinput the user is not allowed to change
is not a sound idea.

I cannot support that. In paper forms it is common to have a column of
boxes for user data entry, followed by a similar box for the result of a
simple calculation; that can reasonably be followed on the screen. Of
course, the result box should have a readonly attribute.
 
A

Andrew Pollock

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%">&nbsp;</TD>
<TD WIDTH="70%">&nbsp;</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%">&nbsp; </TD>
</TR>
<TR>
<TD HEIGHT="24" WIDTH="30%">&nbsp;</TD>
<TD HEIGHT="24" WIDTH="70%">&nbsp;</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%">&nbsp;</TD>
<TD WIDTH="70%">&nbsp;</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%">&nbsp;</TD>
<TD WIDTH="70%">&nbsp;</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>&nbsp;</P>
</FORM>
</BODY>

</HTML>
 
A

Andrew Pollock

Dr J R Stockton said:
In comp.lang.javascript message
<[email protected]
[snipped]> Firstly, you should learn to write unambiguous English.
Which is line
3? Do you count function checkAll() or the following line as Line
1? Far better to mark the line by adding trailing comment.

Secondly, you should both read and heed the FAQ about line-length in
News. Break those assignments after "=", indent by the minimum
recommended amount, and your code will be more readable, to you and to
us. Lines of 80 or more characters will not necessarily be seen as
you
think you sent them; allowing for subsequent quoting, limit your lines
to about 72 characters as per FAQ.
[snipped]> (c) John Stockton, nr London UK. [email protected]
IE7 FF3 Op9 Sf3
FAQ
<URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates,
sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,
links.

Sorry your message displayed just after my last post. I appreciate the
help
and don't want you to think I was ignoring your advice about lines.
I thought setting 72 in preferences took care of it. I see now that
isn't
enough.
 
S

SAM

Le 9/12/09 8:27 PM, Andrew Pollock a écrit :
[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.

Of course you did try your code in Firefox and did have a look to its
console of errors ?

Erreur : temp.RentDifference is undefined
Ligne : 28

Here is the whole page code.

[snip]
<INPUT TYPE="text" NAME="RentDifference "VALUE="">

it is :
<INPUT TYPE="text" NAME="RentDifference" VALUE="">

take care where you put your spaces (whitespaces?)
 
A

Andrew Pollock

SAM said:
Of course you did try your code in Firefox and did have a look to its
console of errors ?

Erreur : temp.RentDifference is undefined
Ligne : 28

Here is the whole page code.

[snip]
<INPUT TYPE="text" NAME="RentDifference "VALUE="">

it is :
<INPUT TYPE="text" NAME="RentDifference" VALUE="">
Thanks, that fixed it. I was not getting a useful message in error
console. Only things that made no sense to me.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
var RentDifference = ConsumerRent - BaseRent;
temp.RentDifference.value = RentDifference;
<INPUT TYPE="text" NAME="RentDifference "VALUE="">


You have not said what browsers you are testing in, nor what error
messages you see from Error Console (Firefox) or equivalent.

If you are not using an Error Console or better, you are wasting
everybody's time. Including your own.

Firefox Error Console gives
Error: temp.RentDifference is undefined ... Line: 30

Opera Error Console says more.
Safari clearly indicates that the problem is with RentDifference.
Chrome indicates the offending line.
IE is inscrutable.

In the third quoted line, you gave a space in the name, which is wrong.

It is unwise, IMHO, to use the same "word" for an identifier and a name.
Even id the system does not get confused, readers might. Use, instead,
similar "word"s; let the var be RentDiff, for example.

If, while testing, you initially give the input controls suitable
values, you can test without typing anything in. That saves time, and
ensures reproducible initial testing.
 
E

Evertjan.

Dr J R Stockton wrote on 12 sep 2009 in comp.lang.javascript:
In comp.lang.javascript message <[email protected]>,


I cannot support that. In paper forms it is common to have a column of
boxes for user data entry, followed by a similar box for the result of a
simple calculation; that can reasonably be followed on the screen. Of
course, the result box should have a readonly attribute.

I do not think commonness is equivalent to a good idea.

I think it is a rotten idea.

Output shoud not make the impression of being changable as an input,
and readonly does not change that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top