1const shuffleArray = (arr) => arr.sort(() => 0.5 - Math.random());
2
3console.log(shuffleArray([1, 2, 3, 4]));
4// Result: [ 1, 4, 3, 2 ]
5
1/**
2 * Shuffles array in place.
3 * @param {Array} a items An array containing the items.
4 */
5function shuffle(a) {
6 var j, x, i;
7 for (i = a.length - 1; i > 0; i--) {
8 j = round(random() * (i + 1));
9 x = a[i];
10 a[i] = a[j];
11 a[j] = x;
12 }
13 return a;
14}
15
16shuffle(array);
1function shuffleArray(array) {
2 for (let i = array.length - 1; i > 0; i--) {
3 const j = Math.floor(Math.random() * (i + 1));
4 [array[i], array[j]] = [array[j], array[i]];
5 }
6}
1let numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
2numbers = numbers.sort(function(){ return Math.random() - 0.5});
3/* the array numbers will be equal for example to [120, 5, 228, -215, 400, 458, -85411, 122205] */