Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中以间隔显示变量时出现问题_Javascript - Fatal编程技术网

在Javascript中以间隔显示变量时出现问题

在Javascript中以间隔显示变量时出现问题,javascript,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 calculatemin

守则:

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

不是增加
变量-如果它与
付款相关
-因此1-12是第一年,13-24是第二年。。。您可以将其简化为:

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

不是增加
变量-如果它与
付款相关
-因此1-12是第一年,13-24是第二年。。。您可以将其简化为:

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

因为这个循环,它一直在重复

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++;
}

因为这个循环,它一直在重复

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++
,为什么它不应该在每一行上递增?更改代码,以便当
付款
是12的倍数时,您只增加
。您所需的输出显示每年11笔付款-是否正确?或者应该是每年12次付款(每月1次)?@JamesMonger这是一个错误,意味着要在13次付款中使用它。你在循环中每次都使用
year++
,为什么它不应该在每一行上递增?更改代码,以便当
付款
是12的倍数时,您只增加
。您所需的输出显示每年11笔付款-是否正确?还是应该是每年12次付款(每月1次)?@JamesMonger这是一个错误,意味着要在13次付款中支付。