Repeat a function y amount of times

I want to repeat a function.

so I have:

function repeatHealth(func, times) {
console.log(Math.floor((Math.random() * 10) + 1);) // Maybe the reponse was 7

func()
    times && --times && repeatHealth(func, times);
}

repeatHealth(function () {
}, 5)

Here it would repeat the function 5 times. I want it to “refresh” the function so it loops through the whole function y amount of times. If I were to run this code then I would keep getting 7 five times. I want it so that I would get a random number for each time the function refreshes.

So If I were to set it at two then I want to console.log maybe 8 and maybe 3. I don’t want it to just copy paste the response, I want to get a different response each refresh.

1 Like

I completely forgot about for loops… thanks for reminding me.

3 Likes