Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to display an icon to a particular number given

please i want to display an icon according to a number given. for instance if the given number is 3 let 3 icon be display. please how can achieve this in TypeScript.

here is my code

const icons = <MdStar />
    const displayIcon = (number:any) => {
        let random: any = Math.floor((Math.random() * 4) + 1);
        if(random > 0 ){
            
            number.length === random
        }
        console.log(number);
    };

    displayIcon(icons)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do this by creating a randomNumber and from the number you can create a array. Finally map over the array and return the MdStar component.

function YourComponent() {
  const randomNumber: number = Math.floor(Math.random() * 4 + 1);

  return (
    <>
      {Array.from({ length: randomNumber }, (_, i) => i).map((_, i) => (
        <MdStar key={i} />
      ))}
    </>
  );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading