1. ## Java script help

Help on Javascript

I’m trying to write a simple javascript form that calculates a figure based on two inputed values. The basis of this form is that we have 3 pay rate at 5.15, 5.24 and 5.49 and three max loan to value rates of 65%, 75% and 85%. The user inputs his property value and current rent and presses submit, then the form calculates the maximum potential loan available and the monthly payments needed. The maximum loan is calculated with the forumula

Rent Per Month * 12/ 1.25 / pay rate * 100

The catch is the max potential loan can not be more than the max loan to value rates (i.e 65%, 75% and 85%). This what I’ve got so far.

function evaluate (string)
{
// Loan to value rate
var lToValue65 = 0.65; //This is 65%
var lToValue75 = 0.75; //This is 75%
var lToValue85 = 0.85; //This is 85%

// Baserate
var baseRate = 0.045 //This is the base rate at 4.5%

// Plus margin
var plusMargin65 = 0.0065; //This is 0.65%
var plusMargin75 = 0.0074; //This is 0.74%
var plusMargin85 = 0.0099; //This is 0.99%

var payRate65 = plusMargin65 + baseRate; //This calculates the total payrate for 65% LTV
var payRate75 = plusMargin75 + baseRate; //This calculates the total payrate for 75% LTV
var payRate85 = plusMargin85 + baseRate; //This calculates the total payrate for 85% LTV

rent65 = Math.Ceil(string.usertext.value*12/1.25/payRate65*100);
rent75 = Math.Ceil(string.usertext.value*12/1.25/payRate75*100);
rent85 = Math.Ceil(string.usertext.value*12/1.25/payRate85*100);

document.evalform.resultbox.value = rent85;

}

I’ve never written javascript before so I’m not sure if I’m the right track here, any advice appreciated as currently this doesn’t even work in IE.

2. give the below a whirl, that main difference is the way you access the value of the textbox, everything else I've left as is.

From the results i think you might want to add brackets to the calculation - it currently says i can have a loan of £4,660,195 if i pay £250 a month rent

anyways, here it comes..

<html>
<title>Loan Calculator</title>
<script language="JavaScript">
function evaluate() {
// Loan to value rate
var lToValue65 = 0.65; //This is 65%
var lToValue75 = 0.75; //This is 75%
var lToValue85 = 0.85; //This is 85%

// Baserate
var baseRate = 0.045 //This is the base rate at 4.5%

// Plus margin
var plusMargin65 = 0.0065; //This is 0.65%
var plusMargin75 = 0.0074; //This is 0.74%
var plusMargin85 = 0.0099; //This is 0.99%

var payRate65 = plusMargin65 + baseRate; //This calculates the total payrate for 65% LTV
var payRate75 = plusMargin75 + baseRate; //This calculates the total payrate for 75% LTV
var payRate85 = plusMargin85 + baseRate; //This calculates the total payrate for 85% LTV

rent65 = Math.ceil(document.frmLoans.txtRent.value * 12 / 1.25 / payRate65 * 100);
rent75 = Math.ceil(document.frmLoans.txtRent.value * 12 / 1.25 / payRate75 * 100);
rent85 = Math.ceil(document.frmLoans.txtRent.value * 12 / 1.25 / payRate85 * 100);

document.frmLoans.resultbox65.value = rent65;
document.frmLoans.resultbox75.value = rent75;
document.frmLoans.resultbox85.value = rent85;
}
</script>
<body>
<form name="frmLoans" method="post" action="somewhere.asp">
Whats your rent? <input type="text" name="txtRent"> <input type="button" value="Calculate" onclick="evaluate()">
<br><br>
65% <input type="text" name="resultbox65"><br>
75% <input type="text" name="resultbox75"><br>
85% <input type="text" name="resultbox85"><br>
</form>
</body>
</html>

HTH!

3. Thanks for that, didn't get to read this until I wrote this code, which doesn't appear to work Any ideas?

<html>

