2014-04-13 122 views
2

該計劃應該確定償還貸款需要多少個月。似乎無法弄清楚如何解決我的數學計算。不知道它是不是while循環。根據用戶輸入確定每月支付,而不是根據用戶輸入確定每月支付,而是顯示總月數(我不想要的,程序應該這樣做)。它應該是這樣的:http://snag.gy/9vzGi.jpg以下是代碼:帶循環的Javascript貸款計算器

<html> 
<head> 
<title></title> 

<script type="text/javascript"> 

function fixVal(value,numberOfCharacters,numberOfDecimals,padCharacter) { 
    var i, stringObject, stringLength, numberToPad;    

    value = value * Math.pow(10,numberOfDecimals);     
    value = Math.round(value);         
    stringObject = new String(value);        
    stringLength = stringObject.length;       
    while(stringLength < numberOfDecimals) {      
     stringObject = "0"+stringObject;      
     stringLength=stringLength+1;      
    } 

    if(numberOfDecimals>0) {      
     stringObject=stringObject.substring(0,stringLength-numberOfDecimals)+"."+ 
     stringObject.substring(stringLength-numberOfDecimals,stringLength); 
    } 

    if (stringObject.length<numberOfCharacters && numberOfCharacters>0) { 
     numberToPad=numberOfCharacters-stringObject.length;  
     for (i=0; i<numberToPad; i=i+1) { 
      stringObject=padCharacter+stringObject; 
     } 
    } 

    return stringObject;          
} 

function buildTable() { 

    var amount=parseFloat(document.getElementById("loanAmt").value); 
    var numpay=parseInt(document.getElementById("monthlyPay").value); 
    var rate=parseFloat(document.getElementById("intRte").value); 

    rate = rate/100; 
    var monthly = rate/12; 
    var payment = ((amount * monthly)/(1-Math.pow((1 + monthly), - numpay))); 
    var total = payment * numpay; 
    var interest = total - amount; 

    var msg = "<table border='4' width='75%'>"; 
    msg += "<tr>"; 
    msg += "<td>Month</td>"; 
    msg += "<td>Principal Paid</td>"; 
    msg += "<td>Interest Paid</td>"; 
    msg += "<td>Loan Balance</td>"; 
    msg += "</tr>"; 

    newPrincipal=amount; 
    var i = 1; 
    while (i <= numpay) { 
     newInterest=monthly*newPrincipal; 
     reduction=payment-newInterest; 
     newPrincipal=newPrincipal-reduction; 

     msg += "<tr><td align='left' bgcolor='pink'>"+i+"</td> \ 
       <td align='left' bgcolor='pink'>"+fixVal(reduction,0,2,' ')+"</td> \ 
       <td align='left' bgcolor='pink'>"+fixVal(newInterest,0,2,' ')+"</td> \ 
       <td align='left' bgcolor='pink'>"+fixVal(newPrincipal,0,2,' ')+"</td></tr>"; 

     i++; 
    } 


    msg += "</table>"; 

    document.getElementById("results").innerHTML = msg; 


} 


</script> 

<style type="text/css"> 

body { 
    background: black; 
    font-family: arial; 
} 

#contentwrap { 
    width: 700px; 
    margin: 40px auto 0px auto; 
    background: #FFFFCC; 
    text-align: center; 
    border: 6px red solid; 
    border-radius: 10px; 
    padding: 40px; 
} 

table { 
    border: 5px blue double; 
    background-color: #FFFFCC; 
} 

#header { 
    text-align: center; 
    font-size: 2.5em; 
    text-shadow: yellow 3px 3px; 
    margin-bottom: 18px; 
    color: red; 
} 

#button { 
    background: blue; 
    color: white; 
    cursor: pointer; 
    padding: 5px 0px 5px 0px; 
    border: 1px solid red; 
    border-radius: 25px; 
    width: 150px; 
} 

.contentTitles { 
    color: green; 
    font-weight: bold; 
} 

.style { 
    background: lightblue; 
    font-family: comic sans ms; 
    border: 6px blue double; 
    color: green; 
    font-weight: bold; 
} 

</style> 

</head> 

<body> 

<div id="contentwrap"> 

<div id="header">Javascript Loan Calculator</div> 

<form> 

<div class="contentTitles">Enter Loan Amount<br /> 
<input type="text" id="loanAmt" class="style"><p /> 

Interest Rate (%)<br /> 
<input type="text" id="intRte" class="style"><p /> 

Monthly Payment Amount<br /> 
<input type="text" id="monthlyPay" class="style"><p /> 

<div style="margin-top:20px;"> 
<input type="button" value="Process Data" id="button" onClick="buildTable()"> 
</div> 

</form> 

<center> 
<div id="results" style="margin-top:20px;"></div> 
</center> 


</div> <!-- ends div#contentwrap --> 

</body> 
</html> 

回答

1

如果你想輸入的是按月支付的,請不要撥打相應的變量numpay

在你的情況下,似乎更實際的是不預先計算月數。您可以使用while循環來建立表格並同時計算貸款的持續時間:

function buildTable() { 

    var amount = parseFloat(document.getElementById("loanAmt").value); 
    var monthly = parseInt(document.getElementById("monthlyPay").value); 
    var rate = parseFloat(document.getElementById("intRte").value); 

    rate = rate/100/12; 

    var m = 0; // number of months 

    while (amount > 0) { 
     var interest = amount * rate; 
     var principal = monthly - interest; 

     if (principal > amount) { 
      principal = amount; 
      amount = 0.0; 
     } else { 
      amount -= principal; 
     } 

     // build table: m + 1, principal, interest, amount 

     m++; 
    } 

    // display table 
} 
+0

工作完美,變量更有意義。謝謝。 – user3389685

+0

任何想法爲什麼當數字小於1.00時小數點之前不會輸出「0」?例如,顯示「.99」而不是「0.99」 – user3389685

+1

在插入小數點之前,您需要將您的數字零點填充到比插入小數點之前的小數點多一位,因此,例如,72將變爲「 fixVal(0.72,0,2,'')'。在'fixVal'的第一個'while'循環中將'while(stringLength