I Need Help Changing Month to Year (Javascript)

I have this Javascript:

function computeLoan(){
	var amount = document.getElementById('amount').value;
	var interest_rate = document.getElementById('interest_rate').value;
	var months = document.getElementById('months').value;
	var interest = (amount * (interest_rate * .01)) / months;
	var payment = ((amount / months) + interest).toFixed(2);
	payment = payment.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
	document.getElementById('payment').innerHTML = "Monthly Payment = $"+payment;

I need help changing this so that the Payment is per year - not per month. Can you help me?

Welcome to the forums, @HBank :wave:

[off-topic]
When you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the block of code.

I have done it for you this time.
[/off-topic]

I think its a matter of changing all references from months to years. Seems to work out

Here is a code example where I’ve made the functions capable of accepting arguments for amount, interest rate, and months, returning only the payment amount.

function computeMonthlyLoan(amount, interestRate, months){
	var interest = (amount * (interestRate * 0.01)) / months;
	var payment = ((amount / months) + interest);
	return payment;
}

That is used by a showMonthlyPayment() function, that gets info from the form and updates the page.

function formattedCurrency(amount) {
  return amount.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function showMonthlyPayment(form) {
    const payment = computeMonthlyLoan(
      form.elements.amount.value,
      form.elements.interestRate.value,
      form.elements.months.value
    );
    const formattedPayment = formattedCurrency(payment);
    const result = "Monthly Payment = $" + formattedPayment;
    document.querySelector("#monthlypayment").innerHTML = result;
}

Similar things are done with the yearly amount, which as someone else has already said just wants renaming months to years.

function computeYearlyLoan(amount, interestRate, years){
	var interest = (amount * (interestRate * 0.01)) / years;
	var payment = ((amount / years) + interest);
	return payment;
}
function formattedCurrency(amount) {
  return amount.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function showYearlyPayment(form) {
    const payment = computeYearlyLoan(
      form.elements.amount.value,
      form.elements.interestRate.value,
      form.elements.years.value
    );
    const formattedPayment = formattedCurrency(payment);
    const result = "Yearly Payment = $" + formattedPayment;
    document.querySelector("#yearlypayment").innerHTML = result;
}

Example code is found at https://jsfiddle.net/pmw57/sg6axd3v/2/

1 Like

The following code calculates loan payments using the standard formula. It allows you to choose payment frequencies of monthly, yearly, quarterly and weekly. The output is the payment amount per period as well as the total amount paid and the total interest paid.
There is a working example at https://jsfiddle.net/AllanP/1nkv6gqp/

<body>

<form name="ff">
	<p>INPUT</p>
	<p>Amount: <input name="amount" type="text" value="1000"></p>
	<p>Interest: <input name="interest_rate_pc" type="text" value="5"></p>
	<p>Term (yrs): <input name="term_yrs" type="text" value="5"></p>
	<p>Repayment frequency: <select name="S1">
	<option selected="" value="12">Monthly</option>
	<option value="1">Yearly</option>
	<option value="4">Quarterly</option>
	<option value="52">Weekly</option>
	</select></p>
	<p>
	<input name="B1" onclick="computeLoan()" type="button" value="Calculate payment"><br>
	</p>
	<p>CALCULATIONS</p>
	<p>Number of payments: <input name="num_pay" type="text" value=""></p>
	<p><span id="ma"></span>Payment:
	<input name="payment_dol" size="30" type="text" value=""></p>
	<p>Total amount paid: <input name="tot_pay" type="text" value=""></p>
	<p>Total interest paid: <input name="tot_int" type="text" value=""></p>
</form>
<script type="text/javascript">
 "use strict"
computeLoan(); 
function computeLoan(){
	var amount, interest_rate,term_yr,n,i,paym, msg, d=document;
	var period=d.ff.S1.options[d.ff.S1.selectedIndex].value;	
	amount=d.ff.amount.value;
	interest_rate=d.ff.interest_rate_pc.value;
	term_yr=d.ff.term_yrs.value;
	n=term_yr*period;
	d.ff.num_pay.value=n;
	i=interest_rate/100/period;
	paym=amount*i*Math.pow((1+i),n)/((Math.pow((1+i),n))-1);
	msg=(period==12)?"Monthly" : (period==1)?"Yearly" : (period==52)?"Weekly" : (period==4)?"Quarterly" : "null";
	d.ff.payment_dol.value = "$"+paym.toFixed(2);
	d.getElementById("ma").innerHTML=msg;
	d.ff.tot_pay.value=(paym*n).toFixed(2);	
	d.ff.tot_int.value=((paym*n)-amount).toFixed(2);	
    //
    }	
</script>

</body>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.