import React, { useState } from 'react'
const data=["Red","Green","blue","orange","yellow"]
function App() {
const [count, setCount] = useState(0)
const [text, setText] = useState([])
const handleSubmit = (e) => {
e.preventDefault()
let amount = parseInt(count)
if (count <= 0) {
amount = 1
}
//for now hardcoding to show only 5 item
if (count > 5) {
amount = 5
}
setText(data.slice(0, amount))
}
return (
<section >
<h3>Generate Fun Ipsum</h3>
<form onSubmit={handleSubmit}>
<label htmlFor='amount'>paragraphs:</label>
<input
type='number'
name='amount'
id='amount'
value={count}
onChange={(e) => setCount(e.target.value)}
/>
<button type='submit'>
generate
</button>
</form>
<article>
{text.map((item, index) => {
return <p key={index}>{item}</p>
})}
</article>
</section>
)
}
export default App
My query here is if user input value 8 then i should again reiterate the array and display 8 items
output should look like - "Red","Green","blue","orange","yellow","Red","Green","blue"
if user inputs 10 then output should be:
"Red","Green","blue","orange","yellow","Red","Green","blue","orange","yellow"
if user inputs 12 then output should be: "Red","Green","blue","orange","yellow","Red","Green","blue","orange","yellow","Red","Green"
I hope i am clear