title: Javascript mortgage calculator - payment frequency calculation
body: <p>I'm creating a mortgage loan calculator that includes a calculation that breaks the calculation into payment frequencies (weekly, fortnightly, monthly, quarterly and yearly) and am now stuck.</p>
  
  <p>I've tried a number of different ways but nothing seems to work for me.</p>
  
  <p>My script is below. Anyone have any suggestions on how I can get this to work?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>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 &gt; 200000 &amp;&amp; amount &lt; 250000){     //If loan amount is between $200,000 and $250,000, the interest rate will be 5.09%
      interest = 5.09;
  }
  if(amount &gt; 250000 &amp;&amp; amount &lt; 500000){     //If loan amount is between $250,000 and $500,000, the interest rate will be 4.84%
      interest = 4.84;
  }
  if(amount &gt; 500000 &amp;&amp; amount &lt; 750000){     //If loan amount is between $500,000 and $750,000, the interest rate will be 4.79%
      interest = 4.79;
  }
  if (amount &gt; 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;
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>form{
  text-align: center;
  border: 2px black solid;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form onsubmit="computeLoan();"&gt;
  
  &lt;legend&gt;Mortgage Calculator&lt;/legend&gt;
  &lt;p&gt;&lt;b&gt;Number of Years&lt;/b&gt;: &lt;input type="text" id="years" value="30" 
  required&gt;&lt;/p&gt;
  
  &lt;p&gt;&lt;b&gt;Loan Amount&lt;/b&gt;: &lt;input type="text" id="amount" value="200000" 
  required&gt;&lt;/p&gt;
  
  &lt;p&gt;&lt;b&gt;Payment Frequency :&lt;/b&gt;
    &lt;select id='paymentTerm'&gt;
      &lt;option value="week"&gt;Weekly&lt;/option&gt;
      &lt;option value="fortnight"&gt;Fortnightly&lt;/option&gt;
      &lt;option value="month"&gt;Monthly&lt;/option&gt;
      &lt;option value="quarter"&gt;Quarterly&lt;/option&gt;
      &lt;option value="year"&gt;Yearly&lt;/option&gt;
    &lt;/select&gt;
  &lt;/p&gt;
  
  &lt;input type="submit"&gt;
  
  &lt;p&gt;&lt;b&gt;The repayment amount is &lt;span id="outMonthly"&gt;&lt;/span&gt; each &lt;span 
  id="paymentTermOut"&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
  
  &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>