Skip to content

Commit

Permalink
Merge pull request Chalarangelo#151 from arjunmahishi/sample
Browse files Browse the repository at this point in the history
Added snippet to sample an array
  • Loading branch information
Chalarangelo authored Dec 15, 2017
2 parents 73b897a + edd961a commit a524b0d
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions snippets/array-sample.md
Original file line number Diff line number Diff line change
@@ -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
```

0 comments on commit a524b0d

Please sign in to comment.