patternjavascriptMinor
Javascript: Profile Lookup from freecodecamp
Viewed 0 times
javascriptprofilefreecodecamplookupfrom
Problem
This is my answer in freecodecamp "Basic Javascript: Profile Lookup" (third day trying javascript using Udemy and freecodecamp).
My problem with the code is the huge number of lines on my
```
//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){
// Only change code below this line
var contain = 0;
if(firstName == "Akira"){
contain = 0;
if(contacts[contain].firstName == firstName)
if(contacts[contain].hasOwnProperty(prop))
return contacts[contain][prop];
else
return "No such property";
else
return "No such contact";
}
else if(firstName == "Harry"){
contain = 1;
if(contacts[contain].firstName == firstName)
if(contacts[contain].hasOwnProperty(prop))
return contacts[contain][prop];
else
return "No such property";
else
return "No such contact";
}
else if(firstName == "Sherlock"){
contain = 2;
if(contacts[contain].firstName == firstName)
if(contacts[contain].hasOwnProperty(prop))
return contacts[contain][prop];
else
return "No such property";
else
return "No such contact";
}
else if(firstName == "Kristian"){
contain = 3;
if(contacts[contain].f
My problem with the code is the huge number of lines on my
lookUpProfile function. I would like to minimize the lines of code of my lookUpProfile function, can you suggest me ways to achieve that? ```
//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){
// Only change code below this line
var contain = 0;
if(firstName == "Akira"){
contain = 0;
if(contacts[contain].firstName == firstName)
if(contacts[contain].hasOwnProperty(prop))
return contacts[contain][prop];
else
return "No such property";
else
return "No such contact";
}
else if(firstName == "Harry"){
contain = 1;
if(contacts[contain].firstName == firstName)
if(contacts[contain].hasOwnProperty(prop))
return contacts[contain][prop];
else
return "No such property";
else
return "No such contact";
}
else if(firstName == "Sherlock"){
contain = 2;
if(contacts[contain].firstName == firstName)
if(contacts[contain].hasOwnProperty(prop))
return contacts[contain][prop];
else
return "No such property";
else
return "No such contact";
}
else if(firstName == "Kristian"){
contain = 3;
if(contacts[contain].f
Solution
You have very repetitive code. This can be optimized. I'm not sure whether your function name
How it works
It loops through all contacts:
It tests whether the
In case a match is found it returns the requested property or
In case no match is found it returns
What it returns
This function will have a mixed return value. The code that is calling the function can now deal with the result. It will return one of the following:
So all those calls will work:
The return values are:
What the code doesn't do:
It doesn't check whether there's a choice for
jsFiddle Demo
Try before buy
Update "No such property"
Regarding the OP's comment about a different return value, I would suggest keeping the code as is and let other parts worry about a concrete message:
If your task is to include that text into the function you can replace this line:
with:
This is a lot harder to handle, as the string "No such property" could be an actual value of a property.
lookUpProfile is the correct one, as you actually look up a property of a Contact and not its whole profile. But if the task is to look up a certain property of a Contact you can simplify the code to this:function lookUpProfile(firstName, prop) {
for (let contact of contacts) {
if (contact.firstName === firstName) {
return contact[prop];
}
}
return false;
}How it works
It loops through all contacts:
for (let contact of contacts) {It tests whether the
firstName matches:if (contact.firstName === firstName) {In case a match is found it returns the requested property or
undefined:return contact[prop];In case no match is found it returns
false:return false;What it returns
This function will have a mixed return value. The code that is calling the function can now deal with the result. It will return one of the following:
falseif noContactwith the requestedfirstNameexists
undefinedif aContactmatches but the requestedpropertydoesn't exists
string|arrayif a match was found and the property exists
So all those calls will work:
console.log(
lookUpProfile(),
lookUpProfile('A'),
lookUpProfile('Sherlock'),
lookUpProfile('Sherlock', 'number'),
lookUpProfile('Kristian', 'likes')
);The return values are:
false false undefined 0487345643 Array [ "Javascript", "Gaming", "Foxes" ]What the code doesn't do:
It doesn't check whether there's a choice for
Contacts, i.e. if more than one Contact with that name exists. It will always exit at the first match.jsFiddle Demo
Try before buy
Update "No such property"
Regarding the OP's comment about a different return value, I would suggest keeping the code as is and let other parts worry about a concrete message:
var value = lookUpProfile('Sherlock', 'random');
if ('undefined' === typeof value) {
console.log('No such property');
};If your task is to include that text into the function you can replace this line:
return contact[prop];with:
return 'undefined' === typeof contact[prop] ? 'No such property' : contact[prop];This is a lot harder to handle, as the string "No such property" could be an actual value of a property.
Code Snippets
function lookUpProfile(firstName, prop) {
for (let contact of contacts) {
if (contact.firstName === firstName) {
return contact[prop];
}
}
return false;
}for (let contact of contacts) {if (contact.firstName === firstName) {return contact[prop];return false;Context
StackExchange Code Review Q#152310, answer score: 4
Revisions (0)
No revisions yet.