-
Notifications
You must be signed in to change notification settings - Fork 0
Challenge Profile Lookup
Instructions
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
lookUpProfilefunction- 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!
for (var x = 0; x < contacts.length; x++){
if (contacts[x].firstName === firstName) {
if (contacts[x].hasOwnProperty(prop)) {
return contacts[x][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
-
The for loop runs, starting at the first object in the
contactslist -
If the
firstNameparameter passed into the function matches the value of the"firstName"key in the first object, theifstatement passes -
Then, we use
.hasOwnProperty()method (checks if there's a given property and returns a boolean) withpropas an argument,if it's true the value of thepropis returned- If the second
ifstatement fails,"No such property"is returned
- If the second
-
If the first
ifstatement fails, the for loop continues on to the next object in thecontactlist -
If the
firstNameparameter isn't matched by the finalcontactobject, the for loop exits and"No such contact"is returned
Example Run
-
lookUpProfile("Akira","likes");runs -
"Akira"is matched to the"firstName"key in the first object, so theifstatement returns true -
"likes"is found within the first object, so the secondifstatement returns true - The value of
"likes"is returned -"Pizza", "Coding", "Brownie Points"
If you found this page useful, you can give thanks by copying and pasting this on the main chat: thanks @leftynaut for your help with Checkpoint: Profile Lookup
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links