dynamically cap down_pct slider based on start_capital
This commit is contained in:
29
ui.js
29
ui.js
@@ -280,6 +280,7 @@ const update_results = (result) => {
|
||||
// ===== Main recalc =====
|
||||
|
||||
const recalc = () => {
|
||||
clamp_down_pct();
|
||||
const p = get_params();
|
||||
const result = run_simulation(p);
|
||||
|
||||
@@ -289,6 +290,26 @@ const recalc = () => {
|
||||
build_table(result);
|
||||
};
|
||||
|
||||
// ===== Down payment cap =====
|
||||
|
||||
const clamp_down_pct = () => {
|
||||
const home_price = +document.getElementById('home_price').value;
|
||||
const start_capital = +document.getElementById('start_capital').value;
|
||||
const closing_pct = +document.getElementById('closing_buy_pct').value;
|
||||
const financed = document.getElementById('finance_nebenkosten').checked;
|
||||
|
||||
const closing_costs = home_price * (closing_pct / 100);
|
||||
const available = financed ? start_capital : Math.max(0, start_capital - closing_costs);
|
||||
const max_pct = home_price > 0 ? Math.min(100, (available / home_price) * 100) : 100;
|
||||
|
||||
const slider = document.getElementById('down_pct_slider');
|
||||
const num = document.getElementById('down_pct');
|
||||
if (+num.value > max_pct) {
|
||||
num.value = Math.round(max_pct * 10) / 10;
|
||||
slider.value = num.value;
|
||||
}
|
||||
};
|
||||
|
||||
// ===== Slider ↔ number sync =====
|
||||
|
||||
const wire_slider = (base_id) => {
|
||||
@@ -317,9 +338,13 @@ const SLIDER_IDS = [
|
||||
SLIDER_IDS.forEach(wire_slider);
|
||||
|
||||
// wire plain number inputs (no slider)
|
||||
document.getElementById('finance_nebenkosten').addEventListener('change', recalc);
|
||||
document.getElementById('finance_nebenkosten').addEventListener('change', () => { clamp_down_pct(); recalc(); });
|
||||
|
||||
['home_price', 'insurance', 'monthly_rent', 'renters_ins', 'start_capital'].forEach((id) => {
|
||||
['home_price', 'start_capital'].forEach((id) => {
|
||||
document.getElementById(id).addEventListener('input', () => { clamp_down_pct(); recalc(); });
|
||||
});
|
||||
|
||||
['insurance', 'monthly_rent', 'renters_ins'].forEach((id) => {
|
||||
document.getElementById(id).addEventListener('input', recalc);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user