-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (58 loc) · 1.05 KB
/
index.js
File metadata and controls
66 lines (58 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// @ts-check
import {
cons,
car,
cdr,
toString as toStr,
} from '@hexlet/pairs';
/**
* Make a point
* @example
* const point = makePoint(4, 5);
*/
export const makePoint = (x, y) => cons(x, y);
/**
* Get X
* @example
* const point = makePoint(4, 5);
* getX(point); // 4
*/
export const getX = (point) => car(point);
/**
* Get Y
* @example
* const point = makePoint(8, -2);
* getY(point); // -2
*/
export const getY = (point) => cdr(point);
/**
* Convert point to string
* @example
* const point = makePoint(0, 10);
* toString(point); // (0, 10)
*/
export const toString = (point) => toStr(point);
/**
* Determine quadrant for given point
* @example
* quadrant(makePoint(5, 0)); // null
* quadrant(makePoint(1, 5)); // 1
* quadrant(makePoint(-3, 10)); // 2
*/
export const quadrant = (point) => {
const x = getX(point);
const y = getY(point);
if (x > 0 && y > 0) {
return 1;
}
if (x < 0 && y > 0) {
return 2;
}
if (x < 0 && y < 0) {
return 3;
}
if (x > 0 && y < 0) {
return 4;
}
return null;
};