<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>LTV</title>
<SCRIPT LANGUAGE="JavaScript">
function evaluate (string)
{
// Loan to value rate
var lToValue65 = 0.65; //This is 65%
var lToValue75 = 0.75; //This is 75%
var lToValue85 = 0.85; //This is 85%

// Baserate
var baseRate = 0.045 //This is the base rate at 4.5%

// Plus margin
var plusMargin65 = 0.0065; //This is 0.65%
var plusMargin75 = 0.0074; //This is 0.74%
var plusMargin85 = 0.0099; //This is 0.99%

var payRate65 = plusMargin65 + baseRate; //This calculates the total payrate for 65% LTV
var payRate75 = plusMargin75 + baseRate; //This calculates the total payrate for 75% LTV
var payRate85 = plusMargin85 + baseRate; //This calculates the total payrate for 85% LTV

maxMortgage65 = Math.ceil(document.evalform.txtRent.value * 12 / 1.25 / payRate65 * 100); // This forumula calculates the max loan available
maxMortgage75 = Math.ceil(document.evalform.txtRent.value * 12 / 1.25 / payRate75 * 100); // This forumula calculates the max loan available
maxMortgage85 = Math.ceil(document.evalform.txtRent.value * 12 / 1.25 / payRate85 * 100); // This forumula calculates the max loan available

calcRent65 = Math.ceil(payRate65*maxMortgage65); // Calculates the mortgage cost per month
calcRent75 = Math.ceil(payRate75*maxMortgage75); // Calculates the mortgage cost per month
calcRent85 = Math.ceil(payRate85*maxMortgage85); // Calculates the mortgage cost per month

document.evalform.prop65.value = maxMortgage65;
document.evalform.prop75.value = maxMortgage75;
document.evalform.prop85.value = maxMortgage85;

document.evalform.rent65.value = calcRent65;
document.evalform.rent75.value = calcRent75;
document.evalform.rent85.value = calcRent85;

}
</SCRIPT>

<body>

<FORM NAME="evalform">
<p><b><font face="Verdana">Property value:&nbsp;&nbsp;&nbsp; £</font></b> <font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B><INPUT NAME="propVal" TYPE="text" SIZE=12><br>
Rent per month:&nbsp; £ <INPUT NAME="currentRent" TYPE="text" SIZE=12></B></font><INPUT NAME="button" TYPE="button" VALUE="Submit" OnClick="evaluate(document.evalform)" style="float: left"></p>
<p align="left">&nbsp;</p>

<table border="0" width="40%" id="table1">
<tr>
<td>LTV</td>
<td>Max mortgage</td>
<td>Mortgage cost per month</td>
</tr>
<tr>
<td>65% LTV</td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="prop65" TYPE="text" SIZE=12></B></font></td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="rent65" TYPE="text" SIZE=12></B></font></td>
</tr>
<tr>
<td>75% LTV</td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="prop75" TYPE="text" SIZE=12></B></font></td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="rent75" TYPE="text" SIZE=12></B></font></td>
</tr>
<tr>
<td>85% LTV</td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="prop85" TYPE="text" SIZE=12></B></font></td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="rent85" TYPE="text" SIZE=12></B></font></td>
</tr>
</table>
<p>&nbsp;</p>

</FORM>

</body>

</html>

4. that's only not working because you're looking to change the value of txtRent in your JS, but the field is called currentRent - if you change either of those to match, Robert will be related to you in some way.

5. rubbishrubbishrubbishrubbishrubbishey, cheers for the heads up. Here's the latest version of it but it's not quiet working correctly.
<html>

<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>LTV</title>
<SCRIPT LANGUAGE="JavaScript">
function evalmax ()
{
// Tidy-up input values
var CurRent = parseInt(document.evalform.currentRent.value);
var CurVal = parseInt(document.evalform.propVal.value);

// Loan to value rate
var lToValue65 = 0.65; //This is 65%
var lToValue75 = 0.75; //This is 75%
var lToValue85 = 0.85; //This is 85%

// Baserate
var baseRate = 0.045; //This is the base rate at 4.5%

// Plus margin
var plusMargin65 = 0.0065; //This is 0.65%
var plusMargin75 = 0.0074; //This is 0.74%
var plusMargin85 = 0.0099; //This is 0.99%

var payRate65 = plusMargin65 + baseRate; //This calculates the total payrate for 65% LTV
var payRate75 = plusMargin75 + baseRate; //This calculates the total payrate for 75% LTV
var payRate85 = plusMargin85 + baseRate; //This calculates the total payrate for 85% LTV

var maxMortgage65 = Math.ceil(CurRent * 12 / 1.25 / payRate65 * 100);

if (maxMortgage65<(0.65*CurVal))
{
maxMortgage65 = maxMortgage65;
}
else
{
maxMortage65 = 0.65*CurVal;
}

var maxMortgage75 = Math.ceil(CurRent * 12 / 1.25 / payRate75 * 100);

if (maxMortgage75<(0.75*CurVal))
{
maxMortgage75 = maxMortgage75;
}
else
{
maxMortgage75 = 0.75*CurVal;
}

var maxMortgage85 = Math.ceil(CurRent * 12 / 1.25 / payRate85 * 100);

if (maxMortgage85<(0.85*CurVal))
{
maxMortgage85 = maxMortgage85;
}
else
{
maxMortgage85 = 0.85*CurVal;
}

document.evalform.prop65.value = maxMortgage65;
document.evalform.prop75.value = maxMortgage75;
document.evalform.prop85.value = maxMortgage85;

var calcRent65 = Math.ceil(payRate65*CurRent);
var calcRent75 = Math.ceil(payRate75*CurRent);
var calcRent85 = Math.ceil(payRate85*CurRent);

document.evalform.rent65.value = calcRent65;
document.evalform.rent75.value = calcRent75;
document.evalform.rent85.value = calcRent85;
}
</SCRIPT>

