-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoints.js
More file actions
45 lines (35 loc) · 949 Bytes
/
points.js
File metadata and controls
45 lines (35 loc) · 949 Bytes
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
const collinear = require("./maths").collinear;
const points = [];
exports.add = (req, res) => {
const point = req.body;
// check the input
if (
!point ||
isNaN(point.x) ||
isNaN(point.y) ||
Object.keys(point).length !== 2
) {
return res.send("invalid point!");
}
// check exists
if (points.some(p => p.x === point.x && p.y === point.y)) {
return res.send("point already exists!");
}
points.push(point);
res.send("point added succesfully!");
};
exports.space = (_, res) => {
res.json(points);
};
exports.lines = (req, res) => {
if (!req.params.n || isNaN(req.params.n) || req.params.n <= 1) {
return res.send("you must specify two or more points!");
}
if (points.length <= 1) {
return res.send("you need at least two points :-P");
}
if (req.params.n > points.length) {
return res.send("I'm sorry, too few points :-(");
}
res.json(collinear(points, req.params.n));
};