Skip to content

Commit 61ec951

Browse files
committed
add contains method to NumberRange
1 parent 2932a1a commit 61ec951

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/number-range/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class NumberRange {
1010
get max() {
1111
return this._data.max;
1212
}
13+
14+
contains(n) {
15+
return n >= this.min && n <= this.max;
16+
}
1317
}
1418

1519
module.exports = { NumberRange };

src/number-range/index.test.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
const { NumberRange } = require('.');
22

33
describe('NumberRange', () => {
4-
it('should have a min and a max', () => {
5-
const min = 10;
6-
const max = 20;
4+
const min = 10;
5+
const max = 20;
76

7+
it('should have a min and a max', () => {
88
const range = new NumberRange(min, max);
99

1010
expect(range.min).toEqual(min);
1111
expect(range.max).toEqual(max);
1212
});
13+
14+
describe('contains', () => {
15+
it('should return true if a number falls inside of the specified range', () => {
16+
const range = new NumberRange(min, max);
17+
expect(range.contains(15)).toEqual(true);
18+
});
19+
20+
it('should return false if a number falls outside of the specified range', () => {
21+
const range = new NumberRange(min, max);
22+
expect(range.contains(9)).toEqual(false);
23+
expect(range.contains(21)).toEqual(false);
24+
});
25+
26+
it('should contain the lower end of the range', () => {
27+
const range = new NumberRange(min, max);
28+
expect(range.contains(min)).toEqual(true);
29+
});
30+
31+
it('should contain the upper end of the range', () => {
32+
const range = new NumberRange(min, max);
33+
expect(range.contains(max)).toEqual(true);
34+
});
35+
});
1336
});

0 commit comments

Comments
 (0)