-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
439 lines (404 loc) · 13.6 KB
/
app.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
Author: devCodeCamp
Description: Most Wanted Starter Code
*/
//////////////////////////////////////////* Beginning Of Starter Code *//////////////////////////////////////////
"use strict";
//? Utilize the hotkey to hide block level comment documentation
////* Mac: Press "CMD"+"K" and then "CMD"+"/"
////* PC: Press "CTRL"+"K" and then "CTRL"+"/"
/**
* This is the main logic function being called in index.html.
* It operates as the entry point for our entire application and allows
* our user to decide whether to search by name or by traits.
* @param {Array} people A collection of person objects.
*/
function app(people) {
// promptFor() is a custom function defined below that helps us prompt and validate input more easily
// Note that we are chaining the .toLowerCase() immediately after the promptFor returns its value
let searchType = promptFor(
"Do you know the name of the person for whom you are searching?\nEnter 'yes' or 'no'",
yesNo
).toLowerCase();
let searchResults;
// Routes our application based on the user's input
switch (searchType) {
case "yes":
searchResults = searchByName(people);
break;
case "no":
//! TODO #4: Declare a searchByTraits (multiple traits) function //////////////////////////////////////////
//! TODO #4a: Provide option to search for single or multiple //////////////////////////////////////////
searchResults = searchByTraits(people);
break;
default:
// Re-initializes the app() if neither case was hit above. This is an instance of recursion.
app(people);
break;
}
// Calls the mainMenu() only AFTER we find the SINGLE PERSON
mainMenu(searchResults, people);
}
// End of app()
/**
* After finding a single person, we pass in the entire person-object that we found,
* as well as the entire original dataset of people. We need people in order to find
* descendants and other information that the user may want.
* @param {Object[]} person A singular object inside of an array.
* @param {Array} people A collection of person objects.
* @returns {String} The valid string input retrieved from the user.
*/
function mainMenu(person, people) {
// A check to verify a person was found via searchByName() or searchByTrait()
if (!person[0]) {
alert("Could not find that individual.");
// Restarts app() from the very beginning
return app(people);
}
let displayOption = prompt(
`Found ${person[0].firstName} ${person[0].lastName}. Do you want to know their 'info', 'family', or 'descendants'?\nType the option you want or type 'restart' or 'quit'.`
);
// Routes our application based on the user's input
switch (displayOption) {
case "info":
//! TODO #1: Utilize the displayPerson function //////////////////////////////////////////
// HINT: Look for a person-object stringifier utility function to help
let personInfo = displayPerson(person[0]);
alert(personInfo);
break;
case "family":
//! TODO #2: Declare a findPersonFamily function //////////////////////////////////////////
// HINT: Look for a people-collection stringifier utility function to help
let personFamily = findPersonFamily(person[0], people);
alert(personFamily);
break;
case "descendants":
//! TODO #3: Declare a findPersonDescendants function //////////////////////////////////////////
// HINT: Review recursion lecture + demo for bonus user story
let personDescendants = findPersonDescendants(person[0], people);
alert(personDescendants);
break;
case "restart":
// Restart app() from the very beginning
app(people);
break;
case "quit":
// Stop application execution
return;
default:
// Prompt user again. Another instance of recursion
return mainMenu(person, people);
}
}
// End of mainMenu()
/**
* This function is used when searching the people collection by
* a person-object's firstName and lastName properties.
* @param {Array} people A collection of person objects.
* @returns {Array} An array containing the person-object (or empty array if no match)
*/
function searchByName(people) {
let firstName = capitalizeFirstLetter(promptFor("What is the person's first name?", chars));
let lastName = capitalizeFirstLetter(promptFor("What is the person's last name?", chars));
// The foundPerson value will be of type Array. Recall that .filter() ALWAYS returns an array.
let foundPerson = people.filter(function (person) {
if (person.firstName === firstName && person.lastName === lastName) {
return true;
}
});
return foundPerson;
}
// End of searchByName()
/**
* This function will be useful for STRINGIFYING a collection of person-objects
* first and last name properties in order to easily send the information
* to the user in the form of an alert().
* @param {Array} people A collection of person objects.
*/
function displayPeople(people) {
alert(
people
.map(function (person) {
return `${person.firstName} ${person.lastName}`;
})
.join("\n")
);
}
// End of displayPeople()
/**
* This function will be useful for STRINGIFYING a person-object's properties
* in order to easily send the information to the user in the form of an alert().
* @param {Object} person A singular object.
*/
function displayPerson(person) {
let personInfo = `First Name: ${person.firstName}\n`;
personInfo += `Last Name: ${person.lastName}\n`;
personInfo += `Gender: ${person.gender}\n`;
personInfo += `DOB: ${person.dob}\n`;
personInfo += `Height: ${person.height}\n`;
personInfo += `Weight: ${person.weight}\n`;
personInfo += `Eye Color: ${person.eyeColor}\n`;
personInfo += `Occupation: ${person.occupation}\n`;
personInfo += `Parents: ${person.parents}\n`;
personInfo += `Current Spouse: ${person.currentSpouse}\n`;
//! TODO #1a: finish getting the rest of the information to display //////////////////////////////////////////
alert(personInfo);
}
// End of displayPerson()
function findPersonFamily(person, people) {
findSpouse(person, people)
findParent(person, people)
findSibling(person, people)
}
// Found Spouse
function findSpouse(person, people) {
let spouseId = person.currentSpouse;
let foundSpouse = people.filter(function (el) {
if (spouseId === el.id) {
return true;
}
else {
return false;
}
})
if (foundSpouse.length > 0) {
displayPeople(foundSpouse)
}
else {
alert("No spouse found")
}
}
// Found Parent(s)
function findParent(person, people) {
let parentId = person.parents;
let foundParents = people.filter(function (el) {
if (parentId.includes(el.id)) {
return true;
}
else {
return false;
}
})
if (foundParents.length > 0) {
displayPeople(foundParents)
}
else {
alert("No parent(s) found")
}
}
// Found Siblings
function findSibling(person, people) {
let personParents = person.parents;
let foundSiblings = people.filter(function (potentialSibling) {
if (personParents.includes(potentialSibling.parents[0]) || personParents.includes(potentialSibling.parents[1])) {
return true;
}
else {
return false;
}
})
if (foundSiblings.length > 0) {
displayPeople(foundSiblings)
}
else {
alert("No sibling(s) found")
}
}
// End of findPersonFamily()
function findPersonDescendants(person, people) {
let findPerson = findChildren(person, people)
let findGrand = findGrandchild(findPerson, people)
let findDescendants = descendantsList(findPerson, findGrand)
}
// End of findPersonDescendants()
/**
* This function's purpose is twofold:
* First, to generate a prompt with the value passed in to the question parameter.
* Second, to ensure the user input response has been validated.
* @param {String} question A string that will be passed into prompt().
* @param {Function} valid A callback function used to validate basic user input.
* @returns {String} The valid string input retrieved from the user.
*/
function promptFor(question, valid) {
do {
var response = prompt(question).trim();
} while (!response || !valid(response));
return response;
}
// End of promptFor()
/**
* This helper function checks to see if the value passed into input is a "yes" or "no."
* @param {String} input A string that will be normalized via .toLowerCase().
* @returns {Boolean} The result of our condition evaluation.
*/
function yesNo(input) {
return input.toLowerCase() === "yes" || input.toLowerCase() === "no";
}
// End of yesNo()
/**
* This helper function operates as a default callback for promptFor's validation.
* Feel free to modify this to suit your needs.
* @param {String} input A string.
* @returns {Boolean} Default validation -- no logic yet.
*/
function chars(input) {
return true; // Default validation only
}
// End of chars()
//////////////////////////////////////////* End Of Starter Code *//////////////////////////////////////////
// Any additional functions can be written below this line 👇. Happy Coding! 😁
function searchByTraits(people) {
let userCount = Number(prompt("By how many of the person's traits would you like to search?\nTo search a single trait, enter '1'\nTo search more, please enter '2', '3', '4', or '5'"));
if (userCount >= 1 && userCount < 5) {
for (let i = 1; i <= userCount; i++) {
let userChoice = promptFor("Select your search trait:\nGender, DOB, Height, Weight, Eye Color, or Occupation\nQuit", chars).toLowerCase();
let foundMatches;
switch (userChoice) {
case "gender":
foundMatches = searchByGender(people);
displayPeople(foundMatches);
break;
case "dob":
foundMatches = searchByDob(people);
displayPeople(foundMatches);
break;
case "eye color":
foundMatches = searchByEyeColor(people);
displayPeople(foundMatches);
break;
case "height":
foundMatches = searchByHeight(people);
displayPeople(foundMatches);
break;
case "weight":
foundMatches = searchByWeight(people);
displayPeople(foundMatches);
break;
case "occupation":
foundMatches = searchByOccupation(people);
displayPeople(foundMatches);
break;
case "quit":
return;
default:
// Prompt user again. Another instance of recursion
return searchByTraits(people);
}
};
}
else
return searchByTraits(people)
}
function searchByGender(people) {
let genderChoice = promptFor("Please enter 'male' or 'female' for your gender search", chars);
let foundMatches = people.filter(function (el) {
if (el.gender.toLowerCase() === genderChoice.toLowerCase()) {
return true;
}
else {
return false;
}
})
return foundMatches;
}
function searchByDob(people) {
let inputDob = promptFor("Please enter the person's DOB (date of birth, format: m/d/yyyy)", chars);
let foundMatches = people.filter(function (el) {
if (el.dob === inputDob) {
return true;
}
else {
return false;
}
})
return foundMatches;
}
function searchByEyeColor(people) {
let inputColor = promptFor("Please enter the person's eye color: \nBlack, Blue, Brown, or Hazel", chars);
let foundMatches = people.filter(function (el) {
if (el.eyeColor.toLowerCase() === inputColor.toLowerCase()) {
return true;
}
else {
return false;
}
})
return foundMatches;
}
function searchByHeight(people) {
let inputHeight = Number(promptFor("Please enter an integer for the person's height (format: ##)", chars));
let foundMatches = people.filter(function (el) {
if (el.height === inputHeight) {
return true;
}
else {
return false;
}
})
return foundMatches;
}
function searchByWeight(people) {
let inputWeight = Number(promptFor("Please enter an integer for the person's weight (format: ###)", chars));
let foundMatches = people.filter(function(el){
if (el.weight === inputWeight){
return true;
}
else{
return false;
}
})
return foundMatches;
}
function searchByOccupation(people) {
let inputOccupation = promptFor("Please enter the person's occupation:\nArchitect, Assistant, Doctor, Landscaper, Nurse, Politician, Programmer,\nor Student", chars);
let foundMatches = people.filter(function (el) {
if (el.occupation.toLowerCase() === inputOccupation.toLowerCase()) {
return true;
}
else {
return false;
}
})
return foundMatches;
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
function findChildren(person, people) {
let childParent = person.id;
let foundChildren = people.filter(function (potentialChild) {
if (potentialChild.parents.includes(childParent)) {
return true;
}
else {
return false;
}
})
return foundChildren;
}
function findGrandchild(findPerson, people) {
let foundChildren = findPerson;
let grandChild;
for (let i = 0; i < foundChildren.length; i++) {
grandChild = people.filter(function (potentialGrandChild) {
if (potentialGrandChild.parents.includes(foundChildren[i].id)) {
return true;
}
else {
return false;
}
})
}
return grandChild
}
function descendantsList(findPerson, findGrand) {
let foundChildren = findPerson;
let grandChild = findGrand;
if (foundChildren.length > 0 || grandChild.length > 0) {
foundChildren = foundChildren.concat(grandChild)
displayPeople(foundChildren)
}
else {
alert("No descendant(s) found")
}
}