forked from hack4impact-calpoly/milestone-2-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
milestone2.ts
210 lines (188 loc) · 4.89 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
// 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 users in the data.
Hint: user data is stored in the userData object above.
*/
type GetNumber = (data: Volunteer[]) => number;
let findAverage: GetNumber = (data) => {
// Code here
var sum = 0
for (const person of data){
sum += (person.age)
}
return sum/data.length
};
console.log(findAverage(userData));
// Question 2: Data Handling
/* 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) => {
return data.findIndex(x => x.city == "SF")
}; // Code here
console.log(findIndexAns(userData));
// Question 3: Filtering data
/* Use data handling function(s) to find all of the volunteers from California (CA) over an age threshold n
*/
type GetVolunteers = (data: Volunteer[], minAge: number) => Volunteer[];
let findCAOverN: GetVolunteers = (data,min) => {
return data.filter(x => x.age > min && x.state == "CA")
} // Code here
console.log("FIND CA OVER N")
console.log(findCAOverN(userData, 25));
// Question 4: Searching Data
/* Use data handling function(s) to find the first staff member from Santa Barbara (SB)
*/
type GetVolunteer = (data: Volunteer[]) => Volunteer | undefined;
let findSBStaff: GetVolunteer = (data) => {
return data.find(x => x.city == "SB")
} // Code here
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 = (data) => {
return {...data}
}; // 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
*/
let updatedLocation = { city: "Seattle", state: "WA" };
let kyleNew: Volunteer;
type UpdateVolunteer = (
vol: Volunteer,
updates: Partial<Volunteer>
) => Volunteer;
let updateVolunteer: UpdateVolunteer = (data,update) =>{
return {...data, ...update}
} // Code here
kyleNew = updateVolunteer(kyleClone,updatedLocation);
console.log("AAH")
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; // Code here
name = vol.name
age = vol.age
city = vol.city
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 user 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) => {
let firstUser : Volunteer = findCAOverN(data,40).filter(x => x.city == "SLO")[0]
if (firstUser){
return updateVolunteer(firstUser,{position: "staff"})
}
else{
return undefined
}
} // Code here
console.log(daBigTest(userData));
export {
Volunteer,
findAverage,
findIndexAns,
findCAOverN,
findSBStaff,
copyVolunteer,
updateVolunteer,
getVolunteerInfo,
daBigTest,
};