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.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.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"> const { useState } = React; function App() { const [inputText, setInputText] = useState('The brown dog jumped over the lazy dog when the brown fox was sleeping'); const [wordCounts, setWordCounts] = useState({}); const countWords = () => { const words = inputText.toLowerCase().split(/\s+/); const counts = {}; for (const word of words) { counts[word] = (counts[word] || 0) + 1; } setWordCounts(counts); }; return ( <div className='container'> <h2 className='title'>React Js Count the occurrences of each word in a string</h2> <textarea className='input-text' placeholder="Enter text..." value={inputText} onChange={(e) => setInputText(e.target.value)} /> <button className='count-button' onClick={countWords}>Count Words</button> <div className='word-counts'> <h3 className='word-counts-title'>Word Counts:</h3> <ul className='word-list'> {Object.entries(wordCounts).map(([word, count]) => ( <li key={word} className='word-item'> {word}: {count} </li> ))} </ul> </div> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> * { box-sizing: border-box; } .container { margin: 0 auto; width: 600px; text-align: center; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); padding: 20px; } .title { font-size: 24px; text-align: center; margin-bottom: 20px; color: #333; } .input-text { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 5px; font-size: 16px; box-sizing: border-box; } .count-button { display: block; width: 100%; padding: 10px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; font-size: 18px; cursor: pointer; margin-top: 10px; } .count-button:hover { background-color: #0056b3; } .word-counts { margin-top: 20px; } .word-counts-title { font-size: 18px; color: #333; } .word-list { list-style-type: none; padding: 0; } .word-item { font-size: 16px; margin-bottom: 5px; color: #555; } </style> </body> </html>