1

I have a button which will call the function that contains the array and will shuffle it and return the first array after the click: Button:

<button onclick="show()">
    click
</button>

My Js:

<script>

const cars = [1,2,3,4,5,6,7,8]; 

</script>
KirkSoft
  • 13
  • 3
  • 1
    Does this answer your question? [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – Francis G Jul 15 '22 at 07:12

1 Answers1

0

Check this out: Found it somewhere in geeksforgeeks credit to them.

<!DOCTYPE html>
<html>
 
<head>

</head>
 
<body>
    <button onclick="show()">
        click
    </button>
     
    <script>
 
        // Function to shuffle the array content
        function shuffleArray(array) {
            for (var i = array.length - 1; i > 0; i--) {
 
                // Generate random number
                var j = Math.floor(Math.random() * (i + 1));
 
                var temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
 
            return array;
        }
 
        // Function to show the result
        function show() {
            var arr = [1, 2, 3, 4, 5, 6, 7]
            var arr1 = shuffleArray(arr)
 
            document.write("After shuffling: ", arr1[0])
        }
    </script>
</body>
 
</html>
xrak
  • 111
  • 6