-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfile_Lookup.js
More file actions
143 lines (122 loc) · 4.59 KB
/
Profile_Lookup.js
File metadata and controls
143 lines (122 loc) · 4.59 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
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUpProfile(firstName, prop){ // Basic function with two params
for (var i = 0; i < contacts.length; i++) { // you know a basic for loop through the contacts array.
if (firstName === contacts[i].firstName) { // if the contact i has a firstName that matches my firstName param
return contacts[i].hasOwnProperty(prop) ? contacts[i][prop] : "No such property"; // true
} // Ternary: if (contacts[i].hasOwnProperty(prop)) { return that contacts prop value }
} // else ... return that it doesn't have the property
return "No such contact"; // after the loop is completed. That means the contact wasn't found.
} // because the function would have *HALTED* with the first return statement.
/* Challenge without Comments.
function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (firstName === contacts[i].firstName) {
return contacts[i].hasOwnProperty(prop) ? contacts[i][prop] : "No such property";
}
}
return "No such contact";
// Only change code above this line
}
*/
/* -- breaking the challenge apart with the instuctions:
// A lookUpProfile function that takes firstName
// and a property (prop) as arguments has been pre-written for you.
function lookUpProfile(firstName, prop){
for (var i=0; i < contacts.length; i++){
// The function should check if firstName
// is an actual contact's firstName
if (contacts[i]["firstName"] == firstName) {
// and the given property (prop) is a property of that contact.
if(contacts[i].hasOwnProperty(prop)) {
// If both are true,
// then return the "value" of that property.
return contacts[i][prop];
} else {
// If prop does not correspond to any valid properties
// then return "No such property".
return "No such property";
}
}
}
// If firstName does not correspond to "any" contacts (key word is any)
// then return "No such contact"
return "No such contact";
}
*/
/* Another way to solve the challenge:
function lookUpProfile(firstName, prop){
var result = "No such contact";
for (var i=0; i<contacts.length; i++) {
if (contacts[i].firstName===firstName) {
if (contacts[i].hasOwnProperty(prop)) {
result = contacts[i][prop];
break;
}
else {
result = "No such property";
break;
}
}
}
return result;
}
*/
// Change these values to test your function
lookUpProfile("Kristian", "lastName"); // should return "Vos"
lookUpProfile("Sherlock", "likes"); // should return ["Intriguing Cases", "Violin"]
lookUpProfile("Harry","likes"); // should return an array
lookUpProfile("Bob", "number"); // should return "No such contact"
lookUpProfile("Akira", "address"); // should return "No such property"
/* I am putting this down here because
it is a bit of a more complicated
solution that works quite well:
(Even thought is uses nested ternary operators)
function lookUpProfile(firstName, prop) {
var contact = contacts.find(x => x.firstName === firstName);
return contact ? contact.hasOwnProperty(prop) ? contact[prop] : 'No such property' : 'No such contact';
}
// Another:
return contacts.reduce(function(a, c) {return a ? a : (c.firstName === firstName ? (c.hasOwnProperty(prop) ? c[prop] : "No such property") : "");}, "") || "No such contact";
// Another solution using for...of
// this is kind of like the first but, a little cleaner.
function lookUpProfile(firstName, prop){
// Only change code below this line
for (let contact of contacts) {
if (Boolean(contact.firstName === firstName)) {
if (Boolean(contact.hasOwnProperty(prop))) {
return contact[prop];
}
return "No such property";
}
}
return "No such contact";
// Only change code above this line
}
*/