SHARE
TWEET

Example 1-3 from JavaScript: The Definitive Guide, Fifth Ed.

redandwhite Mar 2nd, 2012 33 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3. <title>JavaScript Loan Calculator</title>
  4. <style>
  5. /* This is a CSS style sheet: it adds style to the program output */
  6. .result { font-weight: bold; }  /* For elements with class="result" */
  7. #payment { text-decoration: underline; } /* For element with id="payment" */
  8. </style>
  9. </head>
  10. <body>
  11. <!--
  12.  This is an HTML form that allows the user to enter data and allows
  13.  JavaScript to display the results it computes back to the user. The
  14.  form elements are embedded in a table to improve their appearance.
  15.  The form itself is given the name "loandata", and the fields within
  16.  the form are given names such as "interest" and "years". These
  17.  field names are used in the JavaScript code that follows the form.
  18.  Note that some of the form elements define "onchange" or "onclick"
  19.  event handlers. These specify strings of JavaScript code to be
  20.  executed when the user enters data or clicks on a button.
  21. -->
  22. <form name="loandata">
  23.   <table>
  24.     <tr><td><b>Enter Loan Information:</b></td></tr>
  25.     <tr>
  26.       <td>1) Amount of the loan (any currency):</td>
  27.       <td><input type="text" name="principal" onchange="calculate();"></td>
  28.     </tr>
  29.     <tr>
  30.       <td>2) Annual percentage rate of interest:</td>
  31.       <td><input type="text" name="interest" onchange="calculate();"></td>
  32.     </tr>
  33.     <tr>
  34.       <td>3) Repayment period in years:</td>
  35.       <td><input type="text" name="years" onchange="calculate();"></td>
  36.     </tr>
  37.     <tr><td></td>
  38.       <td><input type="button" value="Compute" onclick="calculate();"></td>
  39.     </tr>
  40.     <tr><td><b>Payment Information:</b></td></tr>
  41.     <tr>
  42.       <td>4) Your monthly payment:</td>
  43.       <td>$<span class="result" id="payment"></span></td>
  44.     </tr>
  45.     <tr>
  46.       <td>5) Your total payment:</td>
  47.       <td>$<span class="result" id="total"></span></td>
  48.     </tr>
  49.     <tr>
  50.       <td>6) Your total interest payments:</td>
  51.       <td>$<span class="result" id="totalinterest"></span></td>
  52.     </tr>
  53.   </table>
  54. </form>
  55.  
  56. <script language="JavaScript">
  57. /*
  58.  * This is the JavaScript function that makes the example work. Note that
  59.  * this script defines the calculate() function called by the event
  60.  * handlers in the form. The function reads values from the form
  61.  * <input> fields using the names defined in the HTML code above.  It outputs
  62.  * its results into the named <span> elements.
  63.  */
  64. function calculate() {
  65.     // Get the user's input from the form. Assume it is all valid.
  66.     // Convert interest from a percentage to a decimal, and convert from
  67.     // an annual rate to a monthly rate. Convert payment period in years
  68.     // to the number of monthly payments.
  69.     var principal = document.loandata.principal.value;
  70.     var interest = document.loandata.interest.value / 100 / 12;
  71.     var payments = document.loandata.years.value * 12;
  72.  
  73.     // Now compute the monthly payment figure, using esoteric math.
  74.     var x = Math.pow(1 + interest, payments);
  75.     var monthly = (principal*x*interest)/(x-1);
  76.  
  77.     // Get named <span> elements from the form.
  78.     var payment = document.getElementById("payment");
  79.     var total = document.getElementById("total");
  80.     var totalinterest = document.getElementById("totalinterest");
  81.  
  82.     // Check that the result is a finite number. If so, display the
  83.     // results by setting the HTML content of each <span> element.
  84.     if (isFinite(monthly)) {
  85.         payment.innerHTML = monthly.toFixed(2);
  86.         total.innerHTML = (monthly * payments).toFixed(2);
  87.         totalinterest.innerHTML = ((monthly*payments)-principal).toFixed(2);
  88.     }
  89.     // Otherwise, the user's input was probably invalid, so display nothing.
  90.     else {
  91.         payment.innerHTML = "";
  92.         total.innerHTML = ""
  93.         totalinterest.innerHTML = "";
  94.     }
  95. }
  96. </script>
  97. </body>
  98. </html>
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top