Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,250 Programmers. There are 2,151 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

what is wrong with the following code?

 
Reply to this topicStart new topic

what is wrong with the following code?

roadrunner
post 7 May, 2008 - 05:02 PM
Post #1


New D.I.C Head

*
Joined: 7 May, 2008
Posts: 1



CODE


    <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>

User is offlineProfile CardPM
Go to the top of the page
+Quote Post


girasquid
post 7 May, 2008 - 05:50 PM
Post #2


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 953

What is going wrong wiht it? If you don't tell us, we can't tell you.
User is online!Profile CardPM
Go to the top of the page
+Quote Post

mocker
post 7 May, 2008 - 08:14 PM
Post #3


D.I.C Head

**
Joined: 14 Oct, 2007
Posts: 73

What did you write this in? You have typos and formatting errors all over. Use the Firefox Error console for this type of stuff, it would take you a couple seconds, tells you exactly where the error is. When I copied your code to a text editor it gave me warnings about characters in a different format, if you look at your code it shows up a couple spots with
_strPennies
or
_rate
when these should be strPennies and rate . Also the subtraction symbols showed up in the browser as unrecognized characters, so I just replaced them and saved and it worked.

A working copy of your code: (the only things changed were subtraction symbols, a couple line breaks, and the couple random underscores)

CODE

    <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>
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 08:55AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month