-
Notifications
You must be signed in to change notification settings - Fork 341
Lesson Review Profile Lookup
Created by Rafase282 based on FCC Wiki version.
Github | FreeCodeCamp | CodePen | LinkedIn | Website | E-Mail
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.
- Challenge: Accessing Objects Properties with Bracket Notation
- Challenge: Iterate with JavaScript For Loops
-
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
- Use a for loop to cycle through the
contactlist
- Use a nested
ifstatement to first check if thefirstNamematches, and then checks if thepropmatches
- Leave your
return "No such contact"out of thefor loopas a final catch-all
Solution ahead!
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");- 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.
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3