• Notice regarding image hosts and attachments. See here.

Javascript loans calculator

Associate
Joined
7 Jun 2010
Posts
447
Location
Wales
Basically Im trying to make a loans calculator that will display the following in a new window;

  • Your interest rate is charged at...
  • Your monthly payment is £
  • The total interest paid at the end of the loan will be £

I have based my coding from the following link...
http://www.java2s.com/Code/JavaScript/Form-Control/JavaScriptLoanCalculator.htm

I've tried everything I can think of through looking at tutorials and what not... but it just doesnt work.

Anyone can lend a favour and push me in the right direction?

many thanks, dave

Code:
<head><title> Loan Calculator</title></head>
<body>
<center><h1>Welcome to the Loan Calculator</h1></center>

<br>
<p><h4><center>This web page will allow you to calculate the interest rate.</center></h4></p>
<h4><center>Follow the steps below to calculate the interest on the loan you have taken out.</center></h4>
<br>


<center>

<style type="text/css">
<!--
body{	font-family: Verdana, Arial, Helvetica, sans-serif;
	color: #003366;
}
body {
	background-color: #62B0FF;
}
-->
</style>


<form name="loandata">
  <table>
    <tr><td colspan="3"><b>Enter Loan Information:</b></td></tr>
    <tr>

      <td>1)</td>
      <td>Amount of the loan:</td>
      <td><input type="text" name="amount" size="12" 
                 onchange="calculate();"></td>
    </tr>
    <tr>
      <td>2)</td>
      <td>Monthly percentage rate of interest:</td>
      <td><input type="text" name="interest" size="12" 
                 onchange="calculate();"></td>
	
	</td>

    </tr>
    <tr>
      <td>3)</td>
      <td>Repayment period in years:</td>
      <td><input type="text" name="years" size="12" 
                 onchange="calculate();"></td>
    </tr>

    <tr><td colspan="3">


<input type="Submit" value="calculate" onclick="calculate()" >

    </td></tr>
    <tr><td colspan="3">


  </table>


</form>
</center>
</body>

<script language="JavaScript">
	
	function calculate() 
	{


    var amount = document.loandata.amount.value;
    var interest = document.loandata.interest.value / 100 / 12;
    var payments = document.loandata.years.value * 12;

    var x=Math.pow((1+i),(-payments));
    var y=1-x;
    var z=amount*(i/y);

	
	newWin=window.open ('','NewWindow', 'width=500,height=400');

	newWin.document.write("</br>" + "<b>Your monthly payment is £</b>" + sterling   + "</br>");
		
	newWin.document.write("</br>" + "<b>Your interest rate is charged at </b>" + finalinterest() +"%" + " over " + (payments) + " months" + "</br>");
				
	newWin.document.write("</br>" + "<b>The total interest paid at the end of the loan will be £</b>" + sterling (((z * payments)-amount)));
	
	newWin.document.close(); " >



</script>
	
	
	
<script language="JavaScript" type="text/javascript">

function finalinterest()
{

if (loandata.amount.value <=4000)
{
   i=9.9

}
else if ((loandata.amount.value >4000) && (loandata.amount.value<=10000))
{
   i=7.9

}
else if ((loandata.amount.value >10000) && (loandata.amount.value<=14000))
{
   i=5.9

}
else if ((loandata.amount.value >14000) && (loandata.amount.value<=20000))
{
   i=4.9

 }
else if (loandata.amount.value>=20000)
{
   i=4.0

}
return i;
}



</script>


</html>
 
Biggest issue is the missing } after the calculate method mate. There are a lot of syntax issues such as missing var declarations, misidentified references to input field values.

Grab firefox and firebug and go debugging dude.

Is this just something you want to work or are you looking to learn something from it too?

Cheers mate, im just installing firebug now. Well I need it working but also need to learn from it too. Not sure where you mean to add the } ??

Could you point out which var's im missing?

added <html> tab dunno why that was missing!
 
Last edited:
Cheers fez,

Sorted them changes out, still unsure what i've gotta do with the i method tho. And not to sure what you mean with regards to the document.close section, should I just remove it? Also when the calculate button is clicked it doesn't even open a new window let alone perform the calcuations??
 
I missed out a function:

Code:
function sterling 
{
return Math.round(x*100)/100;
}

which is used to make the result pounds and pence.

As for the math I was given;
this formula

monthly payment = amount of loan multiplied by
equatioc.jpg

(where i =MAY/1-/199, n = number of repayments)

That code is what I came up with from the forumala?? doubtful thats its correct!

many thanks man
 
Last edited:
Hey mate im not sure what you been? where abouts do I add the alerts and how do I run them??

Also I have removed the Monthly percentage rate of interest: textbox as it is not required because of the if statement. Here is my updated code.. still not working tho.

Code:
<html>
<head><title> Loan Calculator</title></head>
<body>
<center><h1>Welcome to the Loan Calculator</h1></center>

<br>
<p><h4><center>This web page will allow you to calculate the interest rate.</center></h4></p>
<h4><center>Follow the steps below to calculate the interest on the loan you have taken out.</center></h4>
<br>


<center>

<style type="text/css">
<!--
body{	font-family: Verdana, Arial, Helvetica, sans-serif;
	color: #003366;
}
body {
	background-color: #62B0FF;
}
-->
</style>


<form name="loandata" onSubmit="calculate()">
  <table>
    <tr><td colspan="3"><b>Enter Loan Information:</b></td></tr>
    <tr>

      <td>1)</td>
      <td>Amount of the loan:</td>
      <td><input type="text" name="amount" size="12"></td>
    </tr>
   
    <tr>
      <td>2)</td>
      <td>Repayment period in years:</td>
      <td><input type="text" name="years" size="12"></td>
    </tr>

    <tr><td colspan="3">


<input type="Submit" value="Calculate">

    </td></tr>
    <tr><td colspan="3">


  </table>


</form>
</center>
</body>

<script language="javascript" type="text/javascript">
	
	function calculate(){
    var i = finalInterest();
    
    var amount = document.loandata.amount.value;
    var interest = document.loandata.interest.value / 100 / 12;
    var payments = document.loandata.years.value * 12;


    var x=Math.pow((1+i),(-payments));
    var y=1-x;
    var z=amount*(i/y);

	
	newWin=window.open ('','NewWindow', 'width=500,height=400');

	newWin.document.write("</br>" + "<b>Your monthly payment is &pound;</b>" + sterling(x)   + "</br>");
		
	newWin.document.write("</br>" + "<b>Your interest rate is charged at </b>" + finalinterest() +"%" + " over " + (payments) + " months" + "</br>");
				
	newWin.document.write("</br>" + "<b>The total interest paid at the end of the loan will be £</b>" + sterling(x) (((z * payments)-amount)));
	
	
	
	//newWin.document.close();
}



function sterling(inVar)
{
 return Math.round(inVar*100)/100;
}


</script>
	
	
	
<script language="javascript" type="text/javascript">

function finalInterest(){

  var i = 10; 
  
  if (loandata.amount.value <=4000){
     i=9.9;
  }else if ((loandata.amount.value >4000) && (loandata.amount.value<=10000)){
     i=7.9;
  }else if ((loandata.amount.value >10000) && (loandata.amount.value<=14000)){
     i=5.9;
  }else if ((loandata.amount.value >14000) && (loandata.amount.value<=20000)){
     i=4.9;
  }else if (loandata.amount.value>=20000){
     i=4.0;
  }
  
  return i;
}


</script>


</html>
 
I dont understand how its producing 87.87 when i enter 1000 and 1 year.. shouldn't it be 99? From your above code.
 
Back
Top Bottom