function BasicSTDCalculator() { const [speed, setSpeed] = useState(''); const [distance, setDistance] = useState(''); const [time, setTime] = useState(''); const [speedUnit, setSpeedUnit] = useState('kmh'); const [result, setResult] = useState(null); const calculate = () => { const s = parseFloat(speed); const d = parseFloat(distance); const t = parseFloat(time); let calculated = {}; if (s && d && !t) { // Calculate time calculated.time = (d / s).toFixed(2); calculated.formula = 'Time = Distance / Speed'; calculated.calculation = `${d} / ${s} = ${calculated.time} hours`; } else if (s && t && !d) { // Calculate distance calculated.distance = (s * t).toFixed(2); calculated.formula = 'Distance = Speed × Time'; calculated.calculation = `${s} × ${t} = ${calculated.distance} km`; } else if (d && t && !s) { // Calculate speed calculated.speed = (d / t).toFixed(2); calculated.formula = 'Speed = Distance / Time'; calculated.calculation = `${d} / ${t} = ${calculated.speed} km/h`; } setResult(calculated); }; return (
Formula: Speed = Distance / Time | Distance = Speed × Time | Time = Distance / Speed
setSpeed(e.target.value)} placeholder="60" />
setDistance(e.target.value)} placeholder="120" />
setTime(e.target.value)} placeholder="2" />
{result && Object.keys(result).length > 0 && (
Formula Used: {result.formula}
Calculation: {result.calculation}
{result.speed &&
Speed: {result.speed} km/h
} {result.distance &&
Distance: {result.distance} km
} {result.time &&
Time: {result.time} hours
}
)}
); } // Export to global registry window.BasicSTDCalculator = BasicSTDCalculator;