在 Javascript 中每隔一段时间显示变量时出现问题



代码:

function displayWelcome() {
    console.log("Welcome! nThis program will determine the time to pay off a credit card and the interest paid based on the current balance, the interest rate, and the monthly payments made.")
}
function calculateminimumPaymentment(balance, minimumPaymentRate) {
    return Math.max(20, balance * minimumPaymentRate);
}
function displayPayments(balance, interest, minimumPayment) {
    console.log("Balance on your credit card: $" + balance.toFixed(2))
    console.log("Interest Rate: " + (interest * 100) + "%")
    console.log("Assuming a minimum payment of 2% of the balance ($20 min)")
    console.log("Your minimum payment would be: $" + minimumPayment)
    console.log("nYear    Balance     Payment #     Interest Paid")
    var year = 1;
    var payments = 1;
    var interestPaid = 0;
    while (balance > 0) {
        interestPaid += balance * interest / 12;
        balance = Math.max(0, balance - (minimumPayment - balance * interest / 12));
        console.log(year + "        " + balance.toFixed(2) + "      " + payments + "              " + interestPaid.toFixed(2));
        year++;
        payments++;
    }
}
var balance = 1500;
var minimumPaymentRate = 0.02;
var interest = 0.18;
displayWelcome()
var minimumPayment = calculateminimumPaymentment(balance, minimumPaymentRate);
displayPayments(balance, interest, minimumPayment);

现在的问题是输出显示正确,除了年份计数。这一年应该每 12 次付款重复一次,但由于某种原因,它一直在重复每次付款。我尝试修改循环,但无济于事。这是我得到的输出:

Year    Balance     Payment #     Interest Paid
    1       1492.50     1             22.50
    2       1484.89     2             44.89
    3       1477.16     3             67.16
    4       1469.32     4             89.32
    5       1461.36     5             111.36
    6       1453.28     6             133.28
    7       1445.08     7             155.08
    8       1436.75     8             176.75
    9       1428.31     9             198.31
    10      1419.73     10            219.73
    11      1411.03     11            241.03
    12      1402.19     12            262.19

期望输出:

Year    Balance     Payment #     Interest Paid
    1       1492.50     1             22.50
            1484.89     2             44.89
            1477.16     3             67.16
            1469.32     4             89.32
            1461.36     5             111.36
            1453.28     6             133.28
            1445.08     7             155.08
            1436.75     8             176.75
            1428.31     9             198.31
            1419.73     10            219.73
            1411.03     11            241.03
    2       1402.19     12            262.19

由于这个循环,它不断重复

while (balance > 0) {
    interestPaid += balance * interest / 12;
    balance = Math.max(0, balance - (minimumPayment - balance * interest / 12));
    console.log(year + "        " + balance.toFixed(2) + "      " + payments + "              " + interestPaid.toFixed(2));
    year++;
    payments++;
}

您增加年份并在每次循环执行时显示它。

若要解决此问题,请将增量更改为:

if(payments % 12 == 0) // If multiple of 12 (aka: a year)
    year++;

要按照您想要的方式格式化它(仅在更改时显示年份(,您可以尝试添加如下所示的标志:

var year = 1;
var payments = 1;
var interestPaid = 0
var yearChanged;

while (balance > 0) {
    yearChanged = false; // Automatically set the flag to false
    if(payments % 12 == 0) {
        year++;
        yearChanged = true; // Change it when year change
    }
        interestPaid += balance * interest / 12;
        balance = Math.max(0, balance - (minimumPayment - balance * interest / 12));
                    // if flag = true show year
        console.log(yearChanged ? year : "-" + "        " + balance.toFixed(2) + "      " + payments + "              " + interestPaid.toFixed(2));
        payments++;
}

而不是增加变量year - 如果它连接到payments - 所以 1-12 是第一年,13-24 - 第二年,...您可以将其简化为:

var year = Math.ceil(payments / 12);
console.log(year + "        " + balance.toFixed(2) + "      " + payments + "              " + interestPaid.toFixed(2));

最新更新