forked from hack4impact-calpoly/milestone-2-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
milestone2.js
161 lines (161 loc) · 5.19 KB
/
milestone2.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
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
"use strict";
// This is the data we'll be working with!
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
exports.__esModule = true;
exports.daBigTest = exports.getVolunteerInfo = exports.updateVolunteer = exports.copyVolunteer = exports.findSBStaff = exports.findCAOverN = exports.findIndexAns = exports.findAverage = void 0;
var userData = [
{
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"
},
];
var findAverage = function (data) {
// Sum all age properties of volunteers in inputted array
var running_age_sum = 0;
for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
var volunteer = data_1[_i];
running_age_sum += volunteer.age;
}
return running_age_sum / data.length;
}; // Define lambda function here
exports.findAverage = findAverage;
//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
*/
var findIndexAns = function (data) {
var cities = data.map(function (volunteer) { return volunteer.city; });
// Note to self: if brackets are present in body of lambda method, a return statement
// is necessary.
return cities.findIndex(function (city) { return city === "SF"; });
}; // Code here
exports.findIndexAns = findIndexAns;
var findCAOverN = function (data, minAge) {
// Filter inputted array for objects with age > 40 and state == "CA"
return data.filter(function (v) { return v.age > minAge && v.state == "CA"; });
};
exports.findCAOverN = findCAOverN;
var findSBStaff = function (data) {
return data.find(function (x) { return x.city == "SB"; });
};
exports.findSBStaff = findSBStaff;
//console.log(findSBStaff(userData));
// Question 5: Spread Operator Part 1
var kyle = {
name: "Kyle",
age: 18,
email: "[email protected]",
position: "volunteer",
city: "SD",
state: "CA"
};
var copyVolunteer = function (vol) {
// 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 __assign({}, vol);
}; // Code here
exports.copyVolunteer = copyVolunteer;
var updateVolunteer = function (vol, updates) {
// 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 __assign(__assign({}, vol), updates
// Note to self: "the last definition of a property wins"
);
}; // Code here
exports.updateVolunteer = updateVolunteer;
var getVolunteerInfo = function (vol) {
return "".concat(vol.name, " is ").concat(vol.age, " years old and lives in ").concat(vol.city);
};
exports.getVolunteerInfo = getVolunteerInfo;
//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
*/
var daBigTest = function (data) {
// Return the first object that matches the criteria described above
var foundVolunteer = data.find(function (x) { return x.city == "SLO" && x.age > 40; });
// If an object was actually found, overwrite position: "{old value}" with
// position: "staff"
if (foundVolunteer != undefined)
foundVolunteer = __assign(__assign({}, foundVolunteer), { position: "staff" });
return foundVolunteer;
}; // Code here
exports.daBigTest = daBigTest;