forked from hack4impact-calpoly/milestone-2-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmilestone2.ts
214 lines (187 loc) · 5.09 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
// 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) => {
let sum = 0;
for (let i=0; i<data.length; i++) {
sum = sum + data[i].age;
}
return sum / data.length;
};
//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) => {
let first = data.find(person => person.city === "SF");
if (first === undefined)
return -1;
return data.indexOf(first);
};
//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, minAge) => {
let cali = data.filter(person => person.state === "CA");
let ageThresh = cali.filter(p => p.age > minAge);
return ageThresh;
};
//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) => {
let fromSB = data.find(x => x.city === "SB");
return fromSB;
};
//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) => {
let volClone = {...vol};
return volClone;
};
// 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, updates) => {
let volNew = {...vol, ...updates};
return volNew;
};
// 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) => {
let {name, age, city} = vol; // Code here
return `${name} is ${age} years old and lives in ${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
*/
type DaBigTest = (data: Volunteer[]) => Volunteer | undefined;
let daBigTest: GetVolunteer = (data) => {
let fromSlo = data.find(x => x.city === "SLO" && x.age > 40);
if (fromSlo === undefined)
return undefined;
let firstClone = {...fromSlo};
firstClone.position = "staff";
return firstClone;
};
//console.log(daBigTest(userData));
export {
Volunteer,
findAverage,
findIndexAns,
findCAOverN,
findSBStaff,
copyVolunteer,
updateVolunteer,
getVolunteerInfo,
daBigTest,
};