我正在编写一个程序来计算 24,36,48和60个月的每月贷款。
我想知道我应该调用循环内部或外部的函数,然后将我的输出放在循环中?
我想这些函数应该进入循环,因为我调用函数4次,这样就会循环遍历每个函数?或者,它是否只是循环遍历每个函数4次?
我也希望循环从24开始,每次增加12,直到它变为60.所以我在for
循环代码中给了一个镜头,但我不确定。< / p>
代码:
<!DOCTYPE html>
<html>
<head>
<title>Chapter 6 Assignment 2</title>
<meta charset="UTF-8">
<style>
body{
background-color: grey;
}
</style>
</head>
<body>
<script type="text/javascript">
vehiclePrice = +prompt("What is the vehicle price? ","");
moneyDown = +prompt("How much are you putting down? ","");
interestRate = +prompt("What is the interest rate for your loan? ","");
numMonths;
loanAmount = vehiclePrice - moneyDown;
MonthlyRate = interestRate / 1200;
function monthly_due(interestRate, numMonths, loanAmount){
var base = Math.pow(1 + interestRate, numMonths);
var payment = loanAmount * interestRate / (1 - (1/base));
return payment
}
//make function calls here? Function needs to be called 4 times for example
//monthly_due(interestRate, 24, loanAmount);
//monthly_due(interestRate, 36, loanAmount);
//monthly_due(interestRate, 48, loanAmount);
//monthly_due(interestRate, 60, loanAmount);
for (var count = 24; count <= 60; count += 12){
document.write("Number of months: ");
document.write("<br>");
document.write(count); // to display 24, and 12 each time it loops?
document.write("<br>");
doucment.write("Monthly Payment: ");
document.write("<br>");
document.write(monthly_due(interestRate, 24, loanAmount); //make function call here?
}
答案 0 :(得分:1)
我会这样:
设置一个数组,其中包含调用该函数的月份。 这种方法的好处是,如果您需要在任何其他“时间”(月)调用它,您只需将值添加到数组中即可:
var months = [24, 36, 48, 60];
months.forEach(function (month) {
console.log(month);
// call function with parameters
});
我想我想知道我会调用里面的函数 循环我们的外部,然后把我的输出放在循环中?
因此,使用上述解决方案,您需要在循环内调用该函数。但是如果你需要在另一个给定的时间内调用该函数,你只需要更改months
的数组。
我想函数应该进入循环因为我调用了 功能4次,那样它循环每一个?或者它会 只是循环遍历每个函数4次?
它将在给定时间内调用该函数,它在months