Needing Help with Profile Lookup

The code below outputs only what Akira likes. If I want to output more properties such as her phone number or her last name what should I add to my codes below or how my codes should be written to output those properties?


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(name, prop){
// Only change code below this line
for(var x = 0; x < contacts.length; x++){
 if(contacts[x].firstName === name){
 if(contacts[x].hasOwnProperty(prop)){
 return contacts[x][prop];
 }else{
 return "No such property";
 }
 }
}
return "No such contact";
// Only change code above this line
}
// Change these values to test your function
var result =  lookUpProfile("Akira", "likes");
document.write(result);

the function you have written returns the value of the property pased in: lookUpProfile("Akira", "likes");
for this function, to print different things, you need to input a different second argument:

lookUpProfile("Akira", "lastName"); // returns "Laine"
lookUpProfile("Akira", "number"); // returns "0543236543"

If you mean something else, please explain: what should be the input and what output should it give?

1 Like

I tried adding my code like this but it didn’t work:
function lookUpProfile(name, prop, phoneNum){

}
lookUpProfile(‘Akira’, ‘likes’, ‘number’)

Can I add more parameters in the objects? If so, can I see how your codes are written?

You can’t add the code like that because now you’re passing 3 arguments into a function that calls for 2. Check @ieahleen’s example above for how to pass a different prop argument through the function.

1 Like

to do this you need to change the whole function to use also the different parameter

See the second to last line of your code:

var result =  lookUpProfile("Akira", "likes");

the part lookUpProfile("Akira", "likes") is the function call, it takes a name, in this case "Akira", and a property, in this case "likes"
you can use any name and any property, and as long they exist in the object, the property value will be printed.
So I changed "likes" to "lastName", doing lookUpProfile("Akira", "lastName"), and this function call returns "Laine"


yes, you can if that’s what you want (note that in this way you can’t pass the challenge anymore, but if you are changing the code to see how it works, have fun!)

If you don’t remember how to add properties to an object you need to review the challenges about manipulating objects.

1 Like