Skip to content

Commit 634e6f9

Browse files
committed
Added medium/three_points
1 parent cc8bede commit 634e6f9

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

medium/three_points.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Using the JavaScript language, have the function threePoints(strArr) read the
3+
* array of strings stored in strArr which will always contain 3 elements and be
4+
* in the form: ["(x1,y1)", "(x2,y2)", "(x3,y3)"]. Your goal is to first create
5+
* a line formed by the first two points (that starts from the first point and
6+
* moves in the direction of the second point and that stretches in both
7+
* directions through the two points), and then determine what side of the line
8+
* point 3 is on. The result will either be right, left, or neither. For
9+
* example: if strArr is ["(1,1)", "(3,3)", "(2,0)"] then your program should
10+
* return the string right because the third point lies to the right of the line
11+
* formed by the first two points.
12+
*
13+
* https://www.coderbyte.com/results/bhanson:Three%20Points:JavaScript
14+
*
15+
* @param {array} strArr
16+
* @return {string} 'right', 'left', or 'neither'
17+
*/
18+
function threePoints(strArr) {
19+
// Parse input
20+
const [pointA, pointB, pointX] = strArr.map(point => {
21+
const [, x, y] = point.match(/\((-?[\d]+),(-?[\d]+)\)/).map(Number);
22+
return { x, y };
23+
});
24+
25+
// y = mx + b
26+
const slope = (pointB.y - pointA.y) / (pointB.x - pointA.x); // m
27+
const yIntercept = (pointA.y - slope) / pointA.x; // b
28+
29+
// x = (y - b) / m
30+
let x;
31+
if (slope === Infinity) {
32+
x = pointX.x;
33+
} else {
34+
x = pointX.y - yIntercept + slope;
35+
}
36+
37+
if (x === 0 || Number.isNaN(x)) {
38+
return 'neither';
39+
}
40+
41+
return x < 0 ? 'left' : 'right';
42+
}
43+
44+
module.exports = threePoints;

medium/three_points.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const threePoints = require('./three_points');
2+
3+
describe('threePoints()', () => {
4+
test('correctly returns side of point relative to line', () => {
5+
expect(threePoints(['(1,1)', '(3,3)', '(2,0)'])).toBe('right');
6+
7+
expect(threePoints(['(0,-3)', '(-2,0)', '(0,0)'])).toBe('right');
8+
9+
expect(threePoints(['(0,0)', '(0,5)', '(0,2)'])).toBe('neither');
10+
});
11+
12+
test('passes Coderbyte.com tests', () => {
13+
expect(threePoints(['(1,1)', '(3,3)', '(2,0)'])).toBe('right');
14+
15+
expect(threePoints(['(0,-3)', '(-2,0)', '(0,0)'])).toBe('right');
16+
17+
expect(threePoints(['(0,0)', '(0,5)', '(0,2)'])).toBe('neither');
18+
19+
expect(threePoints(['(0,0)', '(0,5)', '(-2,2)'])).toBe('left');
20+
21+
expect(threePoints(['(0,0)', '(0,100)', '(-200,5)'])).toBe('left');
22+
23+
expect(threePoints(['(-3,0)', '(0,1)', '(-1,0)'])).toBe('right');
24+
25+
expect(threePoints(['(0,1)', '(-3,0)', '(-1,0)'])).toBe('left');
26+
27+
expect(threePoints(['(0,5)', '(0,-5)', '(1,1)'])).toBe('left');
28+
29+
// TODO: Fix to pass this test
30+
expect(threePoints(['(100,100)', '(-1,-1)', '(5,1)'])).toBe('left');
31+
32+
expect(threePoints(['(5000,5001)', '(-5001,-5000)', '(0,601)'])).toBe(
33+
'right'
34+
);
35+
});
36+
});

0 commit comments

Comments
 (0)