diff --git a/fuel-calc.js b/fuel-calc.js index bb8c610..58eb885 100644 --- a/fuel-calc.js +++ b/fuel-calc.js @@ -5,6 +5,7 @@ function updateLanguage() { const language = langData[currentLang]; document.getElementById("header").textContent = language.header; + document.getElementById("description").textContent = language.description; document.getElementById("dTaxLabel").textContent = language.dTaxLabel; document.getElementById("dPriceLabel").textContent = language.dPriceLabel; document.getElementById("dConsLabel").textContent = language.dConsLabel; @@ -43,34 +44,35 @@ function calculate() { const pCons = parseFloat(document.getElementById("pCons").value); const annualKm = parseFloat(document.getElementById("annualKm").value); - const dieselCostPerKm = (dPrice * dCons) / 100; - const petrolCostPerKm = (pPrice * pCons) / 100; - - const dieselAnnualCost = (dTax + (dieselCostPerKm * annualKm)); - const petrolAnnualCost = (pTax + (petrolCostPerKm * annualKm)); - - const diff = Math.abs(dieselAnnualCost - petrolAnnualCost); - - let breakEvenCons = 0; - if (dCons < pCons || dTax > pTax) { - breakEvenCons = (pCons - ((dTax - pTax) / (annualKm * (pPrice - dPrice)))); - if (breakEvenCons <= 0) breakEvenCons = 0; - } + + // 1. Calculate Break-Even Petrol Consumption + // Formula: P_Cons = (100 * (dTax - pTax) / (annualKm * pPrice)) + (dCons * dPrice / pPrice) + const taxDiff = dTax - pTax; + const breakEven = (100 * taxDiff / (annualKm * pPrice)) + (dCons * dPrice / pPrice); + + // 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 (dPrice < pPrice || dTax > pTax) { - minDieselPrice = (pPrice - ((dTax - pTax) / (annualKm * (pCons - dCons)))); - if (minDieselPrice <= 0) 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("displayKm").textContent = annualKm.toLocaleString(); - document.getElementById("breakEvenCons").textContent = breakEvenCons.toFixed(2); + document.getElementById("breakEvenCons").textContent = breakEven.toFixed(2); document.getElementById("minDieselPrice").textContent = minDieselPrice.toFixed(2); const tbody = document.getElementById("comparisonTable").getElementsByTagName('tbody')[0]; tbody.innerHTML = ''; - for (let i = 10000; i <= 50000; i += 5000) { + for (let i = 5000; i <= 50000; i += 5000) { const row = tbody.insertRow(); const cell1 = row.insertCell(0); const cell2 = row.insertCell(1); diff --git a/index.html b/index.html index 272c20c..2546ae1 100644 --- a/index.html +++ b/index.html @@ -23,6 +23,9 @@ +