Merge pull request #151 from arjunmahishi/sample

Added snippet to sample an array
This commit is contained in:
Angelos Chalaris
2017-12-15 12:42:01 +02:00
committed by GitHub

9
snippets/array-sample.md Normal file
View File

@ -0,0 +1,9 @@
### Array sample
Use `Math.random()` to generate a random number, multiply it with `Array.length` and round it of to the nearest whole number using `Math.floor()`. Works with strings too.
```js
const sample = arr => arr[Math.floor(Math.random() * arr.length)];
// sample([3, 7, 9, 11]) -> 9
```