Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 3282f0d

Browse files
rishabkbakshiKent C. Dodds
authored and
Kent C. Dodds
committed
feat: Simulates roll of a die (#223)
Returns a value between 1 and 6 none
1 parent 8bd0450 commit 3282f0d

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import copyArrayByValue from './copyArrayByValue'
8383
import timeSince from './timeSince'
8484
import first from './first'
8585
import mode from './mode-array'
86+
import rollDice from './rollDice'
8687

8788
export {
8889
reverseArrayInPlace,
@@ -170,4 +171,5 @@ export {
170171
timeSince,
171172
first,
172173
mode,
174+
rollDice,
173175
}

src/rollDice.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default rollDice
2+
3+
/**
4+
* Original Source: https://stackoverflow.com/questions/15603217/random-dice-nr-javascript
5+
*
6+
* This method will return a random value from rolling a dice.
7+
*
8+
* @return {number} - Random number between 1 and 6
9+
*/
10+
11+
function rollDice() {
12+
return Math.floor(6 * Math.random()) + 1
13+
}

test/rollDice.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import test from 'ava'
2+
import { rollDice } from '../src'
3+
4+
test('Dice value should not be greater than 6', t => {
5+
const max = 6
6+
const diceValue = rollDice()
7+
t.false(diceValue > max)
8+
})
9+
10+
test('Dice value should not be less than 1', t => {
11+
const min = 1
12+
const diceValue = rollDice()
13+
t.false(diceValue < min)
14+
})

0 commit comments

Comments
 (0)