screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> // Add the import statement for useState hook const { useState } = React; // Define the App component const App = () => { const [generatedNumbers, setGeneratedNumbers] = useState([]); const generateRandomNumber = () => { const maxNumber = 100; const minNumber = 1; const randomNumber = Math.floor(Math.random() * (maxNumber - minNumber + 1)) + minNumber; // Check if the generated number already exists in the state if (generatedNumbers.includes(randomNumber)) { // If it exists, generate a new number recursively return generateRandomNumber(); } // If the number is unique, add it to the state setGeneratedNumbers([...generatedNumbers, randomNumber]); }; return ( <div className='container'> <h3>React Js Generate Random Number betwwen 1-100 without Repetation</h3> <button onClick={generateRandomNumber}>Generate Random Number</button> {/* Reverse the array to show the latest number first */} {generatedNumbers.slice().reverse().map((number, index) => ( <p key={index}>{number}</p> ))} </div> ); }; ReactDOM.render(<App />, document.getElementById("app")); </script> <style> body { margin: 0; } .container { max-width: 600px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); position: relative; } /* Styles for the button */ .container button { padding: 10px 20px; font-size: 16px; background-color: #4caf50; color: #fff; border: none; border-radius: 4px; cursor: pointer; transition: background-color 0.3s; } .container button:hover { background-color: #45a049; } /* Styles for the generated number */ .container p { margin-top: 20px; font-size: 18px; color: #333; } </style> </body> </html>