vendredi 24 août 2018

How can I make my array choose different choices each time it runs?

Right now, I have an array with three options. I have a function set up so it chooses one option each time, but sometimes it repeats outputs. Is there a way to make it not repeat?

<!DOCTYPEhtml>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Tester</title>
    <script type="text/javascript">

        var colors = ['#ce0e0e', '#079b0c', '#3e3fd6'];

        function changeColor(){
            var randomColor = colors[Math.floor(Math.random() * colors.length)];
            while (myDiv.style.backgroundColor == randomColor)
                randomColor = colors[Math.floor(Math.random() * colors.length)];

            myDiv.style.backgroundColor = randomColor;
        }
    </script>
    <style>
        .divClass {
            width:300px;
            height:300px;
            border: 1px solid black;
            border-radius:1000px;
            background-color:transparent;
        }
    </style>
</head>
<body>

    <div class=divClass id="myDiv" style="border:6px solid black;">
    </div>

    <input id=button type=button value="chagne color" onclick='changeColor()'>

</body>
</html>




Aucun commentaire:

Enregistrer un commentaire