Profile Lookup Challenge (help)

Tell us what’s happening:
Ok so I’m struggling with another one of these. I can’t seem to get my head around how I am supposed to get the function to look past the first ‘object’ in the array.

I thought since the prior lesson were about Loop iteration I should use that but it doesn’t work. The only name that works is the first one. I can get Akira and the properties to work with that name only. Any other name gives me a ‘No such Contact’.

I would expect by running

console.log(lookUpProfile("Harry", "likes"));

That it would return the value of ‘likes’ but it doesn’t.

Here’s the code so far:

Your code so far


// 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(name, prop){
// Only change code below this line

for (var i = 0; i < contacts.length; i++) {
if (name == contacts[i].firstName) {
return contacts[i][prop] || contacts[i][prop].values();
}else if (name == contacts[i].firstName && prop != contacts[i][prop]) {
return "No such property";
}else {
return "No such contact";
}

}

// Only change code above this line
}

console.log(lookUpProfile("Harry", "likes"));

Please help me understand why my function is broken. Thank you.

Your browser information:

User Agent is: Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 EdgiOS/45.3.19 Mobile/15E148 Safari/605.1.15.

Challenge: Profile Lookup

Link to the challenge:

I have a hint: Shouldn’t you check if every name matches before returning this statement?

Isn’t that why I have the for loop? I thought that would run through each object.

You do have the for loop, but as soon as your code encounters a return statement, the for loop (and your entire function) stops running. So the first time the code finds a name that doesn’t match, your code will return instead of checking more names.

for (var i = 0; i < contacts.length; i++) {
if (name == contacts[i].firstName) {
return contacts[i][prop] || contacts[i][prop].values();
}else if (prop != contacts[i][prop]) {
contacts[prop] = "No such property";
}else {
return "No such contact";
}

I’m almost there I think. I can get Harry’s array of values now but I’m not getting the ‘no such contact ‘ or ‘no such property’ to pass.

This statement is what I can’t figure out I guess

else if (property does not equal contacts property [with the iteration from the `for` loop] 

then

contacts property = “No such property”

I wrote it out as I understand it. The actual line is in my code above. Thanks again.

It took a couple hours but I figured it out. I had to go read more about the && and || logical operators. I didn’t realize that when used in a non-boolean operation that the OR would execute expression1 if true otherwise it will execute expression 2. This is why using it on a previous challenge didn’t make sense to me.

The Mozilla site broke it down for me

Returns expr1 if it can be converted to true ; otherwise, returns expr2 . Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false .

Thanks again!