我正在尝试制作一个javascript贷款计算器程序。我似乎无法让我的循环在每一行上输入不同的数字。假设显示24个月,36个月,48个月和60个月开始支付的金额。我正在使用函数来进行计算,但我总是得到24个月的结果。我知道你必须将nummonths改为36,48和60,但我不知道这样做。我认为循环会在每次循环时增加12个月。另外你会如何将数字格式化为货币?最后我得到一个非常长的数字。我试着在calculate()上做parseFloat,但是我得到一个错误。 这里是我的代码:Javascript循环程序
<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>
谢谢!你如何将结果格式化为货币? – jaramore
我无法使accounting.js正常工作。你怎么做呢? – jaramore
看看这里格式化http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript – Stuart