javascript loop payment code example

Example: javascript loop payment

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

Tags:

Misc Example