js how to shufle elements in array 1

js how to shufle elements in array

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const shuffledArray = array.sort((a, b) => Math.random() - 0.5);

Here is what the above code is Doing:
1. We’re creating an array of numbers from 1 to 10.
2. We’re using the sort() method to shuffle the array.
3. We’re using the Math.random() method to generate a random number between 0 and 1.
4. We’re subtracting 0.5 from the random number.
5. We’re using the random number to sort the array.

Similar Posts