【问题标题】:Javascript mortgage calculator - payment frequency calculationJavascript抵押贷款计算器 - 付款频率计算
【发布时间】:2017-08-07 03:40:43
【问题描述】:

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

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

我的脚本在下面。有人对我如何使它起作用有任何建议吗?

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 新手,我不确定自己哪里出错了。

标签: javascript html calculator


【解决方案1】:

我已经稍微整理了您的代码。它似乎工作,除了你可能错过的一些小东西(我相信这是你的代码不起作用的原因)。

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

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

两件事:

    1234563 . 不使用表格
  1. 您的评论约定:

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>

【讨论】:

  • 谢谢罗汉!我有一张预期计算表,这些计算似乎适用于每月的结果,但不适用于每周、每两周、每季度和每年。我的脚本中的计算是否错误?以及如何让频率显示在结果末尾(即还款金额为 $xxx 每个 XXX)我尝试更新:

    还款金额为 每个

    还款金额是每个

  • 预期结果示例:30 年的 300,000 美元贷款 - 每周金额:364.65 美元,每两周金额:729.46 美元,每月金额:1,581.26 美元,每季度金额:4,752.25 美元,每年金额:19,160.95 美元有什么想法吗?跨度>
  • @Sharpie13,你实际上非常接近......你为什么不也使用选择中的值并将其绑定到输出文本,(参见更新的示例)如果你需要一些东西对于每种情况更具体,您可能有一个带有 case 语句的函数,该语句根据提供的值返回自定义文本。 (或者您可以使用键值类型查找......但为了简单起见,也许看看 ho 在这个例子中使用一个案例)
  • @Sharpie13 查看我的更新答案。这就是你要找的东西吗?
  • 完美!太感谢了。关于我哪里出错的任何提示?
猜你喜欢
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 2020-12-13
相关资源
最近更新 更多