1

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

2 Answers 2

1

You could combine simple for loop with modulo operator % to get the desired result

const data=["Red","Green","blue","orange","yellow"]
const { useState } = React

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
    }
   
    const slice = []
    for (let i = 0; i < amount; i++) {
        slice.push(data[i % data.length])
    }
    
    setText(slice)
  }

  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>
  )
}

ReactDOM.render(<App />, document.querySelector("#app"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="app"></div>

Sign up to request clarification or add additional context in comments.

Comments

0

You can try something like that:

setText(
    Array.from({length: amount}).map((_, i) => data[i % data.length])
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.