[英]Adding Cumulative Values in loops in Javascript
我一直試圖在該程序的“已付利息”列中獲取數據,以將其與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的當前值。
我一直試圖在該程序的“ Interest Paid”列中獲取數據,以將其與while循環的每次交互一起添加,但它僅顯示了intrp的下一個值
那是因為您是將值分配給intrp
而不是累積先前的值。
更換
intrp = bal * (intr / 12);
與
intrp += bal * (intr / 12);
如果加上累計利息
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)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.