-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcontroller.js
43 lines (36 loc) · 1.08 KB
/
controller.js
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
import trailData from '../trail-data';
export default {
get: function(req, response) {
let results = trailData;
if (req.query){
results = maybeFilterBySport(results, req.query.sport);
results = maybeFilterByHills(results, req.query.hills);
// SOLUTION!
results = maybeFilterByDistance(results, req.query.distance);
}
response.json(results);
},
};
function maybeFilterBySport(results, querySport) {
//... no changes
}
function maybeFilterByHills(results, queryHills) {
//... no changes
}
// SOLUTION FROM HERE TO END!
function maybeFilterByDistance(results, queryDistance) {
if (queryDistance) {
const { min, max } = getMinMaxDistance(queryDistance);
return results.filter(x => x.distance <= max && x.distance >= min);
}
return results;
}
function getMinMaxDistance(queryDistance) {
if (queryDistance < 10) {
return { min: queryDistance - 1, max: queryDistance + 1 };
}
if (queryDistance < 20) {
return { min: queryDistance - 2, max: queryDistance + 2 };
}
return { min: queryDistance - 3, max: queryDistance + 3 };
}