import { useState } from 'react';
function NumberInput() {
const [inputValue, setInputValue] = useState('');
const [errorMessage, setErrorMessage] = useState('');
const handleInputChange = (event) => {
const value = event.target.value;
setErrorMessage('');
// Only allow positive numbers
if (value < 0) {
setErrorMessage('Please enter a positive number');
return;
}
// Only allow numbers
if (isNaN(value)) {
setErrorMessage('Please enter a valid number');
return;
}
setInputValue(value);
if (value % 2 === 0) {
// If the number is even, show the next 3 even numbers
const nextEvens = [value + 2, value + 4, value + 6];
setErrorMessage(`The next even numbers are: ${nextEvens.join(', ')}`);
} else {
// If the number is odd, show the next 3 odd numbers
const nextOdds = [value + 2, value + 4, value + 6];
setErrorMessage(`The next odd numbers are: ${nextOdds.join(', ')}`);
}
};
return (
<div>
<label htmlFor="number-input">Enter a number:</label>
<input
type="number"
id="number-input"
value={inputValue}
onChange={handleInputChange}
/>
{errorMessage && <div style={{ color: 'red' }}>{errorMessage}</div>}
</div>
);
}
export default NumberInput;
Question is Create a simple UI with one input field that accepts only numbers.
If the user types negative numbers, show “enter a positive value”.
If the user types an even number, show the next 3 even numbers.
If the user types an odd number, show the next 3 odd numbers.
and i think logic is correct but i can’t get the proper answer
ex – i type a even number but couldn’t get the correct next three even number