<body>

<FORM NAME="evalform">
<p><b><font face="Verdana">Property value: £</font></b>
<font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B><INPUT NAME="propVal" TYPE="text" SIZE=12><br>Rent per month: £
<INPUT NAME="currentRent" TYPE="text" SIZE=12></B>
</font>
<INPUT NAME="button" TYPE="button" VALUE="Submit" OnClick="evalmax()" style="float: left"></p>
<p align="left"> </p>

<table border="0" width="40%" id="table1">
<tr>
<td>LTV</td>
<td>Max Mortgage</td>
<td>Mortgage cost per month</td>
</tr>
<tr>
<td>65% LTV</td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="prop65" TYPE="text" SIZE=12></B></font></td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="rent65" TYPE="text" SIZE=12></B></font></td>
</tr>
<tr>
<td>75% LTV</td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="prop75" TYPE="text" SIZE=12></B></font></td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="rent75" TYPE="text" SIZE=12></B></font></td>
</tr>
<tr>
<td>85% LTV</td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="prop85" TYPE="text" SIZE=12></B></font></td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="rent85" TYPE="text" SIZE=12></B></font></td>
</tr>
</table>
<p> </p>

</FORM>

</body>

</html>
First problem: This forumula does not work correctly

var maxMortgage65 = Math.ceil(CurRent * 12 / 1.25 / payRate65 * 100);

And neither do the two below it, in excel it's this forumla "=E15*12/1.25/5.15*100" (where E15 is 700 and the answer is 112000). Any help on that appriciated as the javascript is not giving me the correct answer, it seems to be missing the decimal point, why is this?

Secondly. The 3 property value boxes at the bottom should never exceed 65, 75 or 85% of the value the inputed property value. I was thinking of using a series of IF statements which I've put in but do not seem to make any difference, have these been coded correctly? Is this the best way or is there a more elegant solution?

6. I'm having difficulty with some calculation I'm trying to achieve in javascript

I'm converting this Excel formula

=E15*12/1.25/5.15*100 (where E15 is a random, say 550) and the answer is 102524.2718.

When I converted this into a java script calculation I did this.

var maxMortgage65 = Math.ceil(CurRent * 12 / 1.25 / payRate65 * 100);

(payRate in this example would be 5.15)

But this gave an answer of 10252500, which is wrong.

So I've tried to rail the calculation to force it to do it in order

var temp = Math.ceil(CurRent * 12);
var temp2 = Math.ceil(temp / 1.25);
var temp3 = Math.ceil(temp2 / payRate65);
var temp4 = Math.ceil(temp3 * 100);
var maxMortgage65 = Math.ceil(temp4);

Which gave the same answer, 10252500. Why is javascript doing this? I've tried it without the Math.ceil method but that made no difference (except adding numbers after the decimal point). Any ideas?

7. bracketed up your expression would then be:
Math.ceil((((CurRent * 12) / 1.25) / payRate65) * 100)
but as above this gives the same answer. you may want to play with the brackets to ensure the BODMAS rules apply correctly.

the reason the 3 property value boxes are not giving you a percentage of the total value is because they are not using it in the calculation.

I think it'd be worth you going through the maths of this to make sure you've got it down right. having the answer divided by 1.25 seems odd to me - but im not up on your system so it could be right

8. rubbishrubbishrubbishrubbishrubbishey, thanks for your help but I managed to get it working before reading here for any updates. For reference here is the whole code

<html>

