131°F to °C Conversion Calculator
// Handle source input sourceInput.addEventListener('input', function() { const inputVal = this.value;
if (inputVal === '' || isNaN(inputVal)) { targetInput.value = ''; resultDisplay.textContent = 'Please enter a valid temperature'; return; }
const sourceTemp = parseFloat(inputVal); const targetTemp = convert(sourceTemp, conversionType);
targetInput.value = targetTemp; updateResultDisplay(sourceTemp, targetTemp); animateResult(); });
// Handle target input targetInput.addEventListener('input', function() { const inputVal = this.value;
if (inputVal === '' || isNaN(inputVal)) { sourceInput.value = ''; resultDisplay.textContent = 'Please enter a valid temperature'; return; }
const targetTemp = parseFloat(inputVal); const sourceTemp = reverseConvert(targetTemp, conversionType);
sourceInput.value = formatTemperature(sourceTemp); updateResultDisplay(sourceTemp, targetTemp); animateResult(); });
function convert(temp, type) { if (type === 'c_to_f') { return formatNumber((temp * 9/5) + 32); } else { return formatNumber((temp - 32) * 5/9); } }
function reverseConvert(temp, type) { if (type === 'c_to_f') { return formatNumber((temp - 32) * 5/9); } else { return formatNumber((temp * 9/5) + 32); } }
function formatNumber(num) { return parseFloat(num.toFixed(2)); }
function formatTemperature(temp) { return Number.isInteger(parseFloat(temp)) ? parseInt(temp) : parseFloat(temp).toFixed(1);}
function updateResultDisplay(sourceTemp, targetTemp) { const formattedSource = formatTemperature(sourceTemp); const formattedTarget = formatNumber(targetTemp); const sourceUnit = conversionType === 'c_to_f' ? '°C' : '°F'; const targetUnit = conversionType === 'c_to_f' ? '°F' : '°C'; resultDisplay.textContent = `${formattedSource}${sourceUnit} = ${formattedTarget}${targetUnit}`; }
function animateResult() { resultDisplay.classList.add('updated'); setTimeout(() => resultDisplay.classList.remove('updated'), 300); } });