Lanternkami

03-15-2009, 04:09 PM

So I finnaly finished the APR calculator and it works... for the most part.... sometimes ><. The problem is whenever I run it its hit or miss, the code i had to use can cause a timeout, but its the only way i can write the code, ill post the code below with some comments added in for your convience ^^,

the js code, I am using prototype btw

the code is on my site heres the link: http://cn.iddqd.net/aprcal.html

heres the formulas that the code is suppost to solve, that may help.

http://iddqd.net/help/calchelp.html

//function to remove comma, thx rangana

function stripComma(val) {

return val.replace(/[,\s]/gi,'');

}

//function to round the number to the place of my choosing

function roundNumber(num, dec){

var result = Math.round(num*Math.pow(10,dec))/Math.pow(10, dec);

return result;

}

function apr(){

//declare my variables from the form

var loan = stripComma($('loan').value);

var extra_cost = stripComma($('extra').value);

var interest_rate = stripComma($('rate').value);

var apr;

var months = stripComma($('months').value);

var monthly_payment;

//check to see that all of the fields are filled out

if(loan =="" || extra_cost == "" || interest_rate == "" || months == "")

alert('Missing Information in Fields!');

//if they are all filled out continue with the code.

else {

//firts part of the equation to get the monthly payment

monthly_payment = ((loan + extra_cost) * (interest_rate / 1200) * Math.pow((1 + (interest_rate / 1200)), months)) / ( Math.pow((1 + (interest_rate / 1200)), months) - 1);

//set variables to use in the equation (this is where the problem starts)

var prob = 1;

var a = .30;

// check to see if the answer to the equation was zero (or at least close to zero)

while(prob <= -.40 || prob >= .40){

//if it was greater then zero subract a number from a

if(prob >= 0){

a = a - .005;

}

//if it was less then zero add a number to a

else{

a = a + .005;

}

//solve the equation with a as the inital guess, if the equation is not close to zero repeate with new values

prob = roundNumber(((a * Math.pow((1 + a), months) / (Math.pow((1 + a), months) - 1) - (monthly_payment / loan ))), 2);

}

//format apr

apr = Math.floor(a*1200);

//alert apr

alert("Your APR is " + apr + " %");

}

}

document.observe("dom:loaded", function()

{

$('aprcalc').observe("submit", apr.bind());

});

and the HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<LINK REL=StyleSheet HREF="styles.css" TITLE="Contemporary">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Calculation Nation - Servicing all of your Loan Calculating Needs</title>

<script type="text/javascript" src="prototype.js"></script>

<script type="text/javascript"src="aprfunctions.js"></script>

</head>

<body>

<div id="logo">

<img src="photos/logo.jpg" />

</div>

<div id="content">

<div id="header">

<table>

<tr>

<td><a href="index.html" class="link">Home</a></td>

<td><a href="loancals.html" class="link">Loan Calculators</a></td>

<td><a href="leasecal.html" class="link">Lease Calculators</a></td>

<td><a href="aprcal.html" class="link">APR Calculators</a></td>

<td><a href="links.html" class="link">Usefull Links</a></td>

<td><a href="about.html" class="link">About us</a></td>

</tr>

</table>

</div>

<br />

<div id="containter">

<div id="calc1" class="calculator">

<div id="inner">

<form name="aprcalc" id="aprcalc">

Loan Amount:

<input type="text" name="loan" id="loan"/><br />

Extra Cost:

<input type="text" name="extra" id="extra" /><br />

Intrest Rate:

<input type="text" name="rate" id="rate" /><br />

Number of Months:

<input type="text" name="months" id="months" /><br />

<input type="submit" value="CALCULATE" />

</form>

</div>

</div>

</div>

<div id ="footer">

<p>Made by Marc Pageau © 2009</p>

</div>

</body>

</html>

the js code, I am using prototype btw

the code is on my site heres the link: http://cn.iddqd.net/aprcal.html

heres the formulas that the code is suppost to solve, that may help.

http://iddqd.net/help/calchelp.html

//function to remove comma, thx rangana

function stripComma(val) {

return val.replace(/[,\s]/gi,'');

}

//function to round the number to the place of my choosing

function roundNumber(num, dec){

var result = Math.round(num*Math.pow(10,dec))/Math.pow(10, dec);

return result;

}

function apr(){

//declare my variables from the form

var loan = stripComma($('loan').value);

var extra_cost = stripComma($('extra').value);

var interest_rate = stripComma($('rate').value);

var apr;

var months = stripComma($('months').value);

var monthly_payment;

//check to see that all of the fields are filled out

if(loan =="" || extra_cost == "" || interest_rate == "" || months == "")

alert('Missing Information in Fields!');

//if they are all filled out continue with the code.

else {

//firts part of the equation to get the monthly payment

monthly_payment = ((loan + extra_cost) * (interest_rate / 1200) * Math.pow((1 + (interest_rate / 1200)), months)) / ( Math.pow((1 + (interest_rate / 1200)), months) - 1);

//set variables to use in the equation (this is where the problem starts)

var prob = 1;

var a = .30;

// check to see if the answer to the equation was zero (or at least close to zero)

while(prob <= -.40 || prob >= .40){

//if it was greater then zero subract a number from a

if(prob >= 0){

a = a - .005;

}

//if it was less then zero add a number to a

else{

a = a + .005;

}

//solve the equation with a as the inital guess, if the equation is not close to zero repeate with new values

prob = roundNumber(((a * Math.pow((1 + a), months) / (Math.pow((1 + a), months) - 1) - (monthly_payment / loan ))), 2);

}

//format apr

apr = Math.floor(a*1200);

//alert apr

alert("Your APR is " + apr + " %");

}

}

document.observe("dom:loaded", function()

{

$('aprcalc').observe("submit", apr.bind());

});

and the HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<LINK REL=StyleSheet HREF="styles.css" TITLE="Contemporary">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Calculation Nation - Servicing all of your Loan Calculating Needs</title>

<script type="text/javascript" src="prototype.js"></script>

<script type="text/javascript"src="aprfunctions.js"></script>

</head>

<body>

<div id="logo">

<img src="photos/logo.jpg" />

</div>

<div id="content">

<div id="header">

<table>

<tr>

<td><a href="index.html" class="link">Home</a></td>

<td><a href="loancals.html" class="link">Loan Calculators</a></td>

<td><a href="leasecal.html" class="link">Lease Calculators</a></td>

<td><a href="aprcal.html" class="link">APR Calculators</a></td>

<td><a href="links.html" class="link">Usefull Links</a></td>

<td><a href="about.html" class="link">About us</a></td>

</tr>

</table>

</div>

<br />

<div id="containter">

<div id="calc1" class="calculator">

<div id="inner">

<form name="aprcalc" id="aprcalc">

Loan Amount:

<input type="text" name="loan" id="loan"/><br />

Extra Cost:

<input type="text" name="extra" id="extra" /><br />

Intrest Rate:

<input type="text" name="rate" id="rate" /><br />

Number of Months:

<input type="text" name="months" id="months" /><br />

<input type="submit" value="CALCULATE" />

</form>

</div>

</div>

</div>

<div id ="footer">

<p>Made by Marc Pageau © 2009</p>

</div>

</body>

</html>