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

Commit 6898a39

Browse files
tbarber350Kent C. Dodds
authored andcommitted
feat(mod): Add mod function (#86) (#87)
Closes #86
1 parent 659d8ee commit 6898a39

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import dec2bin from './dec2bin'
2727
import searchAndReplace from './search-and-replace'
2828
import sqrt from './sqrt'
2929
import toPower from './to-power'
30+
import mod from './mod'
3031

3132
export {
3233
initArray,
@@ -57,4 +58,5 @@ export {
5758
searchAndReplace,
5859
sqrt,
5960
toPower,
61+
mod,
6062
}

src/mod.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default mod
2+
3+
/**
4+
* Original Source: http://stackoverflow.com/a/17323608
5+
*
6+
* This method provides a modulo function that will always return a positive
7+
* value even when supplied a negative dividend.
8+
*
9+
* @param {Number} dividend - the number to be divided by divisor
10+
* @param {Number} divisor - the number divided by
11+
* @returns {Number} - the remainder
12+
*/
13+
14+
function mod(dividend, divisor) {
15+
return ((dividend % divisor) + divisor) % divisor
16+
}

test/mod.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import test from 'ava'
2+
import {mod} from '../src'
3+
4+
test('a dividend larger than divisor', t => {
5+
const dividend = 12
6+
const divisor = 8
7+
const expected = 4
8+
const result = mod(dividend, divisor)
9+
t.is(expected, result)
10+
})
11+
12+
test('a negative dividend', t => {
13+
const dividend = -12
14+
const divisor = 8
15+
const expected = 4
16+
const result = mod(dividend, divisor)
17+
t.is(expected, result)
18+
})

0 commit comments

Comments
 (0)