Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c9249d3

Browse files
chore(binary-search): consolidate test for multiple implementations
1 parent 5b49d8a commit c9249d3

File tree

2 files changed

+41
-57
lines changed

2 files changed

+41
-57
lines changed

‎src/runtimes/02-binary-search.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ function binarySearchRecursive(array, search, offset = 0) {
1818

1919
if (current === search) {
2020
return offset + half;
21-
} if (array.length === 1) {
21+
}
22+
if (array.length === 1) {
2223
return -1;
23-
} if (search > current) {
24+
}
25+
if (search > current) {
2426
const right = array.slice(half);
2527
return binarySearchRecursive(right, search, offset + half);
2628
}

‎src/runtimes/02-binary-search.spec.js

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,39 @@
1-
const { binarySearchRecursive, binarySearchIterative } = require('./02-binary-search');
2-
3-
describe('Binary Search Recursive', () => {
4-
let array;
5-
6-
beforeEach(() => {
7-
array = [7, 9, 13, 23];
8-
});
9-
10-
it('should find a middle element', () => {
11-
expect(binarySearchRecursive(array, 9)).toEqual(1);
12-
});
13-
14-
it('should find an first element', () => {
15-
expect(binarySearchRecursive(array, 7)).toEqual(0);
16-
});
17-
18-
it('should find the last element', () => {
19-
expect(binarySearchRecursive(array, 23)).toEqual(3);
20-
});
21-
22-
it('should not find an bigger element', () => {
23-
expect(binarySearchRecursive(array, 9000)).toEqual(-1);
24-
});
25-
26-
it('should find a smaller element', () => {
27-
expect(binarySearchRecursive(array, -9)).toEqual(-1);
28-
});
29-
});
30-
31-
describe('Binary Search Iterative', () => {
32-
let array;
33-
34-
beforeEach(() => {
35-
array = [7, 9, 13, 23];
36-
});
37-
38-
it('should find a middle element', () => {
39-
expect(binarySearchIterative(array, 9)).toEqual(1);
40-
});
41-
42-
it('should find an first element', () => {
43-
expect(binarySearchIterative(array, 7)).toEqual(0);
44-
});
45-
46-
it('should find the last element', () => {
47-
expect(binarySearchIterative(array, 23)).toEqual(3);
48-
});
49-
50-
it('should not find an bigger element', () => {
51-
expect(binarySearchIterative(array, 9000)).toEqual(-1);
52-
});
53-
54-
it('should find a smaller element', () => {
55-
expect(binarySearchIterative(array, -9)).toEqual(-1);
1+
const {
2+
binarySearchRecursive,
3+
binarySearchIterative,
4+
} = require("./02-binary-search");
5+
6+
const binarySearchImplementations = [
7+
binarySearchRecursive,
8+
binarySearchIterative,
9+
];
10+
11+
binarySearchImplementations.forEach((binarySearchImpl) => {
12+
describe(binarySearchImpl.name, () => {
13+
let array;
14+
15+
beforeEach(() => {
16+
array = [7, 9, 13, 23];
17+
});
18+
19+
it("should find a middle element", () => {
20+
expect(binarySearchImpl(array, 9)).toEqual(1);
21+
});
22+
23+
it("should find an first element", () => {
24+
expect(binarySearchImpl(array, 7)).toEqual(0);
25+
});
26+
27+
it("should find the last element", () => {
28+
expect(binarySearchImpl(array, 23)).toEqual(3);
29+
});
30+
31+
it("should not find an bigger element", () => {
32+
expect(binarySearchImpl(array, 9000)).toEqual(-1);
33+
});
34+
35+
it("should find a smaller element", () => {
36+
expect(binarySearchImpl(array, -9)).toEqual(-1);
37+
});
5638
});
5739
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /