Added array shuffle

This commit is contained in:
Angelos Chalaris
2017-12-12 18:39:24 +02:00
parent e2d2b6aa41
commit 836216242d
2 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,12 @@
### Shuffle array values
Create an array of random values by using `Array.map()` and `Math.random()`.
Use `Array.sort()` to sort the elements of the original array based on the random values.
```js
const shuffle = arr => {
let r = arr.map(Math.random);
return arr.sort((a,b) => r[a] - r[b]);
}
// shuffle([1,2,3]) -> [2, 1, 3]
```