<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>LTV</title>
<SCRIPT LANGUAGE="JavaScript">
function evalmax ()
{
// Tidy-up input values
var CurRent = parseInt(document.evalform.currentRent.value);
var CurVal = parseInt(document.evalform.propVal.value);

// Loan to value rate
var lToValue65 = 0.65; //This is 65%
var lToValue75 = 0.75; //This is 75%
var lToValue85 = 0.85; //This is 85%

// Baserate
var baseRate = 4.5; //This is the base rate at 4.5%

// Plus margin
var plusMargin65 = 0.65; //This is 0.65%
var plusMargin75 = 0.74; //This is 0.74%
var plusMargin85 = 0.99; //This is 0.99%

var payRate65 = plusMargin65 + baseRate; //This calculates the total payrate for 65% LTV
var payRate75 = plusMargin75 + baseRate; //This calculates the total payrate for 75% LTV
var payRate85 = plusMargin85 + baseRate; //This calculates the total payrate for 85% LTV

temp65 = 0.65*CurVal;
temp75 = 0.75*CurVal;
temp85 = 0.85*CurVal;

maxMortgage65 = Math.ceil(CurRent * 12 / 1.25 / payRate65 * 100);

if (maxMortgage65 < temp65)
{
maxMortgage65 = maxMortgage65;
}
else
{
maxMortgage65 = temp65;
}

var maxMortgage75 = Math.ceil(CurRent * 12 / 1.25 / payRate75 * 100);

if (maxMortgage75 < temp75)
{
maxMortgage75 = maxMortgage75;
}
else
{
maxMortgage75 = temp75;
}

var maxMortgage85 = Math.ceil(CurRent * 12 / 1.25 / payRate85 * 100);

if (maxMortgage85 < temp85)
{
maxMortgage85 = maxMortgage85;
}
else
{
maxMortgage85 = temp85;
}

document.evalform.prop65.value = maxMortgage65;
document.evalform.prop75.value = maxMortgage75;
document.evalform.prop85.value = maxMortgage85;

var calcRent65 = Math.ceil(0.0515*CurVal);
var calcRent75 = Math.ceil(0.0524*maxMortgage75);
var calcRent85 = Math.ceil(0.0549*maxMortgage85);

document.evalform.rent65.value = calcRent65;
document.evalform.rent75.value = calcRent75;
document.evalform.rent85.value = calcRent85;
}
</SCRIPT>

<body>

<FORM NAME="evalform">
<p><b><font face="Verdana">Property value: £</font></b>
<font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B><INPUT NAME="propVal" TYPE="text" SIZE=12><br>Rent per month: £
<INPUT NAME="currentRent" TYPE="text" SIZE=12></B>
</font>
<INPUT NAME="button" TYPE="button" VALUE="Submit" OnClick="evalmax()" style="float: left"></p>
<p align="left"> </p>

<table border="0" width="40%" id="table1">
<tr>
<td>LTV</td>
<td>Max Mortgage</td>
<td>Mortgage cost per month</td>
</tr>
<tr>
<td>65% LTV</td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="prop65" TYPE="text" SIZE=12></B></font></td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="rent65" TYPE="text" SIZE=12></B></font></td>
</tr>
<tr>
<td>75% LTV</td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="prop75" TYPE="text" SIZE=12></B></font></td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="rent75" TYPE="text" SIZE=12></B></font></td>
</tr>
<tr>
<td>85% LTV</td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="prop85" TYPE="text" SIZE=12></B></font></td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="3">
<B>£<INPUT NAME="rent85" TYPE="text" SIZE=12></B></font></td>
</tr>
</table>
<p> </p>

</FORM>

</body>

</html>
Along with the calculation being in-correct I was using values that work for some percentage calculations and not for others.

The payRate var was calculated from two other var's that were 0.0045 and 0.0065, this worked great for doing a simple figure*payRate to discover the percentage but it didn't work correctly in the above calculation. So I changed the var's that calculated pay rate to simply 4.5 and 0.65 which fixed the problem and declared new var's for anything figure*payRate.

This was really a problem created by some sloppy programming by me (I think), but glad it's all done now.

Once, appricate the help, the things you said jogged my brain into thinking the problem was further up the code

9. glad i could help, but it seems to me thats only working due to the if statements?
when i test it the max mortgage values are still ridculously huge and always invoke the ifs. you may as well forget all the calculations and just say Mortgage65 = CurVal*0.65