编辑:
该函数的债务上限部分实际上将在我尚未编写的步骤中使用。它用于确定用户的债务收入比是否过高,但不会影响现在编写的函数的结果。
但是,我仍然遇到问题,非常感谢任何见解。我想我在 javasctipt 的下一部分中有一些不好的逻辑。这是我的新功能:
$(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);
});
再次感谢您的帮助!
原来的:
这实际上是按揭计算。
当使用正常方式时,我已经让它工作了——也就是说,当用户输入四个变量(贷款金额、首付、利率和期限年)时确定每月付款。
但是当我尝试更改公式来解决贷款金额(负担能力计算器)时,我得到了不好的结果。
这是按揭教授解释的原始公式:
以下公式用于计算按月利率 c 在 n 个月内完全摊销 L 美元贷款所需的固定每月还款额 (P)。[例如,如果报价率为 6%,则 c 为 0.06/12 或 0.005]。
P = L[c(1 + c)n]/[(1 + c)n - 1]
我将首先给出正常工作的示例。
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);
});
不能正常工作的部分:
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);
});
以上是基于查看公式并重新排列它。
原文:x = y(a/b)
求解 y:y = x/(a/b)
不知何故,我的新公式出错了,但我不确定在哪里以及如何修复它。
感谢您的任何帮助!