screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script> <script src="https://unpkg.com/@babel/standalone@7.14.7/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState, useEffect } = React; function App() { const numbers = [5, 8, 2, 10, 3]; const [maxPosition, setMaxPosition] = useState(null); const [maxValue, setMaxValue] = useState(null); useEffect(() => { const maxVal = Math.max(...numbers); const maxPos = numbers.indexOf(maxVal); setMaxPosition(maxPos); setMaxValue(maxVal); }, []); return ( <div className='container'> <h1>REACT Js - GET Position of maximum element in list</h1> <h3>List of Numbers: {numbers.join(', ')}</h3> {maxPosition !== null && ( <p> The maximum element is {maxValue}, and its position is {maxPosition}. </p> )} </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { width: 600px; margin: 0 auto; box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 6px -1px, rgba(0, 0, 0, 0.06) 0px 2px 4px -1px; padding: 20px } </style> </body> </html>