Added Midpoint.js

This commit is contained in:
Gary Baker
2018-11-15 08:49:04 -07:00
parent 9bdee93472
commit 480eb7fc86
3 changed files with 60 additions and 0 deletions

24
snippets/midpoint.md Normal file
View File

@ -0,0 +1,24 @@
### midpoint
Calculates the midpoint between two pairs of x and y points.
Takes in two pairs of (x,y) points, calculates the midpoint between the x's, the midpoint between the y's, and returns the coordinates in an array.
```js
const midpoint = (x1, y1, x2, y2) => {
let points = new Array(2);
let first_point = (x1 + x2) / 2;
let second_point = (y1 + y2) / 2;
points[0] = first_point;
points[1] = second_point;
return points;
};
```
```js
midpoint(2, 2, 4, 4); // [3, 3]
midpoint(4, 4, 6, 6); // [5, 5]
midpoint(1, 3, 2, 4); // [1.5, 3.5]
```

View File

@ -186,6 +186,7 @@ maxN:array,math,beginner
median:math,array,intermediate
memoize:function,advanced
merge:object,array,intermediate
midpoint:math,array,beginner
minBy:math,array,function,beginner
minDate:date,math,beginner
minN:array,math,beginner

35
test/midpoint.test.js Normal file
View File

@ -0,0 +1,35 @@
const expect = require('expect');
const {midpoint} = require('./_30s.js');
test('midpoint is a Function', () => {
expect(midpoint).toBeInstanceOf(Function);
});
test('midpoint(2, 2, 4, 4) = [3, 3]', () => {
expect(midpoint(2, 2, 4, 4)).toEqual([3, 3])
});
test('midpoint(4, 4, 6, 6) = [5,5]', () => {
expect(midpoint(4, 4, 6, 6)).toEqual([5, 5])
});
test('midpoint(1, 3, 2, 4) = [3,3]', () => {
expect(midpoint(1, 3, 2, 4)).toEqual([1.5, 3.5])
});
test('midpoint(-2, 0, -2, 4) = [-2,2]', () => {
expect(midpoint(-2, 0, -2, 4)).toEqual([-2, 2])
});
test('midpoint(0, 0, 0, 0) = [0,0]', () => {
expect(midpoint(0, 0, 0, 0)).toEqual([0, 0])
});
test('midpoint(10, -10, 20, 30) = [15, 10]', () => {
expect(midpoint(10, -10, 20, 30)).toEqual([15, 10])
});
test('midpoint(7.5, -1.3, 2.7, 11.1) = [5.1, 4.8999999999999995]', () => {
expect(midpoint(7.5, -1.3, 2.7, 11.1)).toEqual([5.1, 4.8999999999999995])
});