mercredi 3 février 2021

How to make a function pick a random number without repeating numbers, then start over once its gone through every number?

So im doing some self practice/study with javascript. I am creating a quote generator. I have an array of quote objects. right now, my function randomly chooses an object out of the array and posts a quote with this code.

-

btn.addEventListener("click", function () {
    for (obj of quoteList)
        rando = quoteList[Math.floor(Math.random() * quoteList.length)];
    author.innerHTML = `~${rando.author}`;
    quote.innerHTML = rando.quote;
});

The problem is that I noticed that sometimes I can click the button to get a new quote and by random chance the same quote may pop up 3 times in a row at times. how can I ensure this never happens? How can I reorder the permutations then have it loop through again once it has exhausted all of the objects in the array?




Aucun commentaire:

Enregistrer un commentaire