| 
 | 1 | +// Import React and the useState hook for state management  | 
1 | 2 | import React, { useState } from 'react';  | 
 | 3 | + | 
 | 4 | +// Importing the data array which contains the paragraphs to be displayed  | 
2 | 5 | import data from './data';  | 
 | 6 | + | 
3 | 7 | function App() {  | 
 | 8 | +  // State to keep track of the number of paragraphs to generate  | 
4 | 9 |   const [count, setCount] = useState(0);  | 
 | 10 | + | 
 | 11 | +  // State to store the paragraphs to display  | 
5 | 12 |   const [text, setText] = useState([]);  | 
6 | 13 | 
 
  | 
 | 14 | +  // Event handler for the form submission  | 
7 | 15 |   const handleSubmit = (e) => {  | 
8 |  | -    e.preventDefault();  | 
 | 16 | +    e.preventDefault(); // Prevent the form from refreshing the page  | 
 | 17 | + | 
 | 18 | +    // Convert the count input value to an integer  | 
9 | 19 |     let amount = parseInt(count);  | 
 | 20 | + | 
 | 21 | +    // Ensure the count is at least 1 and at most 8  | 
10 | 22 |     if (count <= 0) {  | 
11 | 23 |       amount = 1;  | 
12 | 24 |     }  | 
13 | 25 |     if (count > 8) {  | 
14 | 26 |       amount = 8;  | 
15 | 27 |     }  | 
 | 28 | + | 
 | 29 | +    // Update the text state with the sliced data array based on the amount  | 
16 | 30 |     setText(data.slice(0, amount));  | 
17 | 31 |   };  | 
 | 32 | + | 
18 | 33 |   return (  | 
19 | 34 |     <section className='section-center'>  | 
20 | 35 |       <h3>tired of boring lorem ipsum?</h3>  | 
 | 36 | +      {/* Form for selecting the number of paragraphs */}  | 
21 | 37 |       <form className='lorem-form' onSubmit={handleSubmit}>  | 
22 | 38 |         <label htmlFor='amount'>paragraphs:</label>  | 
 | 39 | +        {/* Input for the number of paragraphs */}  | 
23 | 40 |         <input  | 
24 | 41 |           type='number'  | 
25 | 42 |           name='amount'  | 
26 | 43 |           id='amount'  | 
27 | 44 |           value={count}  | 
28 |  | -          onChange={(e) => setCount(e.target.value)}  | 
 | 45 | +          onChange={(e) => setCount(e.target.value)} // Update count state on input change  | 
29 | 46 |         />  | 
30 | 47 |         <button className='btn'>generate</button>  | 
31 | 48 |       </form>  | 
 | 49 | + | 
 | 50 | +      {/* Display the generated paragraphs */}  | 
32 | 51 |       <article className='lorem-text'>  | 
33 | 52 |         {text.map((item, index) => {  | 
 | 53 | +          // Render each paragraph in a <p> tag with a unique key  | 
34 | 54 |           return <p key={index}>{item}</p>;  | 
35 | 55 |         })}  | 
36 | 56 |       </article>  | 
 | 
0 commit comments