function RacesCalculator() { const [raceType, setRaceType] = useState('distance'); const [speedA, setSpeedA] = useState(''); const [speedB, setSpeedB] = useState(''); const [raceDistance, setRaceDistance] = useState(''); const [beatBy, setBeatBy] = useState(''); const [result, setResult] = useState(null); const calculate = () => { const sA = parseFloat(speedA); const sB = parseFloat(speedB); const dist = parseFloat(raceDistance); const beat = parseFloat(beatBy); let calculated = {}; if (raceType === 'distance') { // A beats B by x meters const timeA = dist / sA; const distB = dist - beat; const actualSpeedB = distB / timeA; const ratio = sA / actualSpeedB; calculated = { timeA: timeA.toFixed(2), distanceB: distB, speedRatio: ratio.toFixed(2), formula: 'When A finishes, B is behind by given distance', calculation: `Time taken by A = ${dist} / ${sA} = ${timeA.toFixed(2)} hours\nDistance covered by B = ${distB} m\nSpeed ratio = ${ratio.toFixed(2)}:1` }; } else { // A beats B by t seconds const timeA = dist / sA; const timeB = timeA + (beat / 3600); // Convert seconds to hours const actualSpeedB = dist / timeB; const ratio = sA / actualSpeedB; calculated = { timeA: timeA.toFixed(2), timeB: timeB.toFixed(2), timeDiff: beat, speedRatio: ratio.toFixed(2), formula: 'When A finishes, B takes additional time', calculation: `Time by A = ${dist} / ${sA} = ${timeA.toFixed(2)} hours\nTime by B = ${timeB.toFixed(2)} hours\nSpeed ratio = ${ratio.toFixed(2)}:1` }; } setResult(calculated); }; return (