2017-04-07 40 views
1

拋出不需要的差距代碼: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  Minimum Payment") 

     var year = 1; 
     var payments = 1; 
     var interestPaid = 0; 
     var yearChange; 

     while (balance > 0) { 
      yearChange = false; 

      if (payments % 12 == 0) { 
       year++ 
       yearChange = true; 
      } 
      interestPaid += balance * interest/12; 
      balance = Math.max(0, balance - (minimumPayment - balance * interest/12)); 
      minimumPayment = Math.max(20, balance * minimumPaymentRate); 
      console.log(yearChange? year: "" + "  " + balance.toFixed(2) + "  " + payments + "    " + interestPaid.toFixed(2) + "    " + minimumPayment.toFixed(2)); 
      payments++; 
     } 
    } 

    var balance = 1500; 
    var minimumPaymentRate = 0.02; 
    var interest = 0.18; 

    displayWelcome() 
    var minimumPayment = calculateminimumPaymentment(balance, minimumPaymentRate); 

    displayPayments(balance, interest, minimumPayment); 

輸出

Year Balance  Payment #  Interest Paid  Minimum Payment 
     1492.50  1    22.50    29.85 
     1485.04  2    44.89    29.70 
     1477.61  3    67.16    29.55 
     1470.22  4    89.33    29.40 
     1462.87  5    111.38    29.26 
     1455.56  6    133.32    29.11 
     1448.28  7    155.16    28.97 
     1441.04  8    176.88    28.82 
     1433.83  9    198.50    28.68 
     1426.67  10    220.00    28.53 
     1419.53  11    241.40    28.39 
2 
     1405.37  13    283.88    28.11 
     1398.35  14    304.96    27.97 
     1391.35  15    325.94    27.83 
     1384.40  16    346.81    27.69 
     1377.47  17    367.58    27.55 
     1370.59  18    388.24    27.41 
     1363.73  19    408.80    27.27 
     1356.92  20    429.25    27.14 
     1350.13  21    449.61    27.00 
     1343.38  22    469.86    26.87 
     1336.66  23    490.01    26.73 
3 

我不明白如何讓這個隨着年數,這也說明相應的數據,因爲當我現在運行它時,它會跳過其餘部分,並在下一行再次顯示它。還想知道如何在第一行代表第一年有1。期望的輸出如下所示。

Desired Output

感謝您的時間。

+0

難道你不希望顯示一個HTML表格,而不是輸出? – funcoding

+0

@funcoding不,只要在控制檯根據要求。 – AvenNova

回答

3

兩件事情改變:

  1. 初始化支付爲0而不是1:

    var payments = 0; 
    
  2. 圍繞三元運算符使用括號,因爲它不具有優先於+它後面:

    console.log((yearChange? year: "") + "  " + balance.toFixed(2) + 
        // ...etc. 
    

爲月數,然後從零開始,你可能需要添加一個到它的顯示器,像這樣:

 "  " + (payments+1) + 

,或者,只是console.log前移到下面增量:

 payments++; 
+1

此外,一種方法,使間距更加一致的是使用一個標籤'\ t'而不是空格。 – imtheman

+0

此外,如何讓這個第一分鐘付款顯示爲$ 30.00,而不是$ 29.85,增幅?我嘗試將.15添加到console.log,但它不起作用。 – AvenNova

+1

@imtheman你是男人! \讓它看起來更清潔! – AvenNova