-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectAlgos2.js
More file actions
322 lines (251 loc) · 7.47 KB
/
objectAlgos2.js
File metadata and controls
322 lines (251 loc) · 7.47 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
Michael Remy, Micahel Cote, Anthony Yi, and Petra Franklin.
// Michael Remy
// Given an array of objects representing people
// and a string representing a bad habit
// return a "santasNaughtyList" containing the first and last names of all the people
// who have the matching bad habit so that santa knows how much coal he needs.
// you can solve this iteratively or using functional programming. if you finish early, try
// implementing it both ways!
const people = [
{
firstName: "FN1",
lastName: "LN1",
habits: [
"doesn't wash dishes",
"falls asleep in lecture",
"shows up early",
],
},
{
firstName: "FN2",
lastName: "LN2",
habits: ["shows up late", "washes dishes", "helps peers"],
},
{
firstName: "FN3",
lastName: "LN3",
habits: ["doesn't wash dishes", "hoards snacks", "shows up late"],
},
{
firstName: "FN4",
lastName: "LN4",
habits: ["shows up early", "helps peers", "washes dishes"],
},
];
// Input: people, "doesn't wash dishes"
// Output: ["FN1 LN1", "FN3 LN3"]
// Input: people, "shows up late"
// Output: ["FN2 LN2", "FN3 LN3"]
// Input: people, "vapes too much"
// Output: []
function naughtyList(people, habit) {
let output = []
people.forEach((students) => {
students.habits.forEach((badHabit) => {
if (habit == badHabit) {
output.push(students.firstName + " " + students.lastName);
}
});
})
return output;
}
console.log(naughtyList(people, "shows up late"));
// function naughtyList2(people, habit) {
// let output =[];
// for (var i = 0; i < people.length; i++) {
// if (people[i].habits.includes(habit)) {
// output.push(people[i].firstName + " " + people[i].lastName);
// console.log(people[i].firstName);
// }
// }
// return output;
// }
// console.log(naughtyList2(people, "shows up late"));
// [ 'FN2,LN2', 'FN3,LN3' ]
/*
Given an array of person objects with the following keys:
firstName[string]
lastName[string]
friends[arr of friend objects]
isSocialDistancing[bool]
Friend object keys:
firstName[string]
lastName[string]
isSocialDistancing[bool]
isInfected[bool]
return a new array of the names of people who are at high risk of infection
A person is at high risk of catching the virus if they are:
1. not practicing social distancing
2. have a friend who is not practicing social distancing whom is infected
Bonus: after solving it, make a 2nd solution to practice functional programming with built in methods
*/
// Input:
const people = [
{
firstName: "Person",
lastName: "One",
isSocialDistancing: false,
friends: [friend2, friend3],
},
{
firstName: "Person",
lastName: "Two",
isSocialDistancing: true,
friends: [friend2, friend1],
},
{
firstName: "Person",
lastName: "Three",
isSocialDistancing: false,
friends: [friend2, friend1],
},
];
const friend1 = {
firstName: "Friend",
lastName: "One",
isSocialDistancing: false,
isInfected: true,
};
const friend2 = {
firstName: "Friend",
lastName: "Two",
isSocialDistancing: false,
isInfected: true,
};
const friend3 = {
firstName: "Friend",
lastName: "Three",
isSocialDistancing: false,
isInfected: false,
};
// Output: ["Person One", "Person Three"]
Mapping through to identify who is not social distancing
then mapping through to see who has a friend who is infected.
function highRisk(people) {
let output = [];
people.forEach((person) => {
if (person.isSocialDistancing === true) {
break;
}
else{
output.push(people[i].firstName + " " + people[i].lastName);
}
})
}
// ------------------------------------------------------------------------------------------------------------------------------------------------------------
// Given an array of objects representing people
// and a string representing a bad habit
// return a "santasNaughtyList" containing the first and last names of all the people
// who have the matching bad habit so that santa knows how much coal he needs.
// you can solve this iteratively or using functional programming. if you finish early, try
// implementing it both ways!
const students = [
{
firstName: "FN1",
lastName: "LN1",
habits: [
"doesn't wash dishes",
"falls asleep in lecture",
"shows up early",
],
},
{
firstName: "FN2",
lastName: "LN2",
habits: ["shows up late", "washes dishes", "helps peers"],
},
{
firstName: "FN3",
lastName: "LN3",
habits: ["doesn't wash dishes", "hoards snacks", "shows up late"],
},
{
firstName: "FN4",
lastName: "LN4",
habits: ["shows up early", "helps peers", "washes dishes"],
},
];
// Input: students, "doesn't wash dishes"
// Output: ["FN1 LN1", "FN3 LN3"]
// Input: students, "shows up late"
// Output: ["FN2 LN2", "FN3 LN3"]
// Input: students, "vapes too much"
// Output: []
// naughtyList(people){}
function iterativeNaughtyList(persons, habit) {
const naughtyPeople = [];
for(let i = 0; i < persons.length; i++){
const person = persons[i];
for(let j = 0; j < person.habits.length; j++){
const personsHabit = person.habits[j];
if(personsHabit === habit) {
naughtyPeople.push(person.firstName + " " + person.lastName);
// already found the match, stop iterating
break;
}
}
}
return naughtyPeople;
}
const santasNaughtyList = (persons, habit) => {
return persons
.filter((person) => person.habits.includes(habit))
.map((person) => `${person.firstName} ${person.lastName}`)
}
/*
Given an array of person objects with the following keys:
firstName[string]
lastName[string]
friends[arr of friend objects]
isSocialDistancing[bool]
Friend object keys:
firstName[string]
lastName[string]
isSocialDistancing[bool]
isInfected[bool]
return a new array of the names of people who are at high risk of infection
A person is at high risk of catching the virus if they are:
1. not practicing social distancing
2. have a friend who is not practicing social distancing whom is infected
Bonus: after solving it, make a 2nd solution to practice functional programming with built in methods
*/
// Input:
const people = [
{
firstName: "Person",
lastName: "One",
isSocialDistancing: false,
friends: [friend2, friend3],
},
{
firstName: "Person",
lastName: "Two",
isSocialDistancing: true,
friends: [friend2, friend1],
},
{
firstName: "Person",
lastName: "Three",
isSocialDistancing: false,
friends: [friend2, friend1],
},
];
const friend1 = {
firstName: "Friend",
lastName: "One",
isSocialDistancing: false,
isInfected: true,
};
const friend2 = {
firstName: "Friend",
lastName: "Two",
isSocialDistancing: false,
isInfected: true,
};
const friend3 = {
firstName: "Friend",
lastName: "Three",
isSocialDistancing: false,
isInfected: false,
};
// Output: ["Person One", "Person Three"]