Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 2 of 2
Thread: Need Help Debugging

03152009, 05:09 PM #1
 Join Date
 Mar 2009
 Posts
 9
 Thanks
 0
 Thanked 0 Times in 0 Posts
Need Help Debugging
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
Code://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()); });
Code:<!DOCTYPE html PUBLIC "//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <LINK REL=StyleSheet HREF="styles.css" TITLE="Contemporary"> <meta httpequiv="ContentType" content="text/html; charset=utf8" /> <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>
Last edited by Lanternkami; 03152009 at 05:14 PM. Reason: Fixed an error in the code  was missing a )
03162009, 09:41 AM
#2
Well, I suspect that the reason you sometimes get no answer is because you possibly end up just "oscillating" out of the "range" you are allowing.
Example:
You start with an APR of 30% (0.30). Let's say that the actual APR is 29.0025
So you'll keep subtracting 0.005 from 0.30 until you get to 29.000 and you'll find that that's too small. So you'll add on 0.005 and be back at 29.005 and you'll find that that's too large. So you'll subtract 0.005 and be back at 29.000 and... Well, you get the idea.
The problem is that your code may *NEVER* do what is known as "converge".
If you look at the code I showed you, I started at 50% and then I added or subtracted 25% the first iteration. But then I cut the ADDITION VALUE IN HALF! So the next time I added or subtracted 12.5%. And then the next time 6.25%. And then 3.125%. And so on. EACH TIME, my add/subtract value got smaller and smaller. And so as long as the true APR is in the range of 0% to 99.999999%, I would CONVERGE on the correct answer.
The other obvious problem I see is that you report the APR only to the nearest integer percent value. This is inadequate. The US government stipulates that APRs must be accurate to within a quarter of a percent (might be more accurate than that, nowadays...that accuracy was required back in the days before financial calculators and desktop computers, when people just used big tables).
The biggest problem of all, though, is that you precalculate the monthly payment using the interest rate the user typed in. WRONG! When calculating the APR, you MUST IGNORE the interest rate from the user! You use the MONTHLY PAYMENT the user typed in! (Along with the loan amount and the number of months.) You are *SOLVING* for the interest rate, so how can you use what they typed in???
So...in short...try again.