break into spearte files
This commit is contained in:
66
script.js
Normal file
66
script.js
Normal file
@@ -0,0 +1,66 @@
|
||||
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. 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');
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user