Javascript抵押计算器 - 支付频率计算

时间:2017-08-07 03:40:44

标签: javascript html calculator

我正在创建一个抵押贷款计算器,其中包含一个将计算分解为付款频率的计算(每周,每两周,每月,每季度和每年),现在我被卡住了。

我尝试了许多不同的方法,但似乎没有什么对我有用。

我的脚本如下。任何人都有关于如何让它工作的任何建议?



function computeLoan() {
//Prevent the Default Action eg, form to post/refresh the page
event.preventDefault();

var amount = parseInt(document.getElementById("amount").value);
var interest = calculateInterest(amount);
var term = parseInt(document.getElementById("years").value);
var frequency = document.getElementById("paymentTerm").value;

var finalAmmount = calculateMortgage(amount, interest, term, frequency);

document.getElementById("outMonthly").innerText = "$" + finalAmmount;
}


function calculateMortgage(p, r, n, f) {

r = percentToDecimal(r); //convert percentage to a decimal
n = yearsToMonths(n,f); //convert years to months
var pmt = (r * p) / (1 - (Math.pow((1 + r), (-n)))); //c=
((p*r)*Math.pow((1+r),n))/(Math.pow(1+r),n)-1
return parseFloat(pmt.toFixed(2));
}


function percentToDecimal(percent) {        //Change the percent entered to 
a decimal
return (percent / 12) / 100;
}


function yearsToMonths(year,frequency) {
//return year * 12;

if(frequency == "week"){
    return year * 52;
}
if(frequency == "fortnight"){
    return year * 26;
}
if(frequency == "quarter"){
    return year * 4;
}
return year * 12;
}


function calculateInterest(amount){
var interest = 5.4;

if(amount > 200000 && amount < 250000){     //If loan amount is between $200,000 and $250,000, the interest rate will be 5.09%
    interest = 5.09;
}
if(amount > 250000 && amount < 500000){     //If loan amount is between $250,000 and $500,000, the interest rate will be 4.84%
    interest = 4.84;
}
if(amount > 500000 && amount < 750000){     //If loan amount is between $500,000 and $750,000, the interest rate will be 4.79%
    interest = 4.79;
}
if (amount > 750000){       //If loan amount is greater than $750,000, the interest rate will be 4.50%
    interest = 4.50;
}
return interest;
}


function postPayments(payment) {
var htmlEl = document.getElementById("outMonthly");
htmlEl.innerText = "$" + payment;

// document.getElementById("outMonthly").innerText = payment;

return;
}
&#13;
form{
text-align: center;
border: 2px black solid;
}
&#13;
<form onsubmit="computeLoan();">

<legend>Mortgage Calculator</legend>
<p><b>Number of Years</b>: <input type="text" id="years" value="30" 
required></p>

<p><b>Loan Amount</b>: <input type="text" id="amount" value="200000" 
required></p>

<p><b>Payment Frequency :</b>
  <select id='paymentTerm'>
    <option value="week">Weekly</option>
    <option value="fortnight">Fortnightly</option>
    <option value="month">Monthly</option>
    <option value="quarter">Quarterly</option>
    <option value="year">Yearly</option>
  </select>
</p>

<input type="submit">

<p><b>The repayment amount is <span id="outMonthly"></span> each <span 
id="paymentTermOut"></span></b></p>

</form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我稍微整理了你的代码。它似乎有用,除了你可能错过的小东西(我相信你的代码没有工作的原因)。

错误消息还会为您提供有关损坏的信息:

Error: {
  "message": "Uncaught SyntaxError: Unexpected identifier", // < whats wrong
  "filename": "https://stacksnippets.net/js",  // << what file
  "lineno": 66, // << where to find it
  "colno": 3
}

2件事:

  1. 如果您在本地为客户做所有事情,并且在JS中,您实际上并不需要表格来提交&#34;提交&#34;任何地方的数据,因此你可以放弃甚至阻止表格的默认行为......不使用表格

  2. 您的评论惯例:

  3. function foo() {   // commenting here...
    ...can overflow here which will break if you're not paying attention
      return 'I wont run because of the invalid "js" above';
    }
    

    后者可能容易出错,导致您的实际脚本无法正常工作(如错误消息中所述)。

    function percentToDecimal(percent) {   //Change the percent entered to   
    a decimal //  <<< this isnt valid js
    return (percent / 12) / 100;
    }
    

    下面的工作代码(根据您的评论更新):

      

    &#34;我怎样才能在结果结束时显示频率&#34;

    &#13;
    &#13;
    function computeLoan() {
    
      var amount = parseInt(document.getElementById("amount").value);
      var interest = calculateInterest(amount);
      var term = parseInt(document.getElementById("years").value);
      var frequency = document.getElementById("paymentTerm").value;
    
      var finalAmmount = calculateMortgage(amount, interest, term, frequency);
    
      document.getElementById("outMonthly").innerText = "$" + finalAmmount;
      document.getElementById("paymentTermOut").innerText = frequency;
      document.getElementById("customText").innerText = getCustomText(frequency, finalAmmount);
    };
    
    function getCustomText(term, finalAmmount) {
    
      switch (term) {
        case "week":
          return "Each week you pay $" + finalAmmount;
        case "fortnight":
          return "Forthnightly repayment will be $" + finalAmmount;
        default:
          return "Repay $" + finalAmmount + " per " + term;
      }
    
    }
    
    
    function calculateMortgage(p, r, n, f) {
    
      r = percentToDecimal(r); //convert percentage to a decimal
      n = yearsToMonths(n, f); //convert years to months
      var pmt = (r * p) / (1 - (Math.pow((1 + r), (-n)))); //c=
      ((p * r) * Math.pow((1 + r), n)) / (Math.pow(1 + r), n) - 1
      return parseFloat(pmt.toFixed(2));
    }
    
    //Change the percent entered to a decimal
    function percentToDecimal(percent) {
      return (percent / 12) / 100;
    }
    
    
    function yearsToMonths(year, frequency) {
      //return year * 12;
    
      if (frequency == "week") {
        return year * 52;
      }
      if (frequency == "fortnight") {
        return year * 26;
      }
      if (frequency == "quarter") {
        return year * 4;
      }
      return year * 12;
    }
    
    
    function calculateInterest(amount) {
      var interest = 5.4;
    
      //If loan amount is between $200,000 and $250,000, the interest rate will be 5.09%
      if (amount > 200000 && amount < 250000) {
        interest = 5.09;
      }
    
      //If loan amount is between $250,000 and $500,000, the interest rate will be 4.84%
      if (amount > 250000 && amount < 500000) {
        interest = 4.84;
      }
    
      //If loan amount is between $500,000 and $750,000, the interest rate will be 4.79%
      if (amount > 500000 && amount < 750000) {
        interest = 4.79;
      }
    
      //If loan amount is greater than $750,000, the interest rate will be 4.50%
      if (amount > 750000) {
        interest = 4.50;
      }
      return interest;
    }
    
    function postPayments(payment) {
      var htmlEl = document.getElementById("outMonthly");
      htmlEl.innerText = "$" + payment;
    
      // document.getElementById("outMonthly").innerText = payment;
    
      return;
    }
    &#13;
    form {
      text-align: center;
      border: 2px black solid;
    }
    &#13;
    <legend>Mortgage Calculator</legend>
    <p><b>Number of Years</b>: <input type="text" id="years" value="30" required></p>
    
    <p><b>Loan Amount</b>: <input type="text" id="amount" value="200000" required></p>
    
    <p><b>Payment Frequency :</b>
      <select id='paymentTerm'>
        <option value="week">Weekly</option>
        <option value="fortnight">Fortnightly</option>
        <option value="month">Monthly</option>
        <option value="quarter">Quarterly</option>
        <option value="year">Yearly</option>
      </select>
    </p>
    
    <button onclick="computeLoan()">Click Me</button>
    
    <p><b>The repayment amount is <span id="outMonthly"></span> each <span 
    id="paymentTermOut"></span></b></p>
    <p><b><span id="customText"></span></b></p>
    &#13;
    &#13;
    &#13;