找回密码
 注册账号

QQ登录

只需一步,快速开始

《泰拉瑞亚下载-1.4.2.3版》 泰拉瑞亚服务器 - MOD模组下载

入驻泰拉战网 新手指引 - 升级 - 师徒

泰拉瑞亚合成表 泰拉卡牌 - 泰拉江湖 - 泰拉刺客

联系泰拉开发组👈进入 积分市场 - 房产交易 - 水晶获取

查看: 5092|回复: 1

[JS] 一个JavaScript贷款计算器

[复制链接]

378

主题

-130

回帖

167

广播

已有小成

积分
425
泰拉
0
水晶
0
铜钥匙
0
银钥匙
0
金钥匙
0

【江湖新秀】【我是小土豪】【宝剑回鞘】【泰拉达人】【奥运选手】

发表于 2018-1-3 02:41:49 | 显示全部楼层 |阅读模式
练习案例 时间2018/01/02
一个JavaScript贷款计算器
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>HelloJs4</title>
  6. <style>
  7. /*这是一个CSS样式表:定义了输出程序的样式*/


  8. .output {
  9.         font-weight: bold;
  10. }  /*定义计算结果为粗体*/
  11. #payment {
  12.         text-decoration: underline
  13. } /*定义 id = payment的元素样式*/
  14. #graph {
  15.         border: solid #000 1px;
  16. } /*图标有一个1像素的边框*/
  17. th, td {
  18.         vertical-algin: top
  19. }/*表格单元格对齐方式为顶端对其*/
  20. </style>
  21. </head>
  22. <body>
  23. <div>
  24.   <div>
  25.     <p>练习案例 时间2018/01/01</p>
  26.   </div>
  27.   <div>
  28.     <p>一个JavaScript贷款计算器</p>
  29.   </div>
  30.   <div>
  31.    
  32.     <table>
  33.       <tr>
  34.         <th>Enter Loan Data:</th>
  35.         <td></td>
  36.         <th>Loan Balance,Cumulative Equity,and Interest Payments</th>
  37.       </tr>
  38.       <tr>
  39.               <td>Aoumnt of the loan($):</td>
  40.         <td><input id="amount" onChange="calcuate();"/></td>
  41.         <td rowspan="8">
  42.                 <canvas id="graph" width="400" height="250"></canvas>
  43.         </td>
  44.       </tr>
  45.       <tr>
  46.               <td>Annual interest(%):</td>
  47.         <td><input id="apr" onChange="calcuate();"/></td>
  48.       </tr>
  49.       <tr>
  50.               <td>Repayment period(years):</td>
  51.         <td><input id="years" onChange="calcuate();"/></td>
  52.       </tr>
  53.       <tr>
  54.               <td>Zipcode (to find lenders):</td>
  55.         <td><input id="zipcode" onChange="calcuate();"/></td>
  56.       </tr>
  57.       <tr>
  58.               <td>Approximate Payments:</td>
  59.         <td><button onClick="calcuate()">Calcuate</button></td>
  60.       </tr>
  61.       <tr>
  62.               <td>Monthly payment:</td>
  63.         <td>lt;span class="output" id="payment"></span></td>
  64.       </tr>
  65.       <tr>
  66.               <td>Total payment:</td>
  67.         <td>lt;span class="output" id="total"></span></td>
  68.       </tr>
  69.       <tr>
  70.               <td>Total interest:</td>
  71.         <td>lt;span class="output" id="totalinterest"></span></td>
  72.       </tr>
  73.       <tr>
  74.               <th>Sponsors:</th>
  75.         <td colspan="2">Apply for you with one of these fine lenders<div id="lenders"></div></td>
  76.       </tr>
  77.     </table>
  78.   </div>
  79. </div>
  80. </body>
  81. <script>
  82. "use strict"
  83. //----[计算金额]----
  84. function calcuate(){
  85.         var amount = document.getElementById("amount");
  86.         var apr = document.getElementById("apr");
  87.         var years = document.getElementById("years");
  88.         var zipcode = document.getElementById("zipcode");       
  89.         var payment = document.getElementById("payment");       
  90.         var total = document.getElementById("total");       
  91.         var totalinterest = document.getElementById("totalinterest");
  92.        
  93.         var principal = parseFloat(amount.value);
  94.         var interest = parseFloat(apr.value) / 100 / 12;
  95.         var payments = parseFloat(years.value) * 12;
  96.        
  97.         var x = Math.pow(1+interest,payments);
  98.         var monthly = (principal * x * interest) / (x - 1);
  99.        
  100.         if(isFinite(monthly)){
  101.                 payment.innerHTML = monthly.toFixed(2);
  102.                 total.innerHTML = (monthly * payments).toFixed(2);
  103.                 totalinterest.innerHTML = ((monthly * payments) - principal).toFixed(2);
  104.                 save(amount.value,apr.value,years.value,zipcode.value);
  105.                 chart(principal,interest,monthly,payments);
  106.         }else{
  107.                 payment.innerHTML = "";
  108.                 total.innerHTML = "";
  109.                 totalinterest.innerHTML = "";
  110.                 chart();
  111.         }
  112. };
  113. //----[计算金额]----

  114. //----[保存操作]----
  115. function save (amount,apr,years,zipcode) {
  116.         if(window.localStorage){
  117.                 localStorage.loan_amount = amount;
  118.                 localStorage.loan_apr = apr;
  119.                 localStorage.loan_years = years;
  120.                 localStorage.loan_zipcode = zipcode;
  121.         }
  122. };
  123. window.onload = function () {
  124.         if(window.localStorage && localStorage.loan_amount){
  125.                 document.getElementById("amount").value = localStorage.loan_amount;
  126.                 document.getElementById("apr").value = localStorage.loan_apr;
  127.                 document.getElementById("years").value = localStorage.loan_years;
  128.                 document.getElementById("zipcode").value = localStorage.loan_zipcode;
  129.         }
  130. };
  131. //----[保存操作]----
  132. //----[显示图标操作]----
  133. function chart(principal,interest,monthly,payments) {
  134.         var graph = document.getElementById("graph");
  135.         graph.width = graph.width;
  136.        
  137.         if(arguments.length == 0 || !graph.getContext) return;
  138.        
  139.         var g = graph.getContext("2d");
  140.         var width = graph.width,height = graph.height;
  141.        
  142.         function paymentToX(n){return ((n * width) / payments);}
  143.         function amountToY (a){return (height - (a * height)/((monthly * payments * 1.05)));}
  144.        
  145.         g.moveTo(paymentToX(0),amountToY(0));
  146.         g.lineTo(paymentToX(payments),amountToY(monthly * payments));
  147.         g.lineTo(paymentToX(payments),amountToY(0));
  148.         g.closePath();
  149.         g.fillStyle = "#f88";
  150.         g.fill();
  151.         g.font = " bold 12px sans-serif";
  152.         g.fillText("Total Interest Payments",20,20);
  153.        
  154.         var equity = 0;
  155.         g.beginPath();
  156.         g.moveTo(paymentToX(0),amountToY(0));
  157.         for (var p = 1;p <= payments; p++) {
  158.                 var thisMonthsInterest = (principal - equity) * interest;
  159.                 equity += (monthly - thisMonthsInterest);
  160.                 g.lineTo(paymentToX(p),amountToY(equity));
  161.         }
  162.         g.lineTo(paymentToX(payments),amountToY(0));
  163.         g.closePath();
  164.         g.fillStyle = "green";
  165.         g.fill();
  166.         g.fillText("Total Equity",20,35);
  167.        
  168.         var bal = principal;
  169.         g.beginPath();
  170.         g.moveTo(paymentToX(0),amountToY(bal));

  171.         for (var p = 1;p <= payments; p++) {
  172.                 var thisMonthsInterest = bal * interest;
  173.                 bal -= (monthly - thisMonthsInterest);
  174.                 g.lineTo(paymentToX(p),amountToY(bal));
  175.         }
  176.         g.linewidth = 3;
  177.         g.stroke();
  178.         g.fillStyle = "black";
  179.         g.fill();
  180.         g.fillText("Loan Black",20,50);
  181.        
  182.         g.textAlign = "center";
  183.         var y = amountToY(0);
  184.         for (var year = 1; (year*12) <= payments; year++) {
  185.                 var x = paymentToX(year*12);
  186.                 g.fillRect(x - 0.5, y - 3, 1, 3);
  187.                 if (year == 1) {g.fillText("Year", x, y - 5);}
  188.                 if(year % 5 == 0 && year * 12 !== payments) {g.fillText(String(year) , x , y - 5);}
  189.         }
  190.        
  191.         g.textAlign = "right";
  192.         g.textBaseline = "middle";
  193.         var ticks = [monthly * payments,principal];
  194.         var rightEdge = paymentToX(payments);
  195.         for (var i = 0;i < ticks.length ; i++) {
  196.                 var y = amountToY(ticks[i]);
  197.                 g.fillRect(rightEdge - 3,y - 0.5, 3 , 1);
  198.                 g.fillText(String(ticks[i].toFixed(0)) , rightEdge - 5 , y);
  199.         }
  200. }
  201. </script>
  202. </html>
复制代码



您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

QQ|友链申请|Archiver|手机版|小黑屋|游芯沙盒 ( 陕ICP备11006283号-1 )

GMT+8, 2024-11-23 15:55 , Processed in 0.106408 second(s), 40 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表