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

Commit 578e50e

Browse files
mihhuKent C. Dodds
authored andcommitted
feat(median): Add median function (#141)
* feat(median): Add median function Add a function to find the median value of a numeric array. * test(median): Add tests for median
1 parent f269f88 commit 578e50e

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/array-median.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export default median
2+
3+
/**
4+
* Original source: https://stackoverflow.com/a/29544442/4573129
5+
* This method will return the median value of an array
6+
* @param {Array} array - the array to find the median
7+
* @return {Number} - the median of the values in the array
8+
*/
9+
10+
function median(array) {
11+
if (!Array.isArray(array)) {
12+
return `${array} is not an array.`
13+
}
14+
if (array.some(isNaN)) {
15+
return `${array} contains non-numeric items.`
16+
}
17+
if (array.length === 0) {
18+
return `${array} has no items.`
19+
}
20+
21+
let medianValue = 0
22+
const sortedArray = array.sort((curr, next) => (curr - next))
23+
const index = Math.floor(sortedArray.length / 2)
24+
25+
if (sortedArray.length % 2 === 0) {
26+
medianValue = (sortedArray[index - 1] + sortedArray[index]) / 2
27+
} else {
28+
medianValue = sortedArray[index]
29+
}
30+
31+
return medianValue
32+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import contains from './contains'
4848
import getOrdinalSuffix from './get-ordinal-suffix'
4949
import arrayAverage from './array-average'
5050
import find from './find'
51+
import median from './array-median'
5152

5253
export {
5354
isOdd,
@@ -100,4 +101,5 @@ export {
100101
contains,
101102
getOrdinalSuffix,
102103
arrayAverage,
104+
median,
103105
}

test/array-median.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import test from 'ava'
2+
import {median} from '../src'
3+
4+
test('accepts only typeof array param', t => {
5+
const array = {}
6+
const expected = `${array} is not an array.`
7+
const actual = median(array)
8+
t.deepEqual(actual, expected)
9+
})
10+
11+
test('accepts only arrays with numeric items', t => {
12+
const array = ['hello', 'world', 5, false]
13+
const expected = `${array} contains non-numeric items.`
14+
const actual = median(array)
15+
t.deepEqual(actual, expected)
16+
})
17+
18+
test('accepts only nonempty arrays', t => {
19+
const array = []
20+
const expected = `${array} has no items.`
21+
const actual = median(array)
22+
t.deepEqual(actual, expected)
23+
})
24+
25+
test('finds the median of an array with odd number of items', t => {
26+
const array = [13, 2, 5]
27+
const expected = 5
28+
const actual = median(array)
29+
t.deepEqual(actual, expected)
30+
})
31+
32+
test('finds the median of an array with even number of items', t => {
33+
const array = [9, 25, 4, 1]
34+
const expected = 6.5
35+
const actual = median(array)
36+
t.deepEqual(actual, expected)
37+
})

0 commit comments

Comments
 (0)