表格式化Javascript

时间:2016-04-23 10:51:08

标签: javascript loops formatting

我在使用此程序时遇到问题。我似乎无法弄清楚如何获得今年的第一个数字(注意第一年缺少)。我还将我的while循环格式化为零,但我仍然得到-20.82并且未定义在表的底部。最后,我看了一遍,但我无法找到正确的公式来计算我觉得我几乎得到了它,但我错过了一个重要的部分。请帮忙!

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\n______________\nYear\tBalance\t\tPayment Num\tInterest Paid\n";
console.log(top1);
console.log(top2);
var yearcount = 0;
var year = 1;
var paynum = 0;
while (bal >= 0) {
    paynum++;
    yearcount++;
    bal = (bal+(intr-min));
    intrp = intr+(bal*min);
    var tbl1 = parseFloat(bal).toFixed(2) + "\t\t" + paynum + "\t\t" + parseFloat(intrp).toFixed(2);
    var tbl2 = " " + year + "      " + tbl1;
    if (yearcount % 12 === 0) {
        year = year+1;
        var tbl2 = " " + year + "      " + tbl1;
    } else {
        tbl2 = "\t" + tbl1;
    }
    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

        1470.18         1               44105.58
        1440.36         2               43210.98
        1410.54         3               42316.38
        1380.72         4               41421.78
        1350.90         5               40527.18
        1321.08         6               39632.58
        1291.26         7               38737.98
        1261.44         8               37843.38
        1231.62         9               36948.78
        1201.80         10              36054.18
        1171.98         11              35159.58
 2      1142.16         12              34264.98
        1112.34         13              33370.38
        1082.52         14              32475.78
        1052.70         15              31581.18
        1022.88         16              30686.58
        993.06          17              29791.98
        963.24          18              28897.38
        933.42          19              28002.78
        903.60          20              27108.18
        873.78          21              26213.58
        843.96          22              25318.98
        814.14          23              24424.38
 3      784.32          24              23529.78
        754.50          25              22635.18
        724.68          26              21740.58
        694.86          27              20845.98
        665.04          28              19951.38
        635.22          29              19056.78
        605.40          30              18162.18
        575.58          31              17267.58
        545.76          32              16372.98
        515.94          33              15478.38
        486.12          34              14583.78
        456.30          35              13689.18
 4      426.48          36              12794.58
        396.66          37              11899.98
        366.84          38              11005.38
        337.02          39              10110.78
        307.20          40              9216.18
        277.38          41              8321.58
        247.56          42              7426.98
        217.74          43              6532.38
        187.92          44              5637.78
        158.10          45              4743.18
        128.28          46              3848.58
        98.46           47              2953.98
 5      68.64           48              2059.38
        38.82           49              1164.78
        9.00            50              270.18
        -20.82          51              -624.42
undefined

如果您需要更多信息,请与我们联系。

2 个答案:

答案 0 :(得分:0)

所以,有两个问题。

在第一行打印年份。如果你将yearcount++放在循环的开头,当你到达条件时,它的值为1,所以它不会打印。将yearcount作为循环的最后一行。

最后不打印负值。您在while条件下检查bal>=0是否为bal-(int-min) >= 0,但在此之后您缩短29.82点,然后打印。在最后一次迭代中,bal等于9,然后减去29.82,它变为-20.82然后你打印它。

要解决此问题,您应该检查while (bal+(intr-min) >= 0) { paynum++; bal = (bal+(intr-min)); intrp = intr+(bal*min); 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); }

我认为这应该解决这两个问题:

Timeout::Error

答案 1 :(得分:0)

固定。 试试这个:

function displayWelcome() {
return "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.";

}
function calculateMinimumPayment(bal, intr) {
var min = bal * intr;
return min;
}
function displayPayments(bal, intr, min) {
debugger;
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\n______________\nYear\tBalance\t\tPayment Num\tInterest Paid\n";
console.log(top1);
console.log(top2);
var yearcount = 0;
var year = 1;
var paynum = 0;
while (bal >= 0) {
    paynum++;
    yearcount++;
    bal = (bal+(intr-min));
    if(bal <= 0){
    break;
    }
    intrp = intr+(bal*min);
    var tbl1 = parseFloat(bal).toFixed(2) + "\t\t" + paynum + "\t\t" + parseFloat(intrp).toFixed(2);
    var tbl2 = " " + year + "      " + tbl1;
    if (yearcount % 12 === 0 || year == 1) {
        var tbl2 = " " + year + "      " + tbl1;
        year++;
    } else {
        tbl2 = "\t" + tbl1;
    }
    console.log(tbl2);
}
}
debugger;
console.log(displayWelcome());
displayPayments(1500,0.18,calculateMinimumPayment(1500, 0.02));