2017-04-06 109 views
0

在間隔顯示可變的代碼:麻煩與在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 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

你每次都在循環中做「年度++」,爲什麼它不應該在每一行都增加?更改代碼,以便當「payments」是「12」的倍數時,您只增加「year」。 – Barmar

+0

您希望的輸出顯示每年11次付款 - 這是正確的嗎?還是應該每年12次付款(每月1次)? –

+0

@JamesMonger,這是一個錯誤,意味着在13付款。 – AvenNova

回答

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

,而不是增加year變量 - 如果它連接到payments - 所以圖1-12是第一年,13-24 - 第二一年......你可以把它簡化爲:

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