Lesson Review Profile Lookup
Author
Created by Rafase282 based on FCC Wiki version.
Github | FreeCodeCamp | CodePen | LinkedIn | Website | E-Mail
Details
We have an array of objects representing different people in our contacts lists.
A lookUp function that takes firstName and a property (prop) as arguments has been pre-written for you.
The function should check if firstName is an actual contact's firstName and the given property (prop) is a property of that contact.
If both are true, then return the "value" of that property.
If firstName does not correspond to any contacts then return "No such contact"
If prop does not correspond to any valid properties then return "No such property"
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Useful Links
- Challenge: Accessing Objects Properties with Bracket Notation
- Challenge: Iterate with JavaScript For Loops
Problem Explanation:
-
Change the code below
// Only change code below this lineand up to// Only change code above this line -
Take note that you are editing the inside of the
lookUpfunction- This function includes two parameters,
firstNameandprop
- This function includes two parameters,
-
The function should check look through the
contactlist for the givenfirstNameparameter- If there is a match found, the function should then look for the given
propparameter - If both
firstNameand the associatedpropare found, you should return the value of theprop - If
firstNameis found and no associatedpropis found, you should return"No such property" - If
firstNameisn't found anywhere, you should return"No such contact"
- If there is a match found, the function should then look for the given
Hint: 1
- Use a for loop to cycle through the
contactlist
Hint: 2
- Use a nested
ifstatement to first check if thefirstNamematches, and then checks if thepropmatches
Hint: 3
- Leave your
return "No such contact"out of thefor loopas a final catch-all
Spoiler Alert!
Solution ahead!
Code Solution:
function lookUp(firstName, prop) {
// Only change code below this line
var answer = "No such contact";
contacts.some(function(arg) {
if (arg.firstName === firstName && arg.hasOwnProperty(prop) === true) {
answer = arg[prop];
} else if (arg.hasOwnProperty(prop) === false) {
answer = "No such property";
}
});
return answer;
// Only change code above this line
}
// Change these values to test your function
lookUp("Kristian", "lastName");Code Explanation:
- Declare a variable to hold the answer with a predefined answer.
- use
.some()to check if the object has the contact and the property and save it to the variable. - If it does not pass the previous test then check if it does not have the property and return the right answer.
- Outside of the
.some()return the variable withthe answer.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
