在Javascript循环中添加累积值

时间:2016-04-27 13:51:33

标签: javascript loops tabular

我一直在努力获取"利息支付"这个程序中的列与while循环的每个交互一起添加,但它只显示了intrp的下一个值。这是程序:

function displayWelcome() {
    var welcome = "This program will determine the time to pay off a credit card and interest paid based on the current balance, the interest rate, and the monthly payments made.";
    return welcome;
}

function calculateMinimumPayment(bal, intr) {
    var min = bal * intr;
    return min;
}

function displayPayments(bal, intr, min) {
    var top1 = "Balance on your credit card: " + bal + "\nInterest Rate: " + intr + "\nAssuming the minimum payment of 2% of the balance ($20 min)\nYour minimum payment would be $" + min;
    var top2 = "PAYOFF SCHEDULE\n______________\nYear\tBalance\t\tPayment Num\tInterest Paid\n";
    console.log(top1);
    console.log(top2);
    var yearcount = 0;
    var year = 0;
    var paynum = 0;
    while (bal + (intr - min) >= 0) {
        paynum++;
        bal = bal + (intr - min);
        intrp = bal * (intr / 12);
        var tbl1 = parseFloat(bal).toFixed(2) + "\t\t" + paynum + "\t\t" + parseFloat(intrp).toFixed(2);
        if (yearcount % 12 === 0) {
            year = year + 1;
            var tbl2 = " " + year + "      " + tbl1;
        } else {
            var tbl2 = "\t" + tbl1;
        }
        yearcount++;
        console.log(tbl2);
    }
}
console.log(displayWelcome());
console.log(displayPayments(1500, 0.18, calculateMinimumPayment(1500, 0.02)));

这是输出:

This program will determine the time to pay off a credit card and interest paid
based on the current balance, the interest rate, and the monthly payments made.
Balance on your credit card: 1500
Interest Rate: 0.18
Assuming the minimum payment of 2% of the balance ($20 min)
Your minimum payment would be $30
PAYOFF SCHEDULE
______________
Year    Balance         Payment Num     Interest Paid

 1      1470.18         1               22.05
        1440.36         2               21.61
        1410.54         3               21.16
        1380.72         4               20.71
        1350.90         5               20.26
        1321.08         6               19.82
        1291.26         7               19.37
        1261.44         8               18.92
        1231.62         9               18.47
        1201.80         10              18.03
        1171.98         11              17.58
        1142.16         12              17.13
 2      1112.34         13              16.69
        1082.52         14              16.24
        1052.70         15              15.79
        1022.88         16              15.34
        993.06          17              14.90
        963.24          18              14.45
        933.42          19              14.00
        903.60          20              13.55
        873.78          21              13.11
        843.96          22              12.66
        814.14          23              12.21
        784.32          24              11.76
 3      754.50          25              11.32
        724.68          26              10.87
        694.86          27              10.42
        665.04          28              9.98
        635.22          29              9.53
        605.40          30              9.08
        575.58          31              8.63
        545.76          32              8.19
        515.94          33              7.74
        486.12          34              7.29
        456.30          35              6.84
        426.48          36              6.40
 4      396.66          37              5.95
        366.84          38              5.50
        337.02          39              5.06
        307.20          40              4.61
        277.38          41              4.16
        247.56          42              3.71
        217.74          43              3.27
        187.92          44              2.82
        158.10          45              2.37
        128.28          46              1.92
        98.46           47              1.48
        68.64           48              1.03
 5      38.82           49              0.58
        9.00            50              0.14

正如您所看到的,"利息支付"列没有加在一起,它只显示了intrp的当前值。

2 个答案:

答案 0 :(得分:0)

  

我一直在努力获取"利息支付"列中有这个   程序与while循环的每次交互一起添加但是它   只显示intrp的下一个值

这是因为您要将值分配给intrp而不是累积以前的值。

替换

intrp = bal * (intr / 12);

intrp += bal * (intr / 12);

答案 1 :(得分:0)

如果你添加累积兴趣

https://jsfiddle.net/om1a90qc/

function displayPayments(bal, intr, min) {
    var acum = 0;
    var top1 = "Balance on your credit card: " + bal + "\nInterest Rate: " + intr + "\nAssuming the minimum payment of 2% of the balance ($20 min)\nYour minimum payment would be $" + min;
    var top2 = "PAYOFF SCHEDULE\n______________\nYear\tBalance\t\tPayment Num\tInterest Paid\t\tInterest Paid\n";
    console.log(top1);
    console.log(top2);
    var yearcount = 0;
    var year = 0;
    var paynum = 0;
    while (bal + (intr - min) >= 0) {
        paynum++;
        bal = bal + (intr - min);
        intrp = bal * (intr / 12);
        acum = (parseFloat(acum) + parseFloat(intrp)).toFixed(2);
        var tbl1 = parseFloat(bal).toFixed(2) + "\t\t" + paynum + "\t\t" + parseFloat(intrp).toFixed(2) + "\t\t" + acum;
        if (yearcount % 12 === 0) {
            year = year + 1;
            var tbl2 = " " + year + "      " + tbl1;
        } else {
            var tbl2 = "\t" + tbl1;
        }
        yearcount++;
        console.log(tbl2);
    }
}
console.log(displayWelcome());
console.log(displayPayments(1500, 0.18, calculateMinimumPayment(1500, 0.02)));