2016-04-23 83 views
0

我遇到了這個程序的麻煩。我似乎無法弄清楚如何獲得今年的第一個數字(注意第一年不見了)。我也格式化我的while循環結束於零,但我仍然得到-20.82和undefined在表底部。最後,我已經看遍了所有,但我找不到正確的公式來計算,我覺得我已經幾乎得到它,但我錯過了一個重要的一塊。請幫忙!表格格式Javascript

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..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 

讓我知道如果你需要更多的信息。

+0

你可以減少你的代碼到不起作用?或者只顯示最小的代碼? – evolutionxbox

回答

0

所以,有兩個問題。

打印第一行。如果您將yearcount++置於循環的開始處,則當您達到條件時,它的值爲1,因此不會打印。將yearcount作爲循環的最後一行。

不在末尾打印負值。您在while條件中檢查bal>=0,但在此之後,您將減去29.82分,然後打印。在最後一次迭代中,bal等於9,然後減去29.82,然後變成-20.82,然後打印出來。

要解決這個問題,你應該檢查是否bal-(int-min) >= 0

我想這應該解決這兩個問題:

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

這解決了年度問題,但我仍然在表格的末尾得到一個未定義的問題。 – dungo

+0

這很奇怪,當我運行它時,我沒有得到任何'undefined'。 –

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)); 
+0

「年度中的1」專欄仍然缺失! – dungo

+0

刪除調試器) –