EDITED:
The debt ceiling part of the function is actually going to be used in a step that I haven't written yet. It's used to determine if the user's debt to income ratio is too high but it doesn't effect the outcome of the function as it's written now.
I am still having an issue however and would very much appreciate any insight. I think I've got some bad logic in the next section of the javasctipt. Here's my new function:
$(function(){
function calculate_affordability() {
var income = parseFloat($('#income').val());
var debts = parseFloat($('#debts').val());
var apr_decimal = parseFloat($('#apr').val()) / 100;
var monthly_interest = apr_decimal / 12;
var down = parseFloat($('#down').val());
var term = parseFloat($('#term').val()) * 12;
var debts_ceiling = (debts * .08).toFixed(2);
var mortgage_ceiling = (income * .28).toFixed(2);
var formula_numerator = Math.pow((1 + monthly_interest), term) -1;
var formula_denominator = monthly_interest * Math.pow((1 + monthly_interest), term);
var formula_result = (formula_numerator / formula_denominator);
var loan_amount = (mortgage_ceiling * formula_result).toFixed(2);
// the issue is in here somewhere:
var closing_costs = (loan_amount * .03).toFixed(2);
var actual_down = (down - closing_costs);
var max_purchase_price = (loan_amount + actual_down);
$('#monthly-payment').html('$' + mortgage_ceiling);
$('#loan-amount').html('$' + loan_amount);
$('#max-purchase-price').html('$' + max_purchase_price);
$('#actual-down').html('$' + actual_down);
$('#closing-costs').html('$' + closing_costs);
}
$('#term').keyup(calculate_affordability);
$('#term').mouseup(calculate_affordability);
});
Thanks again for the help!
ORIGINAL:
This is actually a mortgage calculation.
I've got it working when used the normal way - that is, to determine a monthly payment when the user inputs four variables (loan amount, down payment, interest rate, and term years).
But when I try to change the formula to solve for loan amount (an affordability calculator), I'm getting bad results.
Here's the original formula as explained by the Mortgage Professor:
The following formula is used to calculate the fixed monthly payment (P) required to fully amortize a loan of L dollars over a term of n months at a monthly interest rate of c. [If the quoted rate is 6%, for example, c is .06/12 or .005].
P = L[c(1 + c)n]/[(1 + c)n - 1]
I'll first give the example that is working properly.
HTML:
<form>
<div class="col-md-4">
<h4>Home Price</h4>
<input type="text" id="price">
<h4>Down Payment</h4>
<input type="text" id="down">
</div>
<div class="col-md-4">
<h4>Rate (%)</h4>
<input type="text" id="apr">
<h4>Term (Years)</h4>
<input type="text" id="term">
</div>
<div class="col-md-4">
Your Monthly Payment:
<div id="payment"></div>
</div>
</form>
jQuery:
$(function(){
function calculate_monthly_payment() {
var price = parseFloat($('#price').val());
var down = parseFloat($('#down').val());
var apr_decimal = parseFloat($('#apr').val()) / 100;
var monthly_interest = apr_decimal / 12;
var term = parseFloat($('#term').val()) * 12;
var loan_amount = parseFloat(price - down);
var formula_numerator = monthly_interest * Math.pow((1 + monthly_interest), term);
var formula_denominator = Math.pow((1 + monthly_interest), term) -1;
var monthly_payment = parseFloat(loan_amount * (formula_numerator / formula_denominator)).toFixed(2);
if (!isNaN(monthly_payment)) {
$('#payment').html('$' + monthly_payment);
} else {
$('#payment').html('Calculating...');
}
}
$('#price').keyup(calculate_monthly_payment);
$('#down').keyup(calculate_monthly_payment);
$('#apr').keyup(calculate_monthly_payment);
$('#term').keyup(calculate_monthly_payment);
$('#price').mouseup(calculate_monthly_payment);
$('#down').mouseup(calculate_monthly_payment);
$('#apr').mouseup(calculate_monthly_payment);
$('#term').mouseup(calculate_monthly_payment);
});
The part that isn't working properly:
HTML:
<form>
<div class="col-md-6">
<h4>Income (Monthly)</h4>
<input type="text" id="income">
<h4>Debts (Monthly, not including housing costs)</h4>
<input type="text" id="debts">
<h4>Down Payment</h4>
<input type="text" id="down">
<h4>Rate (%)</h4>
<input type="text" id="apr">
<h4>Term (Years)</h4>
<input type="text" id="term">
</div>
<div class="col-md-6">
Monthly Payment:
<div id="monthly-payment"></div>
Loan Amount:
<div id="loan-amount"></div>
Purchase Price:
<div id="purchase-price"></div>
Down Payment:
<div id="actual-down"></div>
Closing Costs:
<div id="closing-costs"></div>
</div>
</form>
jQuery:
$(function(){
function calculate_affordability() {
var income = parseFloat($('#income').val());
var debts = parseFloat($('#debts').val());
var down = parseFloat($('#down').val());
var apr_decimal = parseFloat($('#apr').val()) / 100;
var monthly_interest = apr_decimal / 12;
var term = parseFloat($('#term').val());
var debt_ceiling = income * .08;
var monthly_payment = (income * .28).toFixed(2);
var formula_numerator = monthly_interest * Math.pow((1 + monthly_interest), term);
var formula_denominator = Math.pow((1 + monthly_interest), term) -1;
var loan_amount = parseFloat(monthly_payment / (formula_numerator / formula_denominator)).toFixed(2);
var closing_costs = (loan_amount * .03).toFixed(2);
var actual_down = (down - closing_costs).toFixed(2);
var purchase_price = (loan_amount - actual_down).toFixed(2);
$('#monthly-payment').html('$' + monthly_payment);
$('#loan-amount').html('$' + loan_amount);
$('#purchase-price').html('$' + purchase_price);
$('#actual-down').html('$' + actual_down);
$('#closing-costs').html('$' + closing_costs);
}
$('#term').keyup(calculate_affordability);
$('#term').mouseup(calculate_affordability);
});
The above is based on looking at the formula and rearranging it.
Original: x = y(a/b)
Solving for y: y = x/(a/b)
Somehow I've gone wrong with the new formula but I'm not sure where nor how to fix it.
Thank you for any help!