Length Converter Tool
body {
background: linear-gradient(to right, #6a11cb, #2575fc);
font-family: Arial, sans-serif;
color: #333;
}
.card {
border-radius: 12px;
}
.btn-success {
background-color: #28a745;
border: none;
transition: background-color 0.3s ease;
}
.btn-success:hover {
background-color: #218838;
}
#outputValue {
font-size: 1.5rem;
}
function convertLength() {
const inputValue = parseFloat(document.getElementById('inputValue').value);
const inputUnit = document.getElementById('inputUnit').value;
const outputUnit = document.getElementById('outputUnit').value;
if (isNaN(inputValue)) {
alert("Please enter a valid number");
return;
}
// Conversion factors
const conversionFactors = {
meters: 1,
kilometers: 0.001,
feet: 3.28084,
inches: 39.3701,
yards: 1.09361,
};
// Convert input to meters
const valueInMeters = inputValue / conversionFactors[inputUnit];
// Convert from meters to the target unit
const convertedValue = valueInMeters * conversionFactors[outputUnit];
// Display the converted value
document.getElementById('outputValue').textContent = convertedValue.toFixed(4) + ' ' + outputUnit;
}