add min d price

This commit is contained in:
Vasily Davydov
2026-09-21 12:44:26 +03:00
parent 3752b615a7
commit 6652655e2b
2 changed files with 18 additions and 2 deletions

View File

@@ -48,7 +48,8 @@
<div class="result-box">
To offset the tax difference at <span id="displayKm">17500</span> km/year,
a petrol car would need to consume at least <b id="breakEvenCons">0.00</b> L/100km.
a petrol car would need to consume at least <b id="breakEvenCons">0.00</b> L/100km,
or the diesel price should be at least <b id="minDieselPrice">0.00</b> €/L.
</div>
<table id="comparisonTable">

View File

@@ -18,7 +18,22 @@ function calculate() {
document.getElementById('displayKm').innerText = annualKm.toLocaleString();
document.getElementById('breakEvenCons').innerText = isFinite(breakEven) ? breakEven.toFixed(2) : "0.00";
// 2. Generate Table
// 2. Calculate Minimum Diesel Price where user's pCons equals break-even consumption
// We want to solve for dPrice such that: pCons = (100 * taxDiff / (annualKm * pPrice)) + (dCons * dPrice / pPrice)
// Rearranging for dPrice:
// pCons - (100 * taxDiff / (annualKm * pPrice)) = (dCons * dPrice / pPrice)
// dPrice = (pCons - (100 * taxDiff / (annualKm * pPrice))) * pPrice / dCons
let minDieselPrice = 0;
if (dCons > 0 && annualKm > 0 && pPrice > 0) {
const firstTerm = 100 * taxDiff / (annualKm * pPrice);
const secondTerm = pCons - firstTerm;
minDieselPrice = secondTerm * pPrice / dCons;
}
document.getElementById('minDieselPrice').innerText = isFinite(minDieselPrice) ? minDieselPrice.toFixed(2) : "0.00";
// 3. Generate Table
const distances = [5000, 10000, 15000, 20000, 25000, 30000];
const tbody = document.querySelector('#comparisonTable tbody');
tbody.innerHTML = '';