Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>JavaScript Loan Calculator</title>
- <style>
- /* This is a CSS style sheet: it adds style to the program output */
- body {
- font-family:Helvetica, Arial, sans-serif;
- }
- .result {
- font-weight: bold;
- } /* For elements with class="result" */
- #payment {
- text-decoration: underline;
- } /* For element with id="payment" */
- table {
- padding: 1em;
- line-height: 2em;
- }
- tr:hover{
- background-color:#E1E1E1;
- }
- .top {
- background-color:#CAFFC7;
- }
- .bottom {
- background-color:#FFC;
- }
- .title {
- text-align:center;
- background-color:#EDEDED;
- margin:1em 0;
- padding:1em 0;
- font-size:1.4em;
- }
- .compute-button {
- color: #900;
- padding: 0.2em 0.5em;
- margin: 0.5em 0;
- }
- </style>
- </head>
- <body>
- <!--
- This is an HTML form that allows the user to enter data and allows
- JavaScript to display the results it computes back to the user. The
- form elements are embedded in a table to improve their appearance.
- The form itself is given the name "loandata", and the fields within
- the form are given names such as "interest" and "years". These
- field names are used in the JavaScript code that follows the form.
- Note that some of the form elements define "onchange" or "onclick"
- event handlers. These specify strings of JavaScript code to be
- executed when the user enters data or clicks on a button.
- -->
- <form name="loandata">
- <table>
- <tr class="top title"><td colspan="2">
- <b>Enter Loan Information:</b></td></tr>
- <tr class="top">
- <td>1) Amount of the loan (any currency):</td>
- <td><input type="text" name="principal" onChange="calculate();"></td>
- </tr>
- <tr class="top">
- <td>2) Annual percentage rate of interest:</td>
- <td><input type="text" name="interest" onChange="calculate();"></td>
- </tr>
- <!-- discount checkbox -->
- <tr class="top">
- <td>2a) Eligible for 10% discount on interest?</td>
- <td>
- <input type="checkbox" name="discount" onChange="calculate()">
- </td>
- </tr>
- <!-- external account checkbox -->
- <tr class="top" style="display:none">
- <td>2b) Monthly payments from external <br>account?
- (+0.25% interest)
- </td>
- <td>
- <input type="checkbox" name="externalAccount" onChange="calculate()">
- </td>
- </tr>
- <tr class="top">
- <td>3) Repayment period in years:</td>
- <td><input type="text" name="years" onChange="calculate();"></td>
- </tr>
- <tr><td></td>
- <td><input type="button" class="compute-button" value="Compute" onClick="calculate();"></td>
- </tr>
- <tr class="bottom title">
- <td colspan="2"><b>Payment Information:</b></td></tr>
- <tr class="bottom">
- <td>4) Your monthly payment:</td>
- <td>$<span class="result" id="payment"></span></td>
- </tr>
- <tr class="bottom">
- <td>5) Your total payment:</td>
- <td>$<span class="result" id="total"></span></td>
- </tr>
- <tr class="bottom">
- <td>6) Your total interest payments:</td>
- <td>$<span class="result" id="totalinterest"></span></td>
- </tr>
- </table>
- </form>
- <script language="JavaScript">
- /*
- * This is the JavaScript function that makes the example work. Note that
- * this script defines the calculate() function called by the event
- * handlers in the form. The function reads values from the form
- * <input> fields using the names defined in the HTML code above. It outputs
- * its results into the named <span> elements.
- */
- function calculate() {
- // Get the user's input from the form. Assume it is all valid.
- // Convert interest from a percentage to a decimal, and convert from
- // an annual rate to a monthly rate. Convert payment period in years
- // to the number of monthly payments.
- var principal = document.loandata.principal.value;
- var calculatedInterest = parseInt(document.loandata.interest.value);
- if (document.loandata.discount.checked) {
- calculatedInterest -= 10;
- if (calculatedInterest <= 0) {
- calculatedInterest = 0.001;
- }
- }
- if (document.loandata.externalAccount.checked) {
- calculatedInterest += 0.25;
- }
- var interest = calculatedInterest / 100 / 12;
- var payments = document.loandata.years.value * 12;
- // Now compute the monthly payment figure, using esoteric math.
- var x = Math.pow(1 + interest, payments);
- var monthly = (principal*x*interest)/(x-1);
- // Get named <span> elements from the form.
- var payment = document.getElementById("payment");
- var total = document.getElementById("total");
- var totalinterest = document.getElementById("totalinterest");
- // Check that the result is a finite number. If so, display the
- // results by setting the HTML content of each <span> element.
- if (isFinite(monthly)) {
- payment.innerHTML = monthly.toFixed(2) + " x " + payments +" monthly payments.";
- today = new Date();
- yearDue = parseInt(today.getFullYear());
- yearDue += parseInt(document.loandata.years.value);
- total.innerHTML = (monthly * payments).toFixed(2) + "<br/> due by "+ today.getDay() + "/" + today.getMonth() + "/" + yearDue;
- totalinterest.innerHTML = ((monthly*payments)-principal).toFixed(2) + " at " +calculatedInterest+"%";
- }
- // Otherwise, the user's input was probably invalid, so display nothing.
- else {
- payment.innerHTML = "";
- total.innerHTML = ""
- totalinterest.innerHTML = "";
- }
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement