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

问题描述 投票:-4回答:1

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

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

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

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;
}
form{
text-align: center;
border: 2px black solid;
}
<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>
javascript html calculator
1个回答
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中你实际上并不需要一个表单来“提交”任何地方的数据,因此你可以放弃甚至阻止表单的默认行为......不使用表单
  2. 您的评论惯例:
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;
}

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

“我怎样才能在结果结束时显示频率”

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;
}
form {
  text-align: center;
  border: 2px black solid;
}
<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>
© www.soinside.com 2019 - 2024. All rights reserved.