Files
isdieselcheaper/script.js
Vasily Davydov 6652655e2b add min d price
2026-09-21 12:44:26 +03:00

81 lines
3.3 KiB
JavaScript

function calculate() {
// Get values
const dTax = parseFloat(document.getElementById('dTax').value) || 0;
const dPrice = parseFloat(document.getElementById('dPrice').value) || 0;
const dCons = parseFloat(document.getElementById('dCons').value) || 0;
const pTax = parseFloat(document.getElementById('pTax').value) || 0;
const pPrice = parseFloat(document.getElementById('pPrice').value) || 0;
const pCons = parseFloat(document.getElementById('pCons').value) || 0;
const annualKm = parseFloat(document.getElementById('annualKm').value) || 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);
document.getElementById('displayKm').innerText = annualKm.toLocaleString();
document.getElementById('breakEvenCons').innerText = isFinite(breakEven) ? breakEven.toFixed(2) : "0.00";
// 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 = '';
distances.forEach(km => {
const dTotal = dTax + (km * dCons / 100 * dPrice);
const pTotal = pTax + (km * pCons / 100 * pPrice);
const diff = pTotal - dTotal;
const row = document.createElement('tr');
const diffClass = diff >= 0 ? 'saving' : 'loss';
const diffText = diff >= 0 ? `Saving €${diff.toFixed(2)}` : `Loss €${Math.abs(diff).toFixed(2)}`;
row.innerHTML = `
<td>${km.toLocaleString()} km</td>
<td>€${dTotal.toFixed(2)}</td>
<td>€${pTotal.toFixed(2)}</td>
<td class="${diffClass}">${diffText}</td>
`;
tbody.appendChild(row);
});
}
// Initial run
calculate();
function toggleTheme() {
const body = document.body;
body.classList.toggle('dark-mode');
// Save preference to localStorage
if (body.classList.contains('dark-mode')) {
localStorage.setItem('theme', 'dark');
} else {
localStorage.setItem('theme', 'light');
}
}
// Check for saved theme preference and set dark mode by default
document.addEventListener('DOMContentLoaded', function() {
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'dark' || !savedTheme) { // Default to dark mode
document.body.classList.add('dark-mode');
}
});