Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

import React, { useState } from 'react';

function App() {
const [celcius, setCelcius] = useState('');
const [fahrenheit, setFahrenheit] = useState('');
const [kelvin, setKelvin] = useState('');

const convertLength = (value) => {


// Validate the input to ensure it's a positive number
if (isNaN(value) || value <= 0) {
return;
}

// Convert centimeters to inches, feet, yards, and meters


const fahrenheit = (value * 9 / 5 + 32);
const kelvin = ((value - 32) * 5 / 9 + 273.15);

// Update state with the converted values


setFahrenheit(fahrenheit.toFixed(2));
setKelvin(kelvin.toFixed(2));

};

const handleCelciusChange = (event) => {


const value = event.target.value;
setCelcius(value);
convertLength(value);
};

return (
<div>
<h1>Celcius to Fahrenheit and Kelvin</h1>
<label htmlFor="celcius">Celcius:</label>
<input
type="number"
id="celcius"
name="centimeters"
value={celcius}
onChange={handleCelciusChange}
/>

<p>Fahrenheit: {fahrenheit}</p>
<p>Kelvin: {kelvin}</p>

</div>
);
}

export default App;

You might also like