jeudi 5 septembre 2019

How do I make this work for a BINGO game?

Trying to make Bingo game with Javascript. Need to generate random, non-repetitive values (not integers) from an array and display what number is generated. Each value should be generated ONLY ONCE until the game is reset.

I've tried just trying to generate random element from the original Set() that the values are located in, but that will still result in repeated numbers being called. I'm currently trying to implement the Fisher-Yates/Durstenfeld shuffles, but those do not display ANY result in my HTML.

    <head>
        <title>BINGO</title>
    </head>

    <body>
        <div id="bingo">
            <script>

                let numbers = new Set()
                        .add("B1")
                        .add("B2")
                        .add("B3")
                        .add("B4")
                        .add("B5")
                        .add("B6")
                        .add("B7")
                        .add("B8")
                        .add("B9")
                        .add("B10");

                let called = Array.from(numbers);

                let display = new Array();


function getRandomNum()
{
    function rando()
    {
            for (let i = called.length - 1; i > called.length; i++) 
        {
        const j = Math.floor(Math.random() * called.length);
        const number = called[i];
        called[i] = called[j];
        called[j] = number;
        return number;

            let show = called[Math.floor(Math.random() * called.length)];
            return show;
        }
    let index = rando();

            document.getElementById('bingo').innerHTML = index;
        display.push(index);
    document.getElementById('bingo').innerHTML = display[0];
    }
}



            </script>
        </div>

        <div id="button">

            <button onclick="getRandomNum()">Random Number</button>

        </div>
    </body>
</html>

Here, I was trying to shuffle the original array, pick a random element from that array to display, display the element, then add it to another array that will display what numbers were called




Aucun commentaire:

Enregistrer un commentaire