function BoatsCalculator() { const [downstream, setDownstream] = useState(''); const [upstream, setUpstream] = useState(''); const [boatSpeed, setBoatSpeed] = useState(''); const [streamSpeed, setStreamSpeed] = useState(''); const [result, setResult] = useState(null); const calculate = () => { let calculated = {}; if (downstream && upstream) { const down = parseFloat(downstream); const up = parseFloat(upstream); const boat = (down + up) / 2; const stream = (down - up) / 2; calculated = { boatSpeed: boat.toFixed(2), streamSpeed: stream.toFixed(2), formula: 'Boat = (Down + Up)/2, Stream = (Down - Up)/2', calculation: `Boat = (${down} + ${up})/2 = ${boat.toFixed(2)} km/h\nStream = (${down} - ${up})/2 = ${stream.toFixed(2)} km/h` }; } else if (boatSpeed && streamSpeed) { const boat = parseFloat(boatSpeed); const stream = parseFloat(streamSpeed); const down = boat + stream; const up = boat - stream; calculated = { downstream: down.toFixed(2), upstream: up.toFixed(2), formula: 'Downstream = Boat + Stream, Upstream = Boat - Stream', calculation: `Downstream = ${boat} + ${stream} = ${down.toFixed(2)} km/h\nUpstream = ${boat} - ${stream} = ${up.toFixed(2)} km/h` }; } setResult(calculated); }; return (
Formulas:
Downstream = Boat Speed + Stream Speed
Upstream = Boat Speed - Stream Speed
Boat Speed = (Downstream + Upstream) / 2
Stream Speed = (Downstream - Upstream) / 2
Option 1: Find Boat & Stream Speed
setDownstream(e.target.value)} placeholder="15" />
setUpstream(e.target.value)} placeholder="9" />
Option 2: Find Downstream & Upstream
setBoatSpeed(e.target.value)} placeholder="12" />
setStreamSpeed(e.target.value)} placeholder="3" />
{result && (
Formula: {result.formula}
Calculation:
{result.calculation}
{result.boatSpeed &&
Boat Speed in Still Water: {result.boatSpeed} km/h
} {result.streamSpeed &&
Stream Speed: {result.streamSpeed} km/h
} {result.downstream &&
Downstream Speed: {result.downstream} km/h
} {result.upstream &&
Upstream Speed: {result.upstream} km/h
}
)}
); } // Export to global registry window.BoatsCalculator = BoatsCalculator;