Skip to content

Commit f625d93

Browse files
committedNov 26, 2019
add examples

File tree

5 files changed

+1006
-840
lines changed

5 files changed

+1006
-840
lines changed
 

‎package-lock.json

Lines changed: 941 additions & 839 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/algorithms/get-min.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Get the smallest number on an array of numbers
3+
* @param {Array} n array of numbers
4+
*/
5+
function getMin(n = []) {
6+
let min = n[0];
7+
8+
n.forEach(element => {
9+
if(element < min) {
10+
min = element;
11+
}
12+
});
13+
return min;
14+
}
15+
16+
module.exports = getMin;

‎src/algorithms/get-min.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const getMin = require('./get-min');
2+
3+
describe('find smallest number', () => {
4+
it('should get the min number of an array', () => {
5+
expect(getMin([3, 10, 2])).toEqual(2);
6+
});
7+
8+
it('should get the min number of an array with negatives', () => {
9+
expect(getMin([3, 10, -2])).toEqual(-2);
10+
});
11+
12+
it('should work with 0', () => {
13+
expect(getMin([3, 0, 2])).toEqual(0);
14+
});
15+
16+
it('should work with empty', () => {
17+
expect(getMin([])).toEqual(undefined);
18+
});
19+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function binarySearchRecursive(array, search, offset = 0) {
1818

1919
if (current === search) {
2020
return offset + half;
21-
} if (array.length < 2) {
21+
} if (array.length === 1) {
2222
return -1;
2323
} if (search > current) {
2424
const right = array.slice(half);

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const binarySearch = require('./02-binary-search').binarySearchRecursive;
2+
3+
describe('Binary Search', () => {
4+
let array;
5+
6+
beforeEach(() => {
7+
array = [7, 9, 13, 23];
8+
});
9+
10+
it('should find a middle element', () => {
11+
expect(binarySearch(array, 9)).toEqual(1);
12+
});
13+
14+
it('should find an first element', () => {
15+
expect(binarySearch(array, 7)).toEqual(0);
16+
});
17+
18+
it('should find the last element', () => {
19+
expect(binarySearch(array, 23)).toEqual(3);
20+
});
21+
22+
it('should not find an bigger element', () => {
23+
expect(binarySearch(array, 9000)).toEqual(-1);
24+
});
25+
26+
it('should find a smaller element', () => {
27+
expect(binarySearch(array, -9)).toEqual(-1);
28+
});
29+
});

0 commit comments

Comments
 (0)
Please sign in to comment.