Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

PREVIOUS NEXT
Code Example
Javascript :: pdf javascript search text 
Javascript :: program to parenthesize an expression 
Javascript :: tf js change weighs 
Javascript :: Maths help you save money 
Javascript :: jest-badges-readme 
Javascript :: laravel datables get next input jquery next 
Javascript :: filter advantages in js 
Javascript :: json decoding and optionals 
Javascript :: filter keys from object using ramda 
Javascript :: if you run a script.js with the code, how do you access the value passed to "var" inside script.js ... 
Javascript :: validator.addmethod username duplicates 
Javascript :: arrow function component react shortcut vscode 
Javascript :: how to make modules structure like lodash 
Javascript :: universal apollo kit 
Javascript :: change dxform label angular 
Javascript :: processing an express form with node-postgres 
Javascript :: svn node remains in conflict 
Javascript :: select coordinates of image 
Javascript :: what is the purpose of subscript in an array 
Javascript :: how to remove &quot in json in "flutter" 
Javascript :: vuejs filter array by dates 
Javascript :: simple javascript router 
Javascript :: what is amqp10 policy nodejs 
Javascript :: java code that writes code in powerpoint 
Javascript :: pupetter create incognitor browser 
Javascript :: es6 1 to one 
Javascript :: resequency array by atributo 
Javascript :: jquery post data into an iframe from textarea live 
Javascript :: activar funcion js con id 
Javascript :: why setjavascriptenabled will true 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =