Skip to content

Commit 35370f3

Browse files
authored
Merge pull request #66 from variadicjs/range
add range
2 parents 0b510de + 5bb7214 commit 35370f3

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Computability:
102102
- [X] Factoral
103103
- [X] Minimum
104104
- [X] Maximum
105-
- [ ] Range
105+
- [X] Range
106106
- [X] Average
107107
- [X] Mode
108108
- [X] Median

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = Object.assign(
2222
require('./lib/median.js'),
2323
require('./lib/minimum.js'),
2424
require('./lib/mode.js'),
25+
require('./lib/range.js'),
2526
require('./lib/populationStandardDeviation.js'),
2627
require('./lib/populationVariance.js'),
2728
require('./lib/sampleStandardDeviation.js'),

lib/range.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// JS can only safely represent and compare integers
2+
// between Number.MAX_SAFE_INTEGER and Number.MAX_SAFE_INTEGER
3+
const testMaxMinSafe = (params) => {
4+
if (params.some(param => param > Number.MAX_SAFE_INTEGER || param < Number.MIN_SAFE_INTEGER)) {
5+
throw new Error('Cannot reliably find the range of numbers larger than 9,007,199,254,740,991 or smaller than -9,007,199,254,740,991');
6+
}
7+
};
8+
9+
const handleErrors = (params) => {
10+
if (params.length === 0) throw new Error('Must provide one or more paramters');
11+
if (params.some(param => typeof param !== 'number')) {
12+
throw new Error('One of your parameters does not evaluate to a number');
13+
}
14+
testMaxMinSafe(params);
15+
};
16+
17+
/**
18+
* This function returns the difference between the largest and smallest values
19+
* @memberof variadic
20+
* @author tdnelson2
21+
* @param {...*} params - One or more parameters.
22+
*/
23+
exports.range = (...params) => {
24+
handleErrors(params);
25+
26+
// Exit early if there is only one value
27+
if (params.length === 1) return 0;
28+
29+
// Assign first value in the param list to the `high` and `low` variables
30+
let [high, low] = [params[0], params[0]];
31+
const [, ...nums] = params;
32+
33+
// Loop through the rest of the parameters and compare/update `high` and `low`
34+
for (const num of nums) {
35+
if (num > high) { high = num; } else if (num < low) { low = num; }
36+
}
37+
38+
const result = high - low;
39+
testMaxMinSafe([result]);
40+
return result;
41+
};

spec/rangeSpec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const { range } = require('../lib/range');
2+
3+
describe('range', () => {
4+
it('should return either 0 or a positive number', () => {
5+
const result = range(-296.028, 296.028, 42, +'-42');
6+
expect(result >= 0).toBe(true);
7+
});
8+
9+
it('should return the difference between the largest and smallest values', () => {
10+
const result = range(1.6, -2.3325, 596960, 3938475.31589, +'-395757.326518');
11+
expect(result).toEqual(4334232.642408);
12+
});
13+
14+
it('should return 0 when all there numbers are identical', () => {
15+
const result = range(296.028, 296.028, 296.028, 296.028);
16+
expect(result).toEqual(0);
17+
});
18+
19+
it('should return 0 when only one number is provided', () => {
20+
const result = range(296.028);
21+
expect(result).toEqual(0);
22+
});
23+
24+
it('should throw an error when no parameters are provided', () => {
25+
expect(range).toThrow();
26+
});
27+
28+
it('should throw an error when at least one parameter does not evaluate to a number', () => {
29+
expect(() => range(42, 'foo', 'bar')).toThrow();
30+
});
31+
32+
it('should throw an error when at least one parameter evaluates to a number larger than Number.MAX_SAFE_INTEGER', () => {
33+
expect(() => range(Number.MAX_SAFE_INTEGER + 1)).toThrow();
34+
});
35+
36+
it('should throw an error when at least one parameter evaluates to a number smaller than Number.MIN_SAFE_INTEGER', () => {
37+
expect(() => range(Number.MIN_SAFE_INTEGER - 1)).toThrow();
38+
});
39+
40+
it('should throw an error when result is number larger than Number.MAX_SAFE_INTEGER', () => {
41+
expect(() => range(1.6, -2.3325, 9007199254740990, 596960, -3938475)).toThrow();
42+
});
43+
44+
it('should throw an error when result is number smaller than Number.MIN_SAFE_INTEGER', () => {
45+
expect(() => range(1.6, -2.3325, -9007199254740990, 596960, 3938475)).toThrow();
46+
});
47+
});

0 commit comments

Comments
 (0)