dimanche 14 octobre 2018

JavaScript - Loop through non-repeating values displaying only one at a time

I have this page with a button the user can click on to generate a random quote.

I started with something like this:

        function randomize() {
            var myrandom = Math.round(Math.random() * 4)
            if (myrandom == 0)
                document.getElementById("randomQuote").innerHTML = "Quote 01"
            else if (myrandom == 1)
                document.getElementById("randomQuote").innerHTML = "Quote 02"
            else if (myrandom == 2)
                document.getElementById("randomQuote").innerHTML = "Quote 03"
            else if (myrandom == 3)
                document.getElementById("randomQuote").innerHTML = "Quote 04"
            else if (myrandom == 4)
                document.getElementById("randomQuote").innerHTML = "Quote 05"
        }
    <p><button onclick="randomize()" type="button">Random Quote</button></p>
    <p id="randomQuote"></p>

But since I don't want to repeat the quotes (at least until all of them have been showed once), and after looking at this post I tried the following:

        function randomize() {
            var quotes = ["Quote 01", "Quote 02", "Quote 03", "Quote 04", "Quote 05"],
                ranQuotes = [],
                i = quotes.length,
                j = 0;

            while (i--) {
                j = Math.floor(Math.random() * (i + 1));
                ranQuotes.push(quotes[j]);
                quotes.splice(j, 1);
            }
        document.getElementById("randomQuote").innerHTML = ranQuotes;
        }
    <p><button onclick="randomize()" type="button">Random Quote</button></p>
    <p id="randomQuote"></p>

But this solution, along with the alternative solutions on the same post, randomizes and displays all the quotes. I would like to display only one at a time.

How can I do that?




Aucun commentaire:

Enregistrer un commentaire