简体   繁体   English

抵押计算器/我可以借多少钱

[英]Mortgage Calculator/How much can I borrow

I have to repair this mortgage calculator, but I really don't understand why the calculation is not working.我必须修理这个抵押贷款计算器,但我真的不明白为什么计算不起作用。 I need help to sort this out.我需要帮助来解决这个问题。 Or at least someone to make me understand what is wrong with this function.或者至少有人让我明白这个 function 有什么问题。 (The calculator has an input for Applicants' combined annual income and another input for Deposit. The calculator should show results for Maximum property value and Estimated monthly costs). (计算器有一个输入申请人的综合年收入和另一个输入存款。计算器应显示最大财产价值和估计每月成本的结果)。

function PMT (rate_per_period, number_of_payments, present_value, future_value, type ) {
        var q = Math.pow(1 + rate_per_period, number_of_payments);
        return (rate_per_period * (future_value + (q * present_value))) / ((-1 + q) * (1 + rate_per_period * (type)));
}

jQuery(document).ready(function ($) {

        $(".input").keyup(function () {
            var annualIncomeInput = 0;
            var depositInput = 0;
            var annualIncome = 0;
            var deposit = 0;
            var mortgage = 0;
            var propertyValue = 0;
            var monthlyPayments = 0;
            annualIncomeInput = $('#annualIncome').val();
            depositInput = $('#deposit').val();
            annualIncome = parseFloat(annualIncomeInput.replace(/,/g, ''));
            deposit = parseFloat(depositInput.replace(/,/g, ''));
            if (annualIncomeInput && depositInput) {
                if ((deposit / 0.1 - deposit) >= (annualIncome * 4.5)) {
                    mortgage = annualIncome * 4.5;
                    propertyValue = mortgage + deposit;
                } else {
                    propertyValue = deposit / 0.1;
                    mortgage = propertyValue - deposit;
                }
                monthlyPayments = PMT(0.0307/12, 30*12, mortgage, 0, 0).toFixed(2);
            }
            
            propertyValue = parseFloat(propertyValue).toLocaleString();
            monthlyPayments = parseFloat(monthlyPayments).toLocaleString();
            $('#totalHowMuchCanIBorrow').text('£' + propertyValue);
            $('#estimatedCosts').text('£' + monthlyPayments);
            
        })
    })

If you need more details please let me know!如果您需要更多详细信息,请告诉我! Thank you!谢谢!

This works maybe you find the error by comparing it to yours:) didn't changed anything just added the fitting html to it这可行,也许您通过将其与您的比较来发现错误:) 没有改变任何东西,只是添加了配件 html 到它

<!DOCTYPE html>
<html>

<head>
</head>

<body>
    <input class="input" id='annualIncome' type="number">
    <input class="input" id='deposit' type="number">
    <div id="totalHowMuchCanIBorrow"></div>
    <div id="estimatedCosts"></div>
    <script src="https://code.jquery.com/jquery-3.6.0.js"
        integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
    <script>
        function PMT(rate_per_period, number_of_payments, present_value, future_value, type) {
            var q = Math.pow(1 + rate_per_period, number_of_payments);
            return (rate_per_period * (future_value + (q * present_value))) / ((-1 + q) * (1 + rate_per_period * (type)));
        }

        jQuery(document).ready(function ($) {
            //not sure how your html looks but maybe here you want $("input") and not $(".input") that selects all elements with the css class input that's maybe missing in the html
            $(".input").keyup(function () { 
                var annualIncomeInput = 0;
                var depositInput = 0;
                var annualIncome = 0;
                var deposit = 0;
                var mortgage = 0;
                var propertyValue = 0;
                var monthlyPayments = 0;
                annualIncomeInput = $('#annualIncome').val();
                depositInput = $('#deposit').val();
                annualIncome = parseFloat(annualIncomeInput.replace(/,/g, ''));
                deposit = parseFloat(depositInput.replace(/,/g, ''));
                if (annualIncomeInput && depositInput) {
                    if ((deposit / 0.1 - deposit) >= (annualIncome * 4.5)) {
                        mortgage = annualIncome * 4.5;
                        propertyValue = mortgage + deposit;
                    } else {
                        propertyValue = deposit / 0.1;
                        mortgage = propertyValue - deposit;
                    }
                    monthlyPayments = PMT(0.0307 / 12, 30 * 12, mortgage, 0, 0).toFixed(2);
                }
                
                propertyValue = parseFloat(propertyValue).toLocaleString();
                monthlyPayments = parseFloat(monthlyPayments).toLocaleString();
                $('#totalHowMuchCanIBorrow').text('£' + propertyValue);
                $('#estimatedCosts').text('£' + monthlyPayments);   
                
            })
        })
    </script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM