forked from hack4impact-calpoly/milestone-2-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
milestone2.ts
226 lines (201 loc) · 5.92 KB
/
milestone2.ts
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// This is the data we'll be working with!
interface Volunteer {
name: string;
age: number;
email: string;
position: "volunteer" | "staff";
city: string;
state: string;
}
const userData: Volunteer[] = [
{
name: "Joe",
age: 30,
email: "[email protected]",
position: "volunteer",
city: "LA",
state: "CA",
},
{
name: "Jenny",
age: 24,
email: "[email protected]",
position: "volunteer",
city: "SLO",
state: "CA",
},
{
name: "Jeff",
age: 40,
email: "[email protected]",
position: "volunteer",
city: "SF",
state: "CA",
},
{
name: "Julie",
age: 34,
email: "[email protected]",
position: "volunteer",
city: "SLO",
state: "CA",
},
{
name: "Lucy",
age: 21,
email: "[email protected]",
position: "volunteer",
city: "SB",
state: "CA",
},
{
name: "Lee",
age: 27,
email: "[email protected]",
position: "volunteer",
city: "SLO",
state: "CA",
},
{
name: "Leander",
age: 29,
email: "[email protected]",
position: "staff",
city: "SB",
state: "CA",
},
{
name: "Luna",
age: 66,
email: "[email protected]",
position: "volunteer",
city: "SF",
state: "CA",
},
];
// Questions Start Here
// Question 1: Lambda Functions
/* Define a new lambda function that finds the average age of the people in the data.
Hint: user data is stored in the userData object above.
*/
type GetNumber = (data: Volunteer[]) => number;
let findAverage: GetNumber = (data: Volunteer[]) => {
// Sum all age properties of volunteers in inputted array
let running_age_sum = 0;
for(let volunteer of data)
{
running_age_sum += volunteer.age;
}
return running_age_sum / data.length;
} // Define lambda function here
//console.log(findAverage(userData));
// Question 2: Data Handling
// Please AVOID USING FOR LOOPS for questions 2 through 4
/* Use data handling function(s) to find the first index of someone from San Francisco (SF).
Return -1 if no one is from San Francisco.
Hint: Use a lambda function as a value
*/
let findIndexAns: GetNumber = (data: Volunteer[]) => {
const cities: string[] = data.map(volunteer => volunteer.city)
// Note to self: if brackets are present in body of lambda method, a return statement
// is necessary.
return cities.findIndex(city => city === "SF");
} // Code here
// console.log(findIndexAns(userData));
// Question 3: Filtering data
/* Use data handling function(s) to find all of the people from California (CA) over an age threshold n
*/
type GetVolunteers = (data: Volunteer[], minAge: number) => Volunteer[];
let findCAOverN: GetVolunteers = (data: Volunteer[], minAge: number) => {
// Filter inputted array for objects with age > 40 and state == "CA"
return data.filter(v => v.age > minAge && v.state == "CA" );
}
//console.log(findCAOverN(userData, 25));
// Question 4: Searching Data
/* Use data handling function(s) to find the first person from Santa Barbara (SB)
*/
type GetVolunteer = (data: Volunteer[]) => Volunteer | undefined;
let findSBStaff: GetVolunteer = (data: Volunteer[]) => {
return data.find(x => x.city == "SB");
}
//console.log(findSBStaff(userData));
// Question 5: Spread Operator Part 1
let kyle: Volunteer = {
name: "Kyle",
age: 18,
email: "[email protected]",
position: "volunteer",
city: "SD",
state: "CA",
};
/* Lets make a clone of Kyle above using the spread operator and assign it to kyleClone
*/
type CopyVolunteer = (vol: Volunteer) => Volunteer;
let copyVolunteer: CopyVolunteer = (vol: Volunteer) => {
// Use the spread operator to return an (associative) array of key-value pairs,
// then immediately build an object based on those key-value pairs without
// making any edits to properties
return {...vol};
} // Code here
//let kyleClone: Volunteer = copyVolunteer(kyle);
//console.log(kyleClone);
// Question 6: Spread Operator Part 2
/* Next, lets use the spread operator to update your kyleClone object with the updatedLocation defined below and assign it to kyleNew
*/
type UpdateVolunteer = (
vol: Volunteer,
updates: Partial<Volunteer>
) => Volunteer;
let updateVolunteer: UpdateVolunteer = (
vol: Volunteer,
updates: Partial<Volunteer>) => {
// Return a new object with the old object's key-value pairs,
// but with the key-value pairs defined in 'updates' overwriting any old value
return {
...vol,
...updates
// Note to self: "the last definition of a property wins"
}
}// Code here
//let updatedLocation = { city: "Seattle", state: "WA" };
//let kyleNew = updateVolunteer(kyleClone, updatedLocation);
//console.log(kyleNew);
// Question 7: Object Destructuring
/* Now that we have our updated kyle, lets use object destructuring to get his name, age, and city
*/
type GetVolunteerInfo = (vol: Volunteer) => String;
let getVolunteerInfo: GetVolunteerInfo = (vol: Volunteer) => {
return `${vol.name} is ${vol.age} years old and lives in ${vol.city}`;
};
//let kyleInfo = getVolunteerInfo(kyleNew);
//console.log(kyleInfo);
// Question 8: Putting it All Together!
/* Use all the skills we've covered today to get the *first* person from
/* San Luis Obispo (SLO) over the age of 40, and return an updated
/* version of them with their position set to staff. If no one meets
/* these criteria, return undefined
*/
let daBigTest: GetVolunteer = (data: Volunteer[]) => {
// Return the first object that matches the criteria described above
let foundVolunteer: Volunteer | undefined = data.find(x => x.city == "SLO" && x.age > 40);
// If an object was actually found, overwrite position: "{old value}" with
// position: "staff"
if(foundVolunteer != undefined)
foundVolunteer = {
...foundVolunteer,
position: "staff"
};
return foundVolunteer;
}// Code here
//console.log(daBigTest(userData));
export {
Volunteer,
findAverage,
findIndexAns,
findCAOverN,
findSBStaff,
copyVolunteer,
updateVolunteer,
getVolunteerInfo,
daBigTest,
};