Changed around line 1
+ const units = {
+ length: ['m', 'cm', 'mm', 'km', 'in', 'ft', 'yd', 'mi'],
+ weight: ['kg', 'g', 'mg', 'lb', 'oz'],
+ volume: ['l', 'ml', 'gal', 'qt', 'pt', 'fl oz'],
+ temperature: ['°C', '°F', 'K'],
+ area: ['m²', 'ft²', 'in²', 'ha', 'ac']
+ };
+
+ const conversionFactors = {
+ // Length conversions
+ m: { cm: 100, mm: 1000, km: 0.001, in: 39.3701, ft: 3.28084, yd: 1.09361, mi: 0.000621371 },
+ cm: { m: 0.01, mm: 10, km: 1e-5, in: 0.393701, ft: 0.0328084, yd: 0.0109361, mi: 6.2137e-6 },
+ // ... (add all other conversion factors)
+
+ // Temperature conversions
+ '°C': {
+ '°F': (c) => (c * 9/5) + 32,
+ K: (c) => c + 273.15
+ },
+ '°F': {
+ '°C': (f) => (f - 32) * 5/9,
+ K: (f) => (f - 32) * 5/9 + 273.15
+ },
+ K: {
+ '°C': (k) => k - 273.15,
+ '°F': (k) => (k - 273.15) * 9/5 + 32
+ }
+ };
+
+ function populateUnitSuggestions() {
+ const datalist = document.getElementById('units');
+ const allUnits = Object.values(units).flat();
+ allUnits.forEach(unit => {
+ const option = document.createElement('option');
+ option.value = unit;
+ datalist.appendChild(option);
+ });
+ }
+
+ function convertUnits() {
+ const inputValue = parseFloat(document.getElementById('inputValue').value);
+ const inputUnit = document.getElementById('inputUnit').value;
+ const outputDiv = document.getElementById('output');
+
+ if (isNaN(inputValue)) {
+ outputDiv.innerHTML = '
Please enter a valid number
';
+ return;
+ }
+
+ if (!conversionFactors[inputUnit]) {
+ outputDiv.innerHTML = `
Unknown unit: ${inputUnit}
`;
+ return;
+ }
+
+ let resultsHTML = '';
+ const conversions = conversionFactors[inputUnit];
+
+ for (const [unit, factor] of Object.entries(conversions)) {
+ let convertedValue;
+ if (typeof factor === 'function') {
+ convertedValue = factor(inputValue).toFixed(2);
+ } else {
+ convertedValue = (inputValue * factor).toFixed(2);
+ }
+ resultsHTML += `
+
+ ${convertedValue} ${unit}
+
+ `;
+ }
+
+ outputDiv.innerHTML = resultsHTML;
+ }
+
+ // Theme toggle
+ const themeToggle = document.getElementById('themeToggle');
+ themeToggle.addEventListener('click', () => {
+ document.body.classList.toggle('dark-mode');
+ });
+
+ // Initialize
+ document.getElementById('convertBtn').addEventListener('click', convertUnits);
+ populateUnitSuggestions();