hvumonsen@hotmail.com Posted October 14, 2006 Share Posted October 14, 2006 I am an student at the Norwegian Oslo University Collage where I study Computer Science.I have a project in Javascript where I'm goint to make a Loan Calculator...I did, but it did not work. Why? what's wrong? <HTML> <HEAD> <TITLE>Loan Calculator</TITLE> <script LANGUAGE="JavaScript"> <!-- hide from non-JavaScript browsers function roundToPennies(n) { pennies = n * 100; pennies = Math.round(pennies); strPennies = new String(pennies); len = strPennies.length; return strPennies.substring(0, len — 2) + "." + _strPennies.substring(len — 2, len); } function Monthly(principal, years, apr) { rate = apr / 12; payments = years * 12; return roundToPennies(principal * rate / (1 — (1 / Math.pow(1 + _rate, payments)))); } function MonthlyAmortization(principal, years, apr) { payments = years * 12; monthlyInterest = apr / 12; monthlyPayment = Monthly(principal, years, apr); document.write("<CENTER>"); document.write("<H1>Amortization Table</H1>"); document.write("<HR>"); document.write("<TABLE BORDER>"); document.write("<TR>"); document.write("<TH COLSPAN=4>"); document.write("$" + roundToPennies(principal)); document.write(" at " + roundToPennies(apr*100) + "%"); document.write(" over " + years + " years.<BR>"); document.write("Monthly payment: $" + Monthly(principal, years, apr)); document.write("</TH>"); document.write("</TR>"); document.write("<TR>"); document.write("</TH>"); document.write("<TH COLSPAN=2>Payment</TH>"); document.write("</TR>"); document.write("<TR>"); document.write("Month</TH>"); document.write("Interest</TH>"); document.write("Principal</TH>"); document.write("Balance</TH>"); document.write("</TR>"); for(i = 1; i <= payments; i++) { document.write("<TR>"); document.write("<TD>" + i + "</TD>"); interestPayment = principal * monthlyInterest; document.write("<TD>$" + roundToPennies(interestPayment) + _"</TD>"); principalPayment = monthlyPayment - interestPayment; document.write("<TD>$" + roundToPennies(principalPayment) + _"</TD>"); principal -= principalPayment; document.write("<TD>$" + roundToPennies(principal) + "</TD>"); document.write("</TD>"); } document.write("</TABLE>"); document.write("</CENTER>"); } function compute(form) { if((form.principal.value.length != 0) && (form.apr.value.length != 0) && (form.years.value.length != 0)) { principal = eval(form.principal.value); apr = eval(form.apr.value) / 100.0; years = eval(form.years.value); if(years == 0.0) { alert( "You have no monthly payment, since the number of years is zero."); } else { MonthlyAmortization(principal, years, apr); } } else { alert("You must fill in all the fields."); } } //--> </SCRIPT> </HEAD> <BODY> <CENTER><H1>Loan Calculator</H1></CENTER> <HR> <CENTER> <FORM> <CENTER> Fill in the fields, then click <INPUT TYPE=BUTTON VALUE="Amortize!" onClick=compute(this.form)> </CENTER> <P> <TABLE BORDER=3> <TR> <TD>Amount of the loan ($)</TD> <TD><INPUT TYPE=TEXT NAME=principal></TD> </TR> <TR> <TD>Annual interest rate (%)</TD> <TD><INPUT TYPE=TEXT NAME=apr></TD> </TR> <TR> <TD>Total number of years</TD> <TD><INPUT TYPE=TEXT NAME=years></TD> </TR> </TABLE> </FORM> </CENTER> </BODY> </HTML> Link to comment Share on other sites More sharing options...
jesh Posted October 16, 2006 Share Posted October 16, 2006 I couldn't get your code to run either. I'm not sure if it's because of the way this forum handled your text or if it's the language setting on your computer, but it looks like rather than putting hyphens where you want to subtract, there are emdashes.Instead of:return strPennies.substring(0, len — 2) + "." + strPennies.substring(len — 2, len);You might try:return strPennies.substring(0, len - 2) + "." + strPennies.substring(len - 2, len);If this doesn't help, please post any error messages you are getting and describe in more detail what about this code isn't working. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now