-
Notifications
You must be signed in to change notification settings - Fork 10
/
bicycleFinder.js
115 lines (113 loc) · 2.71 KB
/
bicycleFinder.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var bicycleFinder = (function() {
"use strict";
var getBicycles = function() {
return [{
id: 1,
name: "A fast bike",
type: "Road Bike",
quantity: 10,
rentPrice: 20,
dateAdded: new Date(2015, 1, 2)
}, {
id: 2,
name: "An even faster bike",
type: "Road Bike",
quantity: 4,
rentPrice: 25,
dateAdded: new Date(2015, 2, 25)
}, {
id: 3,
name: "A springy bike",
type: "Mountain Bike",
quantity: 20,
rentPrice: 18,
dateAdded: new Date(2014, 10, 1)
}, {
id: 4,
name: "A springier bike",
type: "Mountain Bike",
quantity: 10,
rentPrice: 22,
dateAdded: new Date(2014, 4, 1)
}, {
id: 5,
name: "An all-terain bike",
type: "Mountain Bike",
quantity: 5,
rentPrice: 27,
dateAdded: new Date(2014, 8, 14)
}, {
id: 6,
name: "A classy bike",
type: "Urban Bike",
quantity: 30,
rentPrice: 15,
dateAdded: new Date(2014, 6, 27)
}, {
id: 7,
name: "A modern bike",
type: "Urban Bike",
quantity: 20,
rentPrice: 17,
dateAdded: new Date(2015, 1, 19)
}, {
id: 8,
name: "A commuter bike",
type: "Urban Bike",
quantity: 12,
rentPrice: 14,
dateAdded: new Date(2014, 8, 2)
}, {
id: 9,
name: "A blue bike",
type: "Children Bike",
quantity: 25,
rentPrice: 10,
dateAdded: new Date(2014, 10, 20)
}, {
id: 10,
name: "A pink bike",
type: "Children Bike",
quantity: 25,
rentPrice: 10,
dateAdded: new Date(2015, 2, 5)
}, {
id: 11,
name: "A noisy bike",
type: "Children Bike",
quantity: 3,
rentPrice: 12,
dateAdded: new Date(2014, 8, 23)
}, {
id: 12,
name: "A clown bike",
type: "Children Bike",
quantity: 2,
rentPrice: 12,
dateAdded: new Date(2014, 11, 1)
}];
};
return {
findBicycle: function(type, maxRentPrice) {
var bicycles = getBicycles();
return _.find(bicycles, function(bicycle) {
return bicycle.type === type && bicycle.rentPrice <= maxRentPrice;
});
},
findBicycleById: function(id){
var bicycles = getBicycles();
return _.findWhere(bicycles, {id: id});
},
hasBicycle: function(type, maxRentPrice) {
var bicycles = getBicycles();
return _.some(bicycles, function(bicycle) {
return bicycle.type === type && bicycle.rentPrice <= maxRentPrice;
});
},
hasBicycleWithId: function(id) {
var bicycles = getBicycles();
var bicycleIds = _.pluck(bicycles,"id");
return _.contains(bicycleIds, id);
}
};
}());