0

I'm trying to make a javascript loan calculator program. I can't seem to get my loop to put different numbers on each line. It's suppose to show the amount of money to be paid off starting at 24 months, 36 months, 48 months, and 60 months. I'm using a function to do the calculations but I'm always getting the results for 24 months. I know you got to change the nummonths to 36, 48, and 60 but I have no idea to do that. I thought the loop would add the 12 months each time it loops. Also how would you format numbers to currency? I'm getting a really long number at the end. I tried doing parseFloat on calculate() but I'm getting an error. Here is my code:

<html>
<BODY BGCOLOR="#FFC0CB">
<head>
<title>Chapter 6 Assignment 2</title>
</head>
<body>
<h1>Loan Calculator</h1>

<script type="text/javascript">
var vehicleprice = window.prompt("What is the vehicle price?", "");
var downpayment = window.prompt("What is the amount of the down payment?", "");
var annualinterest = window.prompt("What is the annual interest rate for the loan?", "");
var nummonths = 24
var loanamount = vehicleprice - downpayment
var monthlyinterest = annualinterest / 1200

vehicleprice = parseFloat(vehicleprice).toFixed(2);
downpayment = parseFloat(downpayment).toFixed(2);
loanamount = parseFloat(loanamount).toFixed(2);

function calculate()
             {
              var baseamount = Math.pow(1 + monthlyinterest, nummonths );
              return loanamount * monthlyinterest / (1 - (1/baseamount));
             }

document.write("Vehicle price: $" +vehicleprice+ "<br>");
document.write("Down payment: $" +downpayment+ "<br>");
document.write("Interest Rate: " +annualinterest+ "%<br>");
document.write("Loan Amount: $" +loanamount+ "<br>");

for (var count=2;count<=6;count+=1)
    {
    document.write(+calculate()+"<br />");
    }
</script>
</body>
</html>

2 Answers 2

2

Ok, you'll have to increment the nummonths by yourself. Like this...

for (var count=2;count<=6;count+=1)
{
    document.write(+calculate()+"<br />");
    nummonths += 12;
}

Also, you can use http://josscrowcroft.github.io/accounting.js/ to do the currency formatting.

Sign up to request clarification or add additional context in comments.

1 Comment

have a look here for formatting stackoverflow.com/questions/149055/…
2

Since you're not using count for anything, you can loop over nummonths instead:

for (nummonths = 24; nummonths <= 60; nummonths += 12) {
    document.write(calculate() + "<br />");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.