Reputation: 1694
I am building an application that generates a view of space with a random number of clickable stars
. So far I am generating the stars as so:
const makeStars = () => {
var num = Math.floor(Math.random() * 7 + 2);
return (
<div className="starWrapper">
<Star
name={makeid}
starType={starList[Math.floor(Math.random() * 6 + 1)]}
></Star>
<h2>Star: {makeid()}</h2>
</div>
);
};
This is working great, but I want this function to run a random number of times when the pages loads. Here is my attempt so far
const makeStars = () => {
var num = Math.floor(Math.random() * 7 + 2);
var count = 0
if (count < num) {
window.setTimeout(function() {
return(<div className="starWrapper"><Star name={makeid} starType={starList[Math.floor(Math.random() * 6 + 1)]}></Star><h2>Star: {makeid()}</h2></div>
{() => count + 1})
}, 10);
}
but so far this isn't returning anything and I'm not sure why. I don't know if setTimeout
is the best tool for the job, and I'm open to any other suggestions you have.
The full page of code is viewable here.
Upvotes: 0
Views: 762
Reputation: 366
This is the function that can be ran a random amount of times.
function rand(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function createstars(randnum) {
If (randnum > 0) {
/** Do logic here **/
createstars((randnum - 1))
}
}
createstars(rand(1,10)) /** set min and max to your liking , the rand function is inclusive **/
Upvotes